├── .gitignore ├── LICENSE ├── Pipfile ├── README.md ├── downloader.py ├── gdrive_upload.py ├── requirements.txt └── start.sh /.gitignore: -------------------------------------------------------------------------------- 1 | *.py[cod] 2 | 3 | # C extensions 4 | *.so 5 | 6 | # Packages 7 | *.egg 8 | *.egg-info 9 | dist 10 | build 11 | eggs 12 | parts 13 | bin 14 | var 15 | sdist 16 | develop-eggs 17 | .installed.cfg 18 | lib 19 | lib64 20 | 21 | # Installer logs 22 | pip-log.txt 23 | 24 | # Unit test / coverage reports 25 | .coverage 26 | .tox 27 | nosetests.xml 28 | 29 | # Translations 30 | *.mo 31 | 32 | # Mr Developer 33 | .mr.developer.cfg 34 | .project 35 | .pydevproject 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2018 Sumanjay 2 | All Rights Reserved. 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | url = "https://pypi.org/simple" 3 | verify_ssl = true 4 | name = "pypi" 5 | 6 | [dev-packages] 7 | 8 | [packages] 9 | "beautifulsoup4" = "==4.6.0" 10 | google-api-python-client = "==1.7.3" 11 | google-auth = "==1.5.0" 12 | "google-auth-httplib2" = "==0.0.3" 13 | "httplib2" = "==0.18.0" 14 | more-itertools = "==4.2.0" 15 | ndg-httpsclient = "==0.4.0" 16 | oauth = "==1.0.1" 17 | "urllib3" = "==1.24.2" 18 | virtualenv = "==15.0.1" 19 | 20 | [requires] 21 | python_version = "3.5" 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Google Drive Uploader 2 | ===================== 3 | 4 | Simple script to upload files on Google Drive through Direct URL and gives the direct drive download link. 5 | 6 | # Description 7 | Script uses predefined Drive APIs Client ID and Client Secret (Please don't abuse the credentials) 8 | 9 | It takes upload file as command line argument, uploads it and sets permissions that anyone who has download link can download the file. 10 | After file has been uploaded the script prints direct download url. 11 | 12 | # Requirements 13 | * Python >= 2.6 [Tested on Python 3.6.7] 14 | * [google-api-python-client](http://code.google.com/p/google-api-python-client/) 15 | * python-httplib2 16 | * urllib 17 | * sys 18 | * logging 19 | * mimetypes 20 | * apiclient 21 | * oauth2client 22 | * wget 23 | 24 | #### Install all requirements using this command: 25 | `sudo -H pip3 install -r requirements.txt` 26 | # Example Usage: 27 | #### Note: Make sure you've given the bash file permission to execute 28 | `sudo chmod +x start.sh` 29 | 30 | #### You can also start the file without using bash using the command 31 | 32 | `python3 gdrive_upload.py` 33 | 34 | > cyberboy@MonsterMachine:~/python/gdrive/Gdrivedownloader$ ./start.sh 35 | >Enter the Direct URL of the file you want to download: 36 | >https://cyberboysumanjay.github.io/img/jetpacktocat.jpg 37 | >Beginning file download... 38 | >This may take time depending upon the file size. 39 | >Please be patient 40 | >jetpacktocat.jpg is successfully downloaded locally 41 | >Starting Google Drive upload..... 42 | >Here is your Google Drive link: https://drive.google.com/uc?id=1y3qM4xqNTUf2B_NWAdBeBeO7F2lumtMw&export=download 43 | >Performing local cleanup......... 44 | >Everything done... Exiting! 45 | 46 | # Credits 47 | ### kshcherban for his lovely work [here](https://github.com/kshcherban/gdrive_uploader) 48 | -------------------------------------------------------------------------------- /downloader.py: -------------------------------------------------------------------------------- 1 | import urllib.request 2 | import os 3 | import sys 4 | import subprocess 5 | import wget 6 | 7 | def start(): 8 | url=input("Enter the Direct URL of the file you want to download: ") 9 | print('Beginning file download...') 10 | print('This may take time depending upon the file size.\nPlease be patient') 11 | filename=local_download(url) 12 | return filename 13 | 14 | def local_download(url): 15 | filename='none' 16 | try: 17 | filename=wget.download(url) 18 | except Exception as e: 19 | print("Error! Unable to download file.") 20 | print(e) 21 | else: 22 | print(filename+ ' is successfully downloaded locally ') 23 | print('Starting Google Drive upload.....') 24 | return filename 25 | 26 | 27 | def local_delete(filename): 28 | print('Performing local cleanup.........') 29 | os.remove(filename) 30 | print('Everything done... \nExiting!') 31 | -------------------------------------------------------------------------------- /gdrive_upload.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import logging 3 | import httplib2 4 | import urllib.request 5 | from mimetypes import guess_type 6 | import downloader 7 | # Following libraries can be installed by executing: sudo -H pip3 install --upgrade google-api-python-client 8 | from apiclient.discovery import build 9 | from apiclient.http import MediaFileUpload 10 | from apiclient.errors import ResumableUploadError 11 | from oauth2client.client import OAuth2WebServerFlow 12 | from oauth2client.file import Storage 13 | from oauth2client import file, client, tools 14 | 15 | # Log only oauth2client errors 16 | logging.basicConfig(level="ERROR") 17 | 18 | # Path to token json file, it should be in same directory as script 19 | token_file = sys.path[0] + '/auth_token.txt' 20 | 21 | # Copy your credentials from the APIs Console 22 | #Please don't abuse these credentials 23 | CLIENT_ID = '466641482782-fleutfims8sr6rv3296l2hiefavnl7i0.apps.googleusercontent.com' 24 | CLIENT_SECRET = 'nNKJC_zi-x0T7wVFpO5-Q_68' 25 | # Check https://developers.google.com/drive/scopes for all available scopes 26 | OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive.file' 27 | # Redirect URI for installed apps, can be left as is 28 | REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob' 29 | 30 | 31 | # Get mime type and name of given file 32 | def file_ops(file_path): 33 | mime_type = guess_type(file_path)[0] 34 | mime_type = mime_type if mime_type else 'text/plain' 35 | file_name = file_path.split('/')[-1] 36 | return file_name, mime_type 37 | 38 | 39 | def create_token_file(token_file): 40 | # Run through the OAuth flow and retrieve credentials 41 | flow = OAuth2WebServerFlow( 42 | CLIENT_ID, 43 | CLIENT_SECRET, 44 | OAUTH_SCOPE, 45 | redirect_uri=REDIRECT_URI 46 | ) 47 | authorize_url = flow.step1_get_authorize_url() 48 | print('Go to the following link in your browser: ' + authorize_url) 49 | code = input('Enter verification code: ').strip() 50 | credentials = flow.step2_exchange(code) 51 | storage = Storage(token_file) 52 | storage.put(credentials) 53 | return storage 54 | 55 | 56 | def authorize(token_file, storage): 57 | # Get credentials 58 | if storage is None: 59 | storage = Storage(token_file) 60 | credentials = storage.get() 61 | # Create an httplib2.Http object and authorize it with our credentials 62 | http = httplib2.Http() 63 | credentials.refresh(http) 64 | http = credentials.authorize(http) 65 | return http 66 | 67 | 68 | def upload_file(file_path, file_name, mime_type): 69 | # Create Google Drive service instance 70 | drive_service = build('drive', 'v2', http=http) 71 | # File body description 72 | media_body = MediaFileUpload(file_path, 73 | mimetype=mime_type, 74 | resumable=True) 75 | body = { 76 | 'title': file_name, 77 | 'description': 'backup', 78 | 'mimeType': mime_type, 79 | } 80 | # Permissions body description: anyone who has link can upload 81 | # Other permissions can be found at https://developers.google.com/drive/v2/reference/permissions 82 | permissions = { 83 | 'role': 'reader', 84 | 'type': 'anyone', 85 | 'value': None, 86 | 'withLink': True 87 | } 88 | # Insert a file 89 | file = drive_service.files().insert(body=body, media_body=media_body).execute() 90 | # Insert new permissions 91 | drive_service.permissions().insert(fileId=file['id'], body=permissions).execute() 92 | # Define file instance and get url for download 93 | file = drive_service.files().get(fileId=file['id']).execute() 94 | download_url = file.get('webContentLink') 95 | return download_url 96 | 97 | if __name__ == '__main__': 98 | file_path=downloader.start() 99 | try: 100 | with open(file_path) as f: pass 101 | except IOError as e: 102 | print(e) 103 | sys.exit(1) 104 | # Check if token file exists, if not create it by requesting authorization code 105 | try: 106 | with open(token_file) as f: pass 107 | except IOError: 108 | http = authorize(token_file, create_token_file(token_file)) 109 | # Authorize, get file parameters, upload file and print out result URL for download 110 | http = authorize(token_file, None) 111 | file_name, mime_type = file_ops(file_path) 112 | # Sometimes API fails to retrieve starting URI, we wrap it. 113 | try: 114 | print("Here is your Google Drive link: "+upload_file(file_path, file_name, mime_type)) 115 | except ResumableUploadError as e: 116 | print("Error occured while first upload try:", e) 117 | print("Trying one more time.") 118 | print("Here is your Google Drive link: "+upload_file(file_path, file_name, mime_type)) 119 | finally: 120 | downloader.local_delete(file_path) 121 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | google-api-python-client>=1.7.3 2 | google-auth>=1.5.0 3 | google-auth-httplib2<=0.0.3 4 | httplib2>=0.11.3 5 | more-itertools>=4.2.0 6 | ndg-httpsclient>=0.4.0 7 | oauth>=1.0.1 8 | pyasn1>=0.4.3 9 | pyasn1-modules>=0.2.1 10 | pycurl>=7.43.0 11 | pydns>=2.3.6 12 | uritemplate>=3.0.0 13 | urllib3>=1.23 14 | virtualenv>=15.0.1 15 | wget 16 | -------------------------------------------------------------------------------- /start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | python3 gdrive_upload.py 3 | --------------------------------------------------------------------------------