Refactor code to improve readability

This commit is contained in:
2020-09-30 15:57:50 +02:00
parent d0e0aa9888
commit 0ba60e741c

View File

@ -104,10 +104,10 @@ def fetch_local_torrents():
directories = os.listdir(config['mame-directory'])
for directory in directories:
for regexp in regexps:
match = regexp.match(directory)
match = regexp.search(directory)
if match:
version = match.group('version')
name = match.group(0).replace(version, '#')
version = match.group(0)
name = regexp.sub('#', directory)
torrents[name]['local-version'] = version
torrents[name]['local-name'] = directory
logging.debug('Found the local torrent versions: %s', pformat(torrents))
@ -122,8 +122,8 @@ def fetch_remote_torrents():
for regexp in regexps:
match = regexp.match(post.title)
if match:
matched_version = match.group('version')
matched_torrent = torrents[match.group(0).replace(matched_version, '#')]
matched_version = match.group(0)
matched_torrent = regexp.sub('#', post.title)
if matched_version > matched_torrent.get('remote-version', ''):
matched_torrent['remote-version'] = matched_version
matched_torrent['remote-link'] = post.link
@ -240,9 +240,10 @@ def fetch_transmission_torrents():
logging.info('Listing Transmission torrents')
for torrent in client.list().values():
for regexp in regexps:
match = regexp.match(torrent['name'])
match = regexp.search(torrent['name'])
if match:
torrents[match.group(0).replace(match.group('version'), '#')]['transmission-id'] = torrent['id']
name = regexp.sub('#', torrent['name'])
torrents[name]['transmission-id'] = torrent['id']
logging.debug('Found the Transmission torrent ids: %s', pformat(torrents))
@ -328,8 +329,9 @@ if __name__ == '__main__':
time.sleep(1)
regexps = [
re.compile(r'(?:HB)?MAME (?P<version>[\d.]+) .*'),
re.compile(r'No-Intro \((?P<version>[\d-]+)\) .*')
re.compile(r'(?<=MAME )[\d.]+'),
re.compile(r'(?<=HBMAME )[\d.]+'),
re.compile(r'(?<=No-Intro \()[\d-]+')
]
config = open_config_file()
torrents = defaultdict(dict)