From 769d4b116c04faf4af0c801f06a1b39b7c560df5 Mon Sep 17 00:00:00 2001 From: djib Date: Wed, 23 Oct 2019 20:58:15 +0200 Subject: [PATCH] =?UTF-8?q?Format=20code=20to=20PEP8=C2=A0standards?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- PDMameUpdate.py | 80 ++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/PDMameUpdate.py b/PDMameUpdate.py index 4dc268b..23d9e0e 100755 --- a/PDMameUpdate.py +++ b/PDMameUpdate.py @@ -13,10 +13,10 @@ Basically what it does is: - remove the old torrent from Transmission, - rename the local directory, - add the new torrent - + Work in progress… - * TODO: implement some error handling - + * TODO: implement some error handling + Requirements: * Transmission for Bitorrent * A PleasureDome account @@ -50,9 +50,10 @@ from clutch.core import Client from collections import defaultdict from pprint import pformat + def open_config_file(): """Reads configuration from config.json file""" - + logging.info('Opening config file: config.json') with open('config.json') as config_file: config = json.load(config_file) @@ -61,14 +62,14 @@ def open_config_file(): def fetch_local_torrents(): """Fetches local torrents versions""" - + logging.info('Fetching current MAME versions') directories = os.listdir(config['mame-directory']) for directory in directories: match = re_mame_version.match(directory) if match: torrents[match.group(2)]['local-version'] = int(match.group(1)) - torrents[match.group(2)]['local-name'] = directory + torrents[match.group(2)]['local-name'] = directory logging.debug('Found the local torrent versions: %s', pformat(torrents)) @@ -81,8 +82,8 @@ def fetch_remote_terrents(): match = re_mame_version.match(post.title) if match: torrents[match.group(2)]['remote-version'] = int(match.group(1)) - torrents[match.group(2)]['remote-link'] = post.link - torrents[match.group(2)]['remote-name'] = post.title + torrents[match.group(2)]['remote-link'] = post.link + torrents[match.group(2)]['remote-name'] = post.title logging.debug('Found the remote torrent versions: %s', pformat(torrents)) @@ -90,16 +91,21 @@ def filter_updatable_torrents(): """Checks if newer versions are available and prompt for update""" for torrent, data in list(torrents.items()): - keys_to_check = {'local-version','remote-version','transmission-id'} - if not ( keys_to_check.issubset(data.keys()) - and data['local-version'] < data['remote-version']): - del torrents[torrent] - logging.info('The following torrents can be updated: %s', pformat(torrents)) + keys_to_check = {'local-version', 'remote-version', 'transmission-id'} + if not ( + keys_to_check.issubset(data.keys()) + and + data['local-version'] < data['remote-version'] + ): + del torrents[torrent] + logging.info( + 'The following torrents can be updated: %s', pformat(torrents) + ) def prompt_for_update(): """Ask for user confirmation before updating""" - + if len(torrents) > 0: for torrent, data in torrents.items(): print('Torrent {}: {} -> {}'.format( @@ -125,8 +131,8 @@ def get_cookies_from_pleasuredome(): logging.info('Logging in PleasureDome') data = { - 'uid':config['pleasuredome-user'], - 'pwd':config['pleasuredome-password'] + 'uid': config['pleasuredome-user'], + 'pwd': config['pleasuredome-password'] } r = requests.post('http://www.pleasuredome.org.uk/login2.php', data=data) if r.status_code == 200: @@ -139,44 +145,50 @@ def get_cookies_from_pleasuredome(): r.status_code ) exit(1) - return {k: r.cookies[k] for k in ('uid','pass')} + return {k: r.cookies[k] for k in ('uid', 'pass')} def connect_to_transmission(): """Connects to Transmission and return a Client object""" - + logging.info('Connecting to Transmission Remote Control') return Client( username=config['transmission-user'], password=config['transmission-password'], port=config['transmission-port'] ) - + def fetch_transmission_torrents(): """Gets the torrents id from Transmission""" - + logging.info('Listing Transmission torrents') for torrent in client.list().values(): match = re_mame_version.match(torrent['name']) if match: torrents[match.group(2)]['transmission-id'] = torrent['id'] logging.debug('Found the Transmission torrent ids: %s', pformat(torrents)) - - + + def update_torrents(): """ Updates torrents: * remove it from Transmission, - * rename the local directory, + * rename the local directory, * and add the new torrent """ logging.info('Updating torrents') for torrent in torrents.values(): - old_name = os.path.join(config['mame-directory'], torrent['local-name']) - new_name = os.path.join(config['mame-directory'], torrent['remote-name']) - - client.torrent.remove(torrent['transmission-id']); + old_name = os.path.join( + config['mame-directory'], + torrent['local-name'] + ) + new_name = os.path.join( + config['mame-directory'], + torrent['remote-name'] + ) + + client.torrent.remove(torrent['transmission-id']) os.rename(old_name, new_name) client.torrent.add( filename=torrent['remote-link'], @@ -184,8 +196,8 @@ def update_torrents(): cookies=cookies, paused=False ) - - + + if __name__ == '__main__': logging.basicConfig( level=logging.INFO, @@ -194,14 +206,14 @@ if __name__ == '__main__': print('PDMameUpdate is about to start') # Useful if you run this script when your machine boots - for i in range(5,0,-1): + for i in range(5, 0, -1): print('{}\r'.format(i), end=''), time.sleep(1) - - re_mame_version = re.compile('MAME 0.(\d+) (.*)') + + re_mame_version = re.compile(r'MAME 0.(\d+) (.*)') config = open_config_file() torrents = defaultdict(dict) - + client = connect_to_transmission() cookies = get_cookies_from_pleasuredome() @@ -210,4 +222,4 @@ if __name__ == '__main__': fetch_transmission_torrents() filter_updatable_torrents() prompt_for_update() - update_torrents() \ No newline at end of file + update_torrents()