From 6bea77a509a0e7a586ec6936d30b4edc91c77c16 Mon Sep 17 00:00:00 2001 From: Piotr Staroszczyk Date: Wed, 26 Jul 2017 14:24:52 +0200 Subject: [PATCH] fstrings #1 --- pdeo/core.py | 18 +++++++++++++----- pdeo/databases/sql.py | 2 +- pdeo/databases/trakt.py | 12 ++++++------ pdeo/providers/__init__.py | 8 +++++--- pdeo/providers/polishsource.py | 1 - setup.py | 16 ++++++++-------- 6 files changed, 33 insertions(+), 24 deletions(-) diff --git a/pdeo/core.py b/pdeo/core.py index 8fc2e74..91890cd 100644 --- a/pdeo/core.py +++ b/pdeo/core.py @@ -58,17 +58,25 @@ def get(self, destination='.', quality='1080p', min_size=0): # TODO?: ability to search by imdb_id (moviedatabse request first to get metadata) https://www.themoviedb.org/documentation/api # TODO?: ability to serach without year (might be necessary for old rips but should we care?) movies = self.db.load(category='movies') - self.logger.debug('MOVIES: %s' % movies) + self.logger.debug(f'MOVIES: {movies}') for movie in movies: torrent = self.provider.search(title=movie['title'], year=movie['year'], imdb=movie['imdb'], quality=quality, min_size=min_size) if torrent and torrent['score'] > 0: # TODO: i don't like this if - filepath = '%s/%s.torrent' % (destination, torrent['name']) + filepath = f'{destination}/{torrent["name"]}.torrent' open(filepath, 'wb').write(torrent['torrent']) # with? - print('INFO: torrent downloaded (%s).' % torrent['name']) + print(f'INFO: torrent downloaded ({torrent["name"]}).') else: - print('INFO: torrent not found: %s' % movie['title']) # DEBUG + print(f'INFO: torrent not found: {movie["title"]}') # DEBUG pass # torrent not found - input('next?') # DEBUG + # input('next?') # DEBUG # tvshows = self.db.load(category='') + # def getShows(self, destination='.', quality='1080p', min_size=0): + # # TODO: merge into get + # shows = self.db.loadShows() + # for show in shows: + # for season in shows['seasons']: + # for episode in season['episodes']: + # torrent = self.provider.search( + # TODO: logger like in fut diff --git a/pdeo/databases/sql.py b/pdeo/databases/sql.py index c0034ab..d80ff08 100644 --- a/pdeo/databases/sql.py +++ b/pdeo/databases/sql.py @@ -37,7 +37,7 @@ def add(self, tmdb_id): """Add movie to database.""" # TODO: check if not a duplicate try: - self.db.execute('INSERT INTO pdeo (tmdb_id) VALUES (%s)' % tmdb_id) + self.db.execute(f'INSERT INTO pdeo (tmdb_id) VALUES ({tmdb_id})') except sqlite3.IntegrityError: # duplicate return False return True diff --git a/pdeo/databases/trakt.py b/pdeo/databases/trakt.py index d99e1ca..050a44e 100644 --- a/pdeo/databases/trakt.py +++ b/pdeo/databases/trakt.py @@ -43,7 +43,7 @@ def __init__(self): def __headers(self): return { # 'Content-Type': 'application/json', # requests manages this - 'Authorization': 'Bearer %s' % self.config.trakt['token'], + 'Authorization': f'Bearer {self.config.trakt["token"]}', 'trakt-api-version': '2', 'trakt-api-key': client_id } @@ -77,8 +77,8 @@ def __authenticate(self): # shouldn't this be private? data = {'client_id': client_id} rc = self.r.post('https://api.trakt.tv/oauth/device/code', data=data).json() # TODO: automatically open browser link - print('1. Go to the following link: %s' % rc['verification_url']) - print('2. Enter user code: %s' % rc['user_code']) + print(f'1. Go to the following link: {rc["verification_url"]}') + print(f'2. Enter user code: {rc["user_code"]}') input('done?') # TODO: async? check instead of asking user # TODO: daemon mode without any interupt (error instead) self.__getToken(rc['device_code']) # TODO?: raise error if false @@ -88,7 +88,7 @@ def __tokenRefresh(self): # shouldn't this be private? def loadCollection(self, category='movies'): """Loads collection, returns list of movies.""" - rc = self.r.get('https://api.trakt.tv/sync/collection/%s' % category).json() + rc = self.r.get(f'https://api.trakt.tv/sync/collection/{category}').json() return [m[category[:-1]] for m in rc] # TODO?: parse data (could be usefull to unify with tvshows) def load(self, category='movies'): @@ -97,7 +97,7 @@ def load(self, category='movies'): # TODO: tvshows (get imdb/tmdb id from show, nobody cares to attach episode id) collection = self.loadCollection(category) movies = [] - rc = self.r.get('https://api.trakt.tv/sync/watchlist/%s' % category).json() + rc = self.r.get(f'https://api.trakt.tv/sync/watchlist/{category}').json() for m in rc: if m[m['type']] not in collection: movies.append({ @@ -152,7 +152,7 @@ def loadShows(self): 'tmdb': show['show']['ids']['tmdb'], 'tvrage': show['show']['ids']['tvrage'], 'seasons': {}} - rc2 = self.r.get('https://api.trakt.tv/shows/%s/progress/watched' % data['slug']).json() # use trakt instead of slug + rc2 = self.r.get(f'https://api.trakt.tv/shows/{data["slug"]}/progress/watched').json() # use trakt instead of slug for season in rc2['seasons']: data['seasons'][season['number']] = {'episodes': []} for episode in season['episodes']: diff --git a/pdeo/providers/__init__.py b/pdeo/providers/__init__.py index c49b61b..80f167d 100644 --- a/pdeo/providers/__init__.py +++ b/pdeo/providers/__init__.py @@ -56,14 +56,15 @@ def key(torrents): return (torrents['score'], torrents['size'], torrents['seeders'] + torrents['leechers']) - print(sorted(torrents, key=key, reverse=True)) # DEBUG - return sorted(torrents, key=key, reverse=True) + s = sorted(torrents, key=key, reverse=True) + print(s) # DEBUG + return s def magnetToTorrent(self, magnet): """'Converts' magnet to torrent file. This method probably won't work with private trackers.""" # TODO: validate hash = re.search('urn:btih:(.+?)&', magnet).group(1) - torrent_file = self.r.get('https://itorrents.org/torrent/%s.torrent' % hash).content # TODO?: don't use the same session + torrent_file = self.r.get(f'https://itorrents.org/torrent/{hash}.torrent').content # TODO?: don't use the same session return torrent_file def download(self, url=None, magnet=None): @@ -76,6 +77,7 @@ def download(self, url=None, magnet=None): def search(self, title, year, imdb, quality, min_size): """Search the one and only torrent. Return torrent file.""" + # TODO?: remove ' and other special signs before searching torrents = self.searchAll(title=title, year=year, imdb=imdb, quality=quality, min_size=min_size) if torrents: torrent = self.__sort(torrents)[0] diff --git a/pdeo/providers/polishsource.py b/pdeo/providers/polishsource.py index b38488f..386410f 100644 --- a/pdeo/providers/polishsource.py +++ b/pdeo/providers/polishsource.py @@ -107,5 +107,4 @@ def searchAll(self, title, year, imdb, quality, min_size): # imdb tmdb 'score': score, 'imdb': imdb_id, 'url': url}) # TODO: scheme in BaseProvider - print(torrents) return torrents diff --git a/setup.py b/setup.py index 92fc88d..8b0739c 100644 --- a/setup.py +++ b/setup.py @@ -15,9 +15,9 @@ packages = [ __title__, - '%s.databases' % __title__, - '%s.providers' % __title__, - # '%s.modules' % __title__, + f'{__title__}.databases', + f'{__title__}.providers', + # f'{__title__}.modules', ] with open('requirements.txt') as f: @@ -30,15 +30,15 @@ setup( name=__title__, version=__version__, - description='%s is a very simple alternative for radarr/couchpotato - automatically downloading movies from torrent.' % __title__, + description=f'{__title__} is a very simple alternative for radarr/couchpotato - automatically downloading movies from torrent.', long_description=long_desc, author=__author__, author_email=__author_email__, - url='https://github.com/oczkers/%s' % __title__, - download_url='https://github.com/oczkers/%s/releases' % __title__, - bugtrack_url='https://github.com/oczkers/%s/issues' % __title__, + url=f'https://github.com/oczkers/{__title__}', + download_url=f'https://github.com/oczkers/{__title__}/releases', + bugtrack_url=f'https://github.com/oczkers/{__title__}/issues', platforms='any', - keywords='%s download torrent movies' % __title__, + keywords=f'{__title__} download torrent movies', packages=packages, package_data={'': ['LICENSE']}, package_dir={__title__: __title__},