Skip to content

Instantly share code, notes, and snippets.

@thealphadollar
Last active April 1, 2018 04:45
Show Gist options
  • Save thealphadollar/fcd771b97eafc34efd534a49e2475d2d to your computer and use it in GitHub Desktop.
Save thealphadollar/fcd771b97eafc34efd534a49e2475d2d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
'''
requires python-pydrive
'''
import multiprocessing
import os
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import ntpath
dir_base = os.path.join(os.path.expanduser('~'), "Downloads/test_up")
def get_f_name(addr):
"""
splits address into head and tail
Args:
addr: path to the file
Returns:
file name
"""
head, tail = ntpath.split(addr)
return tail or ntpath.basename(head) # return tail when file, otherwise other one for folder
def f_create(drive, addr):
"""
creates and uploads file to drive
Args:
drive: GoogleDrive class from PyDrive module
addr: path to the file to be uploaded
Returns:
True: upload successful
False: upload failed
"""
# check whether address is right or not
if not os.path.exists(addr):
print("Specified file/folder doesn't exist, check the address!")
return False
# print progress
print("uploading file " + addr)
up_file = drive.CreateFile()
up_file.SetContentFile(addr)
up_file['title'] = get_f_name(addr) # sets file title to original
up_file.Upload()
return True
def main():
g_auth = GoogleAuth()
g_auth.LocalWebserverAuth()
drive = GoogleDrive(g_auth)
files = ["c8.ts", "tracesports.ts", "cinemax.ts", "c8 (copy).ts",
"cinemax (another copy).ts", "cinemax (copy).ts", "tracesports (another copy).ts", "tracesports (copy).ts"]
# uncomment commented lines below and comment line 70 to use multiprocessing
# processes = multiprocessing.Pool(processes=multiprocessing.cpu_count())
for f in files:
address = os.path.join(dir_base, str(f))
# processes.apply_async(f_create, args=(drive, address,))
f_create(drive, address)
# processes.close()
# processes.join()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment