Make a show class

Use this instead of the strange torrent class behaviour
This commit is contained in:
Derek Schmidt 2018-06-08 17:51:25 +00:00
parent ae283723ad
commit c4bf3b03a0

View file

@ -46,18 +46,22 @@ class Resolution:
def __ge__(self, other):
return self.__gt__(other) or self.__eq__(other)
class Torrent:
def __init__(self, name, link, id=None, episode=None, quality=None, encoding=None):
class Show:
def __init__(self, name, episode=None):
self.name = name
self.link = link
self.id = id if id else hashlib.sha256(link).hexdigest()
self.episode = episode
self.quality = Resolution(quality)
self.encoding = encoding
self.episode = int(episode)
self.key = '{name} - {episode}'.format(name=self.name, episode=self.episode if self.episode else 'None')
def getConfig(self, config):
try:
show_config = next(want for want in config['wants'] if want['name'] == self.name)
except StopIteration:
show_config = None
return show_config
def __repr__(self):
return '<Torrent {id} for {name} - {episode}>'.format(id=self.id, name=self.name, episode=self.episode)
return '<Show {name} - {episode}>'.format(name=self.name, episode=self.episode)
def __hash__(self):
return hash(self.key)
@ -65,6 +69,27 @@ class Torrent:
def __eq__(self, other):
return self.key == other.key
class Torrent:
def __init__(self, name, link, id=None, episode=None, quality=None, encoding=None):
self.name = name
self.link = link
self.id = id if id else hashlib.sha256(link).hexdigest()
self.episode = int(episode)
self.quality = Resolution(quality)
self.encoding = encoding
def isShow(self, show):
return self.name == show.name and (show.episode is None or self.episode == show.episode)
def __repr__(self):
return '<Torrent {id} for {name} - {episode}>'.format(id=self.id, name=self.name, episode=self.episode)
def __hash__(self):
return hash(self.id)
def __eq__(self, other):
return self.id == other.id
@click.command()
@ -105,26 +130,31 @@ def main(config_path='config.json', seen_path='seen.txt'):
quality=parsed_title.get('quality'),
encoding=parsed_title.get('encoding')
)
show = Show(torrent.name, episode=torrent.episode)
# Check if its something we're interested in and add it to a dict
if torrent.name in wants and torrent.id not in seen:
if filtered.get(torrent) is None:
filtered[torrent] = [torrent]
if filtered.get(show) is None:
filtered[show] = [torrent]
else:
filtered[torrent].append(torrent)
filtered[show].append(torrent)
# Record that we have seen it
# FIXME: should be derived from the magnet itself as not all providers are nice like this
seen.append(torrent.id)
# Choose the best item
for torrent_key, entries in filtered.items():
item_config = next(want for want in config['wants'] if want['name'] == torrent_key.name)
entries = [entry for entry in entries if torrent.quality >= Resolution(item_config.get('min-quality', '240p'))] # bad hardcode of lowest res
for show, torrents in filtered.items():
item_config = show.getConfig(config)
# Filter
if item_config.get('target-quality'):
torrents = [torrent for torrent in torrents if torrent.quality == Resolution(item_config['target-quality'])]
elif item_config.get('min-quality'):
torrents = [torrent for torrent in torrents if torrent.quality >= Resolution(item_config['min-quality'])]
if entries:
best_entry = sorted(entries, key=itemgetter('quality'))[0]
if len(torrents) > 0:
best_entry = sorted(torrents, key=lambda t: t.quality)[0]
# Output the magnet / torrent link
# TODO: transmission-cli subprocess call here?
print(best_entry['link'])
print(best_entry.link)
# Save our records to a file for later use
with open(seen_path, 'w') as seen_file: