Guest User

Untitled

a guest
Jan 30th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. # Get Put.io Key with the AuthHelper and fill in PCLOUD/PUTIO variables
  2. # putio_pcloud_sync.py list
  3. # putio_pcloud_sync.py filter 'START OF ONE OF THE list OUTPUTS'
  4. # putio_pcloud_sync.py sync 'START OF ONE OF THE list OUTPUTS' 'PATH IN PCLOUD'
  5.  
  6. import sys
  7. import asyncio
  8. import putiopy
  9. from pcloud import PyCloud
  10. from sh import mv, rm, aria2c
  11.  
  12. TMPDIR = '/home/gordin/tmp'
  13. #helper = putiopy.AuthHelper(ID, KEY, 'xxx.xxx', type='token')
  14.  
  15. PCLOUD_USER = XXX
  16. PCLOUD_PASS = XXX
  17. PUTIO_KEY = XXX
  18.  
  19. def filename(file):
  20. return '{}/{}'.format(TMPDIR, file.name)
  21.  
  22. def process_output(line):
  23. print(line)
  24.  
  25. class PutIOpCloudSyncer(object):
  26. def __init__(self):
  27. self.pcloud = PyCloud(PCLOUD_USER, PCLOUD_PASS)
  28. self.putio = putiopy.Client(PUTIO_KEY)
  29. self.download_list = []
  30. self.upload_list = []
  31. self.files_left = 0
  32. self.destination = None
  33.  
  34. def download(self, file):
  35. print('Download of {} started'.format(file.name))
  36. aria2c('-d', TMPDIR, '-o', file.name, '--continue=true', '-x3', file.get_download_link())
  37. print('Download finished')
  38.  
  39. def upload(self, file):
  40. print('Starting upload of {}'.format(file.name))
  41. self.pcloud.uploadfile(path=self.destination, files=['{}/{}'.format(TMPDIR, file.name)])
  42. print('Finished upload')
  43.  
  44.  
  45. def cleanup(self, file):
  46. print('Removing local copy of {}'.format(file.name))
  47. rm(filename(file))
  48. print('Removed successfully')
  49.  
  50.  
  51. async def process_folder(self, folder):
  52. for file in folder.dir():
  53. self.enqueue_file(file)
  54. print("Files to sync: {}".format(self.files_left))
  55. await asyncio.gather(
  56. self.file_uploader(),
  57. self.file_downloader(),
  58. )
  59.  
  60.  
  61. def enqueue_file(self, file):
  62. self.download_list.append(file)
  63. self.files_left += 1
  64.  
  65.  
  66. def list_paths(self):
  67. file_list = self.putio.File.list()
  68. folders = [x for x in file_list if x.name in ('Serien', 'Filme')]
  69. for path in [folder for fs in folders for folder in fs.dir()]:
  70. print(path.name, path.id)
  71. def filter_paths(self, name): 19:13:50
  72. file_list = self.putio.File.list()
  73. folders = [x for x in file_list if x.name in ('Serien', 'Filme')]
  74. files = [file for folder in folders for file in folder.dir()]
  75. file = list(filter(lambda x: x.name.startswith(name), files))
  76. if file:
  77. if len(file) > 1:
  78. print("More than 1 possible folder", file)
  79. sys.exit(1)
  80. return file[0]
  81. print('No Matching file')
  82. sys.exit(1)
  83.  
  84.  
  85. async def file_downloader(self):
  86. print("File Downloader started")
  87. while self.download_list:
  88. file = self.download_list.pop()
  89. self.download(file)
  90. self.upload_list.append(file)
  91. print("File Downloader stopped")
  92.  
  93.  
  94. async def file_uploader(self):
  95. print("File Uploader started")
  96. while self.files_left:
  97. print('Files left to upload: {}'.format(self.files_left))
  98. print('Files to upload in queue: {}'.format(len(self.upload_list)))
  99. while not self.upload_list:
  100. print('Waiting for something to upload...')
  101. await asyncio.sleep(10)
  102. while self.upload_list:
  103. file = self.upload_list.pop()
  104. self.upload(file)
  105. self.cleanup(file)
  106. self.files_left -= 1
  107. print("File Uploader stopped")
  108.  
  109. def sync(self):
  110. if sys.argv[1] == 'list':
  111. self.list_paths()
  112. elif sys.argv[1] == 'filter':
  113. path = self.filter_paths(sys.argv[2])
  114. print('Selected Path: {}'.format(path.name))
  115. elif sys.argv[1] == 'sync':
  116. path = self.filter_paths(sys.argv[2])
  117. print('Selected Path: {}'.format(path.name))
  118. self.destination = sys.argv[3]
  119. asyncio.run(self.process_folder(path))
  120. print("Started downloader and uploader")
  121.  
  122. if __name__ == '__main__':
  123. syncer = PutIOpCloudSyncer() 19:14:03
  124. syncer.sync()
Add Comment
Please, Sign In to add comment