gdcmdtools

Google drive command-line tools


License
BSD-3-Clause
Install
pip install gdcmdtools==0.98

Documentation

Status

Build Status

pypi

What's new

  • 0.98
    • added gdrm.py, able to remove files and folder.

gdcmdtools

Google drive command-line tools

Tools to maintain files on Google Drive.

Features

Installation

  • Check your system has Python installed
    • gdcmdtools are developed under Python 2.7.3, I would suggest users to run the tools with Python 2.7.3.(OK: 2.7.2)
  • PIP
    • For Mac OSX
      1. sudo easy_install pip
      2. sudo pip install google-api-python-client
      3. sudo pip install gdcmdtools
    • For Ubuntu Linux
      1. sudo apt-get install python-pip
      2. sudo pip install google-api-python-client
      3. sudo pip install gdcmdtools
    • After the installation, you will see gd*.py under /usr/local/bin.
  • Ubuntu Linux PPA
    1. sudo apt-add-repository ppa:ctf/gdcmdtools
    2. sudo apt-get update
    3. sudo apt-get install python-gdcmdtools

API Setup

  • Create a new project for this tool to access your Google drive

  • Enable the following Google APIs in "APIs & auth/APIs"

    • Drive API
    • Fusion Tables API
  • Make sure your application has an application name in "APIs & auth/Credentials/OAuthConsent screen"

    • Find "PRODUCT NAME" field. Make sure it's not blank.
  • Grant access to Google Drive for gdcmdtools in "APIs & auth/Credentials"

    1. Click "Create new Client ID", APPLICATION TYPE: Installed application, INSTALLED APPLICATION TYPE: Other
    2. Check the section "Client ID for native application", click at the "Download JSON".
    3. Execute gdauth.py in a terminal and give the downloaded secret file just downloaded as parameter: $ gdauth.py client_secrets.json
    4. You will see message like: INFO:gdcmdtools.base:Please visit the URL in your browser: https://accounts.google.com/o/oauth2/auth?scope=....
    5. Visit the URL with browser and allow the app accessing your Google Drive.
    6. Copy the code you see in your browser, then back to the terminal, paste the code and hit enter.
    7. Done, you won't be asked for the code again unless the credential expired.

gdauth

Use the tool to pass the OAuth2 authentication

Usage

usage: gdauth.py [-h] [-r {local,oob}] secret_file

gdauth v0.0.1 - Google Drive OAuth2 authentication tool - gdcmdtools (Google Drive command line tools)

positional arguments:
  secret_file           the secret file in JSON format, ~/.gdcmdtools.secrets will be overwritten

optional arguments:
  -h, --help            show this help message and exit
  -r {local,oob}, --redirect_uri {local,oob}
                        specify the redirect URI for the oauth2 flow, could be:
                        local: means "http://localhost"
                        oob: (default) means "urn:ietf:wg:oauth:2.0:oob"

Examples for gdauth

% python ./gdauth.py /tmp/client_secrets.json   # Use the /tmp/client_secrets.json as secret file

gdput

This tool can be used to upload files to Google drive as Spreadsheet,csv,fusion table,doc, etc.

Usage

usage: gdput.py [-h] [-s SOURCE_TYPE] [-l TARGET_TITLE]
                [-d TARGET_DESCRIPTION] [--no_print_id] [-f FOLDER_ID]
                [--debug {debug,info,warning,error,critical}]
                [-p TYPE ROLE VALUE] [-t {raw,ft,pt,ss,doc,ocr,dr,gas}]
                [--ft_latlng_column FT_LATLNG_COLUMN]
                [--ft_location_column FT_LOCATION_COLUMN]
                [--csv_column_define define1_define2_defineN...]
                source_file

gdput v0.91 - Tool to upload file to Google Drive - gdcmdtools (Google Drive command line tools)

positional arguments:
  source_file           The file you're going to upload to Google Drive

optional arguments:
  -h, --help            show this help message and exit
  -s SOURCE_TYPE, --source_type SOURCE_TYPE
                        define the source file type by MIME type,
                        ex: "text/csv", or "auto" to determine the file type by file name
  -l TARGET_TITLE, --target_title TARGET_TITLE
                        specify the title of the target file
  -d TARGET_DESCRIPTION, --target_description TARGET_DESCRIPTION
                        specify the description of the target file
  --no_print_id         set if you like not to print the file id after file being uploaded
  -f FOLDER_ID, --folder_id FOLDER_ID
                        the target folder ID on the Google drive
  --debug {debug,info,warning,error,critical}
                        define the debug level
  -p TYPE ROLE VALUE, --permission TYPE ROLE VALUE
                        set the permission of the uploaded file, can be:
                        type: user, group, domain, anyone
                        role: owner, reader, writer
                        value: user or group e-mail address,
                        or 'me' to refer to the current authorized user
                        ex: -p anyone reader me # set the uploaded file public-read
  -t {raw,ft,pt,ss,doc,ocr,dr,gas}, --target_type {raw,ft,pt,ss,doc,ocr,dr,gas}
                        define the target file type on Google Drive, can be:
                        raw: (default) the source file will uploaded without touching
                        ft: Fusion Table (for .csv)
                        pt: Presentation (for .ppt, .pps, .pptx)
                        ss: Spreadsheet (for .xls, .xlsx, .ods, .csv, .tsv, .tab)
                        doc: Document (for .doc, .docx, .html, .htm, .txt, .rtf)
                        ocr: OCR (for .jpg, .git, .png, .pdf)
                        dr: Drawing (for .wmf)
                        gas: GAS project (for .json)

fusion table support (--target_type ft):
  --ft_latlng_column FT_LATLNG_COLUMN
                        specify the column header for latitude and longitude for the fusion table,
                        the column will be created automatically
  --ft_location_column FT_LOCATION_COLUMN
                        specify the location column header for the fusion table
  --csv_column_define define1_define2_defineN...
                        define the columns type for each column of the csv file,
                        can be "string", "number", "datetime", or "location".
                        ex: has 4 columns in the csv file: "name", "age", "birthday", "address".
                        you can set --csv_column_define string_number_datetime_location

Examples for gdput

% python ./gdput.py photo.jpg                    # upload photo.jpg to gd without changing the format
% python ./gdput.py -t ft samples/sample.csv     # upload a csv file to gd as fusion table
% python ./gdput.py -t ss samples/sample.csv     # upload a csv file to gd as spreadsheet
% python ./gdput.py -t ft --ft_location_column address  --ft_latlng_column latlng  samples/sample.csv 
                                          # upload a csv to gd as fusion table with geocoding the latitude longitude data according to the address rows
% python ./gdput.py -p anyone reader me samples/sample.csv     
                                          # upload a csv file as Spreadsheet and set the file public-read
% python ./gdput.py -t ft --csv_column_define string_string_location_string_string_number samples/sample.csv
                                          # upload a csv file as ft with specifed column type
% python ./gdput.py -t gas gas_project.json
                                          # update gas project with project file 'gas_project.json'

gdget

get files from google drive

Usage

usage: gdget.py [-h] [-f FORMAT] [-s NEW_FILE_NAME]
                [--debug {debug,info,warning,error,critical}]
                file_id

gdget v0.91 - Tool to download file from Google Drive - gdcmdtools (Google Drive command line tools)

positional arguments:
  file_id               The id for the file you're going to download

optional arguments:
  -h, --help            show this help message and exit
  -f FORMAT, --export_format FORMAT
                        specify the export format for downloading,
                        google_format: export_format
                        spreadsheet: pdf, ods, xlsx
                        drawing: png, pdf, jpeg, svg
                        document: pdf, docx, rtf, odt, html, txt
                        script+json: json
                        presentation: pdf, pptx, txt
  -s NEW_FILE_NAME, --save_as NEW_FILE_NAME
                        save the downloaded file as 
  --debug {debug,info,warning,error,critical}
                        define the debug level

Examples for gdget

% python ./gdget.py FILE_ID --export_format pdf -s /tmp/myfile.pdf # export the file as pdf and save as /tmp/myfile.pdf
% python ./gdget.py FILE_ID -f json # down files associated with GAS project

gdperm

This tool can be used to set up file's permission

Usage

usage: gdperm.py [-h]
                 [--list | --get PERMISSION_ID | --insert TYPE ROLE VALUE | --delete PERMISSION_ID]
                 file_id

gdperm v0.0.1 - Tool to change file's permission on Google Drive - gdcmdtools (Google Drive command line tools)

positional arguments:
  file_id               The id of file you're going to change permission

optional arguments:
  -h, --help            show this help message and exit
  --list                list the permission resource of the file
  --get PERMISSION_ID   get the permission resource by id
  --insert TYPE ROLE VALUE
                        insert the permission to the file by id
  --delete PERMISSION_ID
                        delete the permission of the file by id

Examples for gdperm

% python ./gdperm.py 0B_XXXXXXXXXX --insert anyone reader me   # set the file as public-read
% python ./gdperm.py 0B_XXXXXXXXXX --list                      # list the permissions by file id: 0B_XXXXXXXXXX
% python ./gdperm.py 0B_XXXXXXXXXX --get 5566520               # get the permissions by permission id: 5566520
% python ./gdperm.py 0B_XXXXXXXXXX --delete 5566520            # delete the permissions by permission id: 5566520

Packages

License

BSD 2-Clause License

Bug

Please report bugs via https://github.com/tienfuc/gdcmdtools/issues

Author

Tien Fu Chen

Email: tienfu.c @ gmail.com