Guest User

Untitled

a guest
Mar 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. '''
  4. requires python-pydrive
  5. '''
  6.  
  7. import multiprocessing
  8. import os
  9. from pydrive.drive import GoogleDrive
  10. from pydrive.auth import GoogleAuth
  11. import ntpath
  12.  
  13. dir_base = os.path.join(os.path.expanduser('~'), "Downloads/test_up")
  14.  
  15.  
  16. def get_f_name(addr):
  17. """
  18. splits address into head and tail
  19. Args:
  20. addr: path to the file
  21. Returns:
  22. file name
  23. """
  24. head, tail = ntpath.split(addr)
  25. return tail or ntpath.basename(head) # return tail when file, otherwise other one for folder
  26.  
  27.  
  28. def f_create(drive, addr):
  29. """
  30. creates and uploads file to drive
  31. Args:
  32. drive: GoogleDrive class from PyDrive module
  33. addr: path to the file to be uploaded
  34. Returns:
  35. True: upload successful
  36. False: upload failed
  37. """
  38. # check whether address is right or not
  39. if not os.path.exists(addr):
  40. print("Specified file/folder doesn't exist, check the address!")
  41. return False
  42.  
  43. # print progress
  44. print("uploading file " + addr)
  45.  
  46. up_file = drive.CreateFile()
  47.  
  48. up_file.SetContentFile(addr)
  49. up_file['title'] = get_f_name(addr) # sets file title to original
  50. up_file.Upload()
  51.  
  52. return True
  53.  
  54.  
  55. def main():
  56.  
  57. g_auth = GoogleAuth()
  58. g_auth.LocalWebserverAuth()
  59. drive = GoogleDrive(g_auth)
  60.  
  61. files = ["c8.ts", "tracesports.ts", "cinemax.ts", "c8 (copy).ts",
  62. "cinemax (another copy).ts", "cinemax (copy).ts", "tracesports (another copy).ts", "tracesports (copy).ts"]
  63.  
  64. # uncomment commented lines below and comment line 69 to use multiprocessing
  65.  
  66. # processes = multiprocessing.Pool(processes=multiprocessing.cpu_count())
  67. for f in files:
  68. address = os.path.join(dir_base, str(f))
  69. # processes.apply_async(f_create, args=(drive, address,))
  70. f_create(drive, address)
  71.  
  72. # processes.close()
  73. # processes.join()
  74.  
  75.  
  76. if __name__ == '__main__':
  77.  
  78. main()
Add Comment
Please, Sign In to add comment