Google drive command-line tools
Tools to maintain files on Google Drive.
Create a new project for this tool to access your Google drive
Enable the following Google APIs in "APIs & auth/APIs"
Make sure your application has an application name in "APIs & auth/Credentials/OAuthConsent screen"
Grant access to Google Drive for gdcmdtools in "APIs & auth/Credentials"
Use the tool to pass the OAuth2 authentication
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"
% python ./gdauth.py /tmp/client_secrets.json # Use the /tmp/client_secrets.json as secret file
This tool can be used to upload files to Google drive as Spreadsheet,csv,fusion table,doc, etc.
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
% 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'
get files from google drive
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
% 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
This tool can be used to set up file's permission
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
% 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
BSD 2-Clause License
Please report bugs via https://github.com/tienfuc/gdcmdtools/issues
Tien Fu Chen
Email: tienfu.c @ gmail.com