Format code to PEP8 standards

This commit is contained in:
2019-10-23 20:58:15 +02:00
parent 7b5196fb11
commit 769d4b116c

View File

@ -13,10 +13,10 @@ Basically what it does is:
- remove the old torrent from Transmission, - remove the old torrent from Transmission,
- rename the local directory, - rename the local directory,
- add the new torrent - add the new torrent
Work in progress… Work in progress…
* TODO: implement some error handling * TODO: implement some error handling
Requirements: Requirements:
* Transmission for Bitorrent * Transmission for Bitorrent
* A PleasureDome account * A PleasureDome account
@ -50,9 +50,10 @@ from clutch.core import Client
from collections import defaultdict from collections import defaultdict
from pprint import pformat from pprint import pformat
def open_config_file(): def open_config_file():
"""Reads configuration from config.json file""" """Reads configuration from config.json file"""
logging.info('Opening config file: config.json') logging.info('Opening config file: config.json')
with open('config.json') as config_file: with open('config.json') as config_file:
config = json.load(config_file) config = json.load(config_file)
@ -61,14 +62,14 @@ def open_config_file():
def fetch_local_torrents(): def fetch_local_torrents():
"""Fetches local torrents versions""" """Fetches local torrents versions"""
logging.info('Fetching current MAME versions') logging.info('Fetching current MAME versions')
directories = os.listdir(config['mame-directory']) directories = os.listdir(config['mame-directory'])
for directory in directories: for directory in directories:
match = re_mame_version.match(directory) match = re_mame_version.match(directory)
if match: if match:
torrents[match.group(2)]['local-version'] = int(match.group(1)) 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)) 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) match = re_mame_version.match(post.title)
if match: if match:
torrents[match.group(2)]['remote-version'] = int(match.group(1)) torrents[match.group(2)]['remote-version'] = int(match.group(1))
torrents[match.group(2)]['remote-link'] = post.link torrents[match.group(2)]['remote-link'] = post.link
torrents[match.group(2)]['remote-name'] = post.title torrents[match.group(2)]['remote-name'] = post.title
logging.debug('Found the remote torrent versions: %s', pformat(torrents)) 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""" """Checks if newer versions are available and prompt for update"""
for torrent, data in list(torrents.items()): for torrent, data in list(torrents.items()):
keys_to_check = {'local-version','remote-version','transmission-id'} keys_to_check = {'local-version', 'remote-version', 'transmission-id'}
if not ( keys_to_check.issubset(data.keys()) if not (
and data['local-version'] < data['remote-version']): keys_to_check.issubset(data.keys())
del torrents[torrent] and
logging.info('The following torrents can be updated: %s', pformat(torrents)) data['local-version'] < data['remote-version']
):
del torrents[torrent]
logging.info(
'The following torrents can be updated: %s', pformat(torrents)
)
def prompt_for_update(): def prompt_for_update():
"""Ask for user confirmation before updating""" """Ask for user confirmation before updating"""
if len(torrents) > 0: if len(torrents) > 0:
for torrent, data in torrents.items(): for torrent, data in torrents.items():
print('Torrent {}: {} -> {}'.format( print('Torrent {}: {} -> {}'.format(
@ -125,8 +131,8 @@ def get_cookies_from_pleasuredome():
logging.info('Logging in PleasureDome') logging.info('Logging in PleasureDome')
data = { data = {
'uid':config['pleasuredome-user'], 'uid': config['pleasuredome-user'],
'pwd':config['pleasuredome-password'] 'pwd': config['pleasuredome-password']
} }
r = requests.post('http://www.pleasuredome.org.uk/login2.php', data=data) r = requests.post('http://www.pleasuredome.org.uk/login2.php', data=data)
if r.status_code == 200: if r.status_code == 200:
@ -139,44 +145,50 @@ def get_cookies_from_pleasuredome():
r.status_code r.status_code
) )
exit(1) 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(): def connect_to_transmission():
"""Connects to Transmission and return a Client object""" """Connects to Transmission and return a Client object"""
logging.info('Connecting to Transmission Remote Control') logging.info('Connecting to Transmission Remote Control')
return Client( return Client(
username=config['transmission-user'], username=config['transmission-user'],
password=config['transmission-password'], password=config['transmission-password'],
port=config['transmission-port'] port=config['transmission-port']
) )
def fetch_transmission_torrents(): def fetch_transmission_torrents():
"""Gets the torrents id from Transmission""" """Gets the torrents id from Transmission"""
logging.info('Listing Transmission torrents') logging.info('Listing Transmission torrents')
for torrent in client.list().values(): for torrent in client.list().values():
match = re_mame_version.match(torrent['name']) match = re_mame_version.match(torrent['name'])
if match: if match:
torrents[match.group(2)]['transmission-id'] = torrent['id'] torrents[match.group(2)]['transmission-id'] = torrent['id']
logging.debug('Found the Transmission torrent ids: %s', pformat(torrents)) logging.debug('Found the Transmission torrent ids: %s', pformat(torrents))
def update_torrents(): def update_torrents():
""" """
Updates torrents: Updates torrents:
* remove it from Transmission, * remove it from Transmission,
* rename the local directory, * rename the local directory,
* and add the new torrent * and add the new torrent
""" """
logging.info('Updating torrents') logging.info('Updating torrents')
for torrent in torrents.values(): for torrent in torrents.values():
old_name = os.path.join(config['mame-directory'], torrent['local-name']) old_name = os.path.join(
new_name = os.path.join(config['mame-directory'], torrent['remote-name']) config['mame-directory'],
torrent['local-name']
client.torrent.remove(torrent['transmission-id']); )
new_name = os.path.join(
config['mame-directory'],
torrent['remote-name']
)
client.torrent.remove(torrent['transmission-id'])
os.rename(old_name, new_name) os.rename(old_name, new_name)
client.torrent.add( client.torrent.add(
filename=torrent['remote-link'], filename=torrent['remote-link'],
@ -184,8 +196,8 @@ def update_torrents():
cookies=cookies, cookies=cookies,
paused=False paused=False
) )
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
@ -194,14 +206,14 @@ if __name__ == '__main__':
print('PDMameUpdate is about to start') print('PDMameUpdate is about to start')
# Useful if you run this script when your machine boots # 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=''), print('{}\r'.format(i), end=''),
time.sleep(1) time.sleep(1)
re_mame_version = re.compile('MAME 0.(\d+) (.*)') re_mame_version = re.compile(r'MAME 0.(\d+) (.*)')
config = open_config_file() config = open_config_file()
torrents = defaultdict(dict) torrents = defaultdict(dict)
client = connect_to_transmission() client = connect_to_transmission()
cookies = get_cookies_from_pleasuredome() cookies = get_cookies_from_pleasuredome()
@ -210,4 +222,4 @@ if __name__ == '__main__':
fetch_transmission_torrents() fetch_transmission_torrents()
filter_updatable_torrents() filter_updatable_torrents()
prompt_for_update() prompt_for_update()
update_torrents() update_torrents()