Last active
January 29, 2019 20:57
-
-
Save Gordin/d08bae9f5680086bad7ac7d5be01d56a to your computer and use it in GitHub Desktop.
Script to Sync files from put.io to pcloud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get Put.io Key with the AuthHelper and fill in PCLOUD/PUTIO variables | |
# putio_pcloud_sync.py list | |
# putio_pcloud_sync.py filter 'START OF ONE OF THE list OUTPUTS' | |
# putio_pcloud_sync.py sync 'START OF ONE OF THE list OUTPUTS' 'PATH IN PCLOUD' | |
import sys | |
import asyncio | |
import putiopy | |
from pcloud import PyCloud | |
from sh import mv, rm, aria2c | |
TMPDIR = '/home/gordin/tmp' | |
#helper = putiopy.AuthHelper(ID, KEY, 'xxx.xxx', type='token') | |
PCLOUD_USER = XXX | |
PCLOUD_PASS = XXX | |
PUTIO_KEY = XXX | |
def filename(file): | |
return '{}/{}'.format(TMPDIR, file.name) | |
def process_output(line): | |
print(line) | |
class PutIOpCloudSyncer(object): | |
def __init__(self): | |
self.pcloud = PyCloud(PCLOUD_USER, PCLOUD_PASS) | |
self.putio = putiopy.Client(PUTIO_KEY) | |
self.download_list = [] | |
self.upload_list = [] | |
self.files_left = 0 | |
self.destination = None | |
def download(self, file): | |
print('Download of {} started'.format(file.name)) | |
aria2c('-d', TMPDIR, '-o', file.name, '--continue=true', '-x3', file.get_download_link()) | |
print('Download finished') | |
def upload(self, file): | |
print('Starting upload of {}'.format(file.name)) | |
self.pcloud.uploadfile(path=self.destination, files=['{}/{}'.format(TMPDIR, file.name)]) | |
print('Finished upload') | |
def cleanup(self, file): | |
print('Removing local copy of {}'.format(file.name)) | |
rm(filename(file)) | |
print('Removed successfully') | |
async def process_folder(self, folder): | |
for file in folder.dir(): | |
self.enqueue_file(file) | |
print("Files to sync: {}".format(self.files_left)) | |
await asyncio.gather( | |
self.file_uploader(), | |
self.file_downloader(), | |
) | |
def enqueue_file(self, file): | |
self.download_list.append(file) | |
self.files_left += 1 | |
def list_paths(self): | |
file_list = self.putio.File.list() | |
folders = [x for x in file_list if x.name in ('Serien', 'Filme')] | |
for path in [folder for fs in folders for folder in fs.dir()]: | |
print(path.name, path.id) | |
def filter_paths(self, name): 19:13:50 | |
file_list = self.putio.File.list() | |
folders = [x for x in file_list if x.name in ('Serien', 'Filme')] | |
files = [file for folder in folders for file in folder.dir()] | |
file = list(filter(lambda x: x.name.startswith(name), files)) | |
if file: | |
if len(file) > 1: | |
print("More than 1 possible folder", file) | |
sys.exit(1) | |
return file[0] | |
print('No Matching file') | |
sys.exit(1) | |
async def file_downloader(self): | |
print("File Downloader started") | |
while self.download_list: | |
file = self.download_list.pop() | |
self.download(file) | |
self.upload_list.append(file) | |
print("File Downloader stopped") | |
async def file_uploader(self): | |
print("File Uploader started") | |
while self.files_left: | |
print('Files left to upload: {}'.format(self.files_left)) | |
print('Files to upload in queue: {}'.format(len(self.upload_list))) | |
while not self.upload_list: | |
print('Waiting for something to upload...') | |
await asyncio.sleep(10) | |
while self.upload_list: | |
file = self.upload_list.pop() | |
self.upload(file) | |
self.cleanup(file) | |
self.files_left -= 1 | |
print("File Uploader stopped") | |
def sync(self): | |
if sys.argv[1] == 'list': | |
self.list_paths() | |
elif sys.argv[1] == 'filter': | |
path = self.filter_paths(sys.argv[2]) | |
print('Selected Path: {}'.format(path.name)) | |
elif sys.argv[1] == 'sync': | |
path = self.filter_paths(sys.argv[2]) | |
print('Selected Path: {}'.format(path.name)) | |
self.destination = sys.argv[3] | |
asyncio.run(self.process_folder(path)) | |
print("Started downloader and uploader") | |
if __name__ == '__main__': | |
syncer = PutIOpCloudSyncer() 19:14:03 | |
syncer.sync() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment