Compare commits

...

3 Commits

1 changed files with 187 additions and 170 deletions

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3
import feedparser
import subprocess
import click
import re
import json
@ -9,11 +8,14 @@ import hashlib
import sys
import os
import transmissionrpc
from operator import itemgetter
import requests
import base64
def printe(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
class Resolution:
# In decending order of quality, since indexes are used to tell whats "better"
names = [
@ -28,6 +30,7 @@ class Resolution:
('240p', 'QVGA'),
('Unknown', 'None', None)
]
def __init__(self, _name):
self.name = _name
for family in self.names:
@ -45,16 +48,21 @@ class Resolution:
if not isinstance(other, Resolution):
raise TypeError
return self.names.index(self.family) > other.names.index(other.family)
def __eq__(self, other):
if not isinstance(other, Resolution):
raise TypeError
return self.names.index(self.family) == other.names.index(other.family)
def __ne__(self, other):
return not self.__eq__(other)
def __gt__(self, other):
return not self.__lt__(other)
def __le__(self, other):
return self.__lt__(other) or self.__eq__(other)
def __ge__(self, other):
return self.__gt__(other) or self.__eq__(other)
@ -84,6 +92,7 @@ class Show:
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
@ -109,7 +118,6 @@ class Torrent:
return self.id == other.id
@click.command()
@click.option('--config', 'config_path', type=click.Path(readable=True, dir_okay=False), default='config.json', help="Config file in JSON format.", )
@click.option('--seen-file', 'seen_path', type=click.Path(writable=True, dir_okay=False), default='seen.txt', help="File to store seen torrents in. Pruning is acceptable if pruned torrent id's will not appear in the RSS feeds.")
@ -178,6 +186,8 @@ def main(config_path='config.json', seen_path='seen.txt', dry_run=False, no_seen
timeout=config['rpc'].get('timeout')
)
default_download_dir = rpc.get_session().download_dir
else:
rpc = None
# Choose the best item
for show, torrents in filtered.items():
item_config = show.getConfig(config)
@ -203,7 +213,13 @@ def main(config_path='config.json', seen_path='seen.txt', dry_run=False, no_seen
destination = os.path.join(default_download_dir, destination)
if not os.path.exists(destination):
os.makedirs(destination)
t = rpc.add_torrent(best_entry.link, download_dir=destination or default_download_dir)
if best_entry.link.startswith('http'):
r = requests.get(best_entry.link)
r.raise_for_status()
rpc.add_torrent(base64.b64encode(r.content).decode('utf-8'), download_dir=destination or default_download_dir)
else:
rpc.add_torrent(best_entry.link, download_dir=destination or default_download_dir)
else:
printe("No torrents for {show} pass quality restrictions".format(show=show.name))
@ -212,6 +228,7 @@ def main(config_path='config.json', seen_path='seen.txt', dry_run=False, no_seen
with open(seen_path, 'w') as seen_file:
seen_file.write('\n'.join(seen))
# Usual python stuff
if __name__ == '__main__':
main()