gdown

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2025 License: MIT Imports: 22 Imported by: 0

README

gdown

gdown is a Go package and CLI tool for downloading files and folders from Google Drive. It is a complete re‑implementation of wkentaro/gdown (originally written in Python) in Golang.

This project was created by asking chatgpt o3-mini-high to convert the original Python code into Go. Even this README was written by chatgpt o3-mini-high.

🚀 Features

  • Download Files: Download files from Google Drive via URL or file ID.
  • Cached Downloads: Use a caching mechanism to avoid repeated downloads.
  • Resume Downloads: Resume interrupted downloads.
  • Download Folders: Recursively download an entire Google Drive folder with preserved structure.
  • List Folder Contents: Retrieve detailed information about the files and folders within a Google Drive folder, including individual download URLs.
  • Extract Archives: Extract archive files (e.g., ZIP, TAR, TAR.GZ) to a specified directory.
  • CLI Interface: The project provides a comprehensive CLI with subcommands for each public function, powered by ffcli.

📦 Installation

Using go install

Ensure you have Go installed. Then install the package with:

go install github.com/igolaizola/gdown/cmd/gdown@latest
🛠️ Build from Source

Clone the repository and build the CLI tool:

git clone https://github.com/yourusername/gdown.git
cd gdown
go build -o gdown

📋 Usage

Command-Line Interface (CLI)

The CLI tool exposes subcommands for each public function in the package.

📝 Version

Print the version information:

./gdown version
📥 Download a File

Download a single file from Google Drive by URL:

./gdown download -url "https://drive.google.com/uc?id=FILE_ID" -output "myfile.txt"

Flags:

  • -url: URL of the file to download (required).
  • -output: Output file name (if empty, the basename of the URL is used).
  • -quiet: Suppress logging output.
  • -proxy: Set a proxy URL (e.g., http://host:port).
  • -speed: Limit download speed in bytes per second (0 means unlimited).
  • -no-cookies: Do not use cookies.
  • -no-verify: Skip TLS certificate verification.
  • -resume: Resume an interrupted download.
  • -fuzzy: Enable fuzzy file ID extraction (Google Drive only).
  • -format: Specify a file format (for Google Docs/Sheets/Slides).
  • -user-agent: Custom User-Agent string.
🗃️ Cached Download

Download a file using a caching mechanism:

./gdown cachedownload -url "https://drive.google.com/uc?id=FILE_ID" -output "cachedfile.txt" -hash "md5:YOUR_HASH"
📂 Download a Folder

Download an entire Google Drive folder:

./gdown downloadfolder -id "FOLDER_ID" -output "folder_download_dir"

Flags:

  • -url or -id: Provide either the folder URL or the folder ID.
  • Other flags are similar to the file download options.
📑 List Folder Contents

List the contents of a Google Drive folder, showing details for each file (including a download URL):

./gdown listfolder -id "FOLDER_ID"
📦 Extract an Archive

Extract an archive file (ZIP, TAR, TAR.GZ, etc.):

./gdown extractall -archive "archive.zip" -to "destination_dir"
🔍 Parse a URL

Extract a Google Drive file ID from a URL:

./gdown parseurl -url "https://drive.google.com/file/d/FILE_ID/view"
🧑‍💻 Programmatic Usage

You can also use gdown as a library in your own Go projects. For example:

package main

import (
    "fmt"
    "log"

    "github.com/igolaizola/gdown"
)

func main() {
    opts := gdown.DownloadOptions{
        Quiet:      false,
        UseCookies: true,
        Verify:     true,
        Resume:     true,
        Speed:      0, // unlimited
    }
    output, err := gdown.Download("https://drive.google.com/uc?id=FILE_ID", "myfile.txt", opts)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Downloaded file saved to: %s\n", output)
}

🏗️ Project Background & Credits

  • Based on gdown: This project is inspired by and based on gdown, a popular Python tool for downloading files from Google Drive.
  • Conversion to Go: The original Python code was converted into Golang by asking chatgpt o3-mini-high to help translate and adapt the logic.
  • Contributors:
    • Original gdown: wkentaro
    • gdown conversion: chatgpt o3-mini-high (with human oversight)

📚 Dependencies

  • goquery for HTML parsing.
  • ffcli for CLI flag parsing and subcommand handling.
  • ffyaml for YAML configuration file support.

Install dependencies via:

go get github.com/PuerkitoBio/goquery
go get github.com/peterbourgon/ff/v3
go get github.com/peterbourgon/ff/v3/ffyaml

📜 License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

View Source
const (
	CHUNK_SIZE       = 512 * 1024
	MAX_NUMBER_FILES = 50
)

Variables

View Source
var (
	// ErrFileURLRetrieval is returned when a file’s public URL cannot be determined.
	ErrFileURLRetrieval = errors.New("failed to retrieve file URL")
)

Functions

func CachedDownload

func CachedDownload(urlStr, outputPath, hash string, quiet bool, postprocess func(string) error, opts DownloadOptions) (string, error)

func Download

func Download(urlStr, output string, opts DownloadOptions) (string, error)

func DownloadFolder

func DownloadFolder(urlStr, id, output string, opts FolderOptions) ([]string, error)

func ExtractAll

func ExtractAll(archivePath, to string) ([]string, error)

func IsGoogleDriveUrl

func IsGoogleDriveUrl(urlStr string) bool

func MD5Sum

func MD5Sum(filename string) (string, error)

func ParseUrl

func ParseUrl(urlStr string, warn bool) (fileId string, isDownloadLink bool, err error)

ParseUrl extracts a Google Drive file ID (if any) from the URL.

Types

type DownloadOptions

type DownloadOptions struct {
	Quiet      bool
	Proxy      string
	Speed      int64 // bytes per second; 0 means unlimited
	UseCookies bool
	Verify     bool
	Resume     bool
	Fuzzy      bool
	Format     string
	UserAgent  string
}

type FileInfo

type FileInfo struct {
	ID          string
	Path        string // relative path within the folder
	DownloadURL string // non-empty for files; empty for folders
	IsFolder    bool
}

New: FileInfo type and ListFolder function. FileInfo holds details about a file or folder within a Google Drive folder. For files, DownloadURL is provided so you can call Download individually.

func ListFolder

func ListFolder(urlStr, id string, opts FolderOptions) ([]FileInfo, error)

ListFolder retrieves a folder’s structure and returns a list of FileInfo. Either urlStr or id must be specified (but not both). For files, DownloadURL is set.

type FileToDownload

type FileToDownload struct {
	ID        string
	Path      string // relative path within the folder
	LocalPath string
}

FileToDownload holds information for a file (or folder) within a folder.

type FolderOptions

type FolderOptions struct {
	DownloadOptions
	RemainingOk bool
}

type GoogleDriveFile

type GoogleDriveFile struct {
	ID       string
	Name     string
	Type     string
	Children []*GoogleDriveFile
}

GoogleDriveFile represents a file or folder on Google Drive.

func (*GoogleDriveFile) IsFolder

func (f *GoogleDriveFile) IsFolder() bool

type ThrottledWriter

type ThrottledWriter struct {
	// contains filtered or unexported fields
}

func NewThrottledWriter

func NewThrottledWriter(w io.Writer, speed int64) *ThrottledWriter

func (*ThrottledWriter) Write

func (tw *ThrottledWriter) Write(p []byte) (n int, err error)

Directories

Path Synopsis
cmd
gdown
main.go
main.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL