From cd50bd9b54f467303091f466072c0972c813a220 Mon Sep 17 00:00:00 2001 From: djib Date: Sat, 5 Oct 2019 16:54:08 +0200 Subject: [PATCH] Refactor code: separate fetching and printing --- FreeboxMoviePlanner.py | 102 +++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/FreeboxMoviePlanner.py b/FreeboxMoviePlanner.py index 2739c86..b74b16e 100755 --- a/FreeboxMoviePlanner.py +++ b/FreeboxMoviePlanner.py @@ -15,80 +15,92 @@ import textwrap from bs4 import BeautifulSoup from collections import deque + class FreeboxMoviePlanner: TV_GUIDE_URL = 'https://www.programme-television.org/{}?bouquet=tnt' - + def __init__(self): logging.info('Opening config file: config.json') with open('config.json') as config_file: self.config = json.load(config_file) tmdbsimple.API_KEY = self.config['tmdb-api'] - @staticmethod def _tag_is_film(tag): return ( tag.has_attr('data-nature') - and - tag['data-nature']=='films-telefilms' + and + tag['data-nature'] == 'films-telefilms' ) - + + @staticmethod + def _printMovie(movie): + print('{} - {} ({})'.format( + movie['title'], + movie['genre'], + movie['channel'] + )) + print(' TMDB: {} - {}\n {}'.format( + movie['rating'], + movie['original_title'], + movie['overview'], + )) + + def printAllMovies(self, movies): + for day, movies in movies.items(): + print('=== {}'.format(day.title())) + for movie in movies: + FreeboxMoviePlanner._printMovie(movie) def getAllMovies(self): days = deque(['lundi', 'mardi', 'mercredi', - 'jeudi', 'vendredi', 'samedi', 'dimanche']) + 'jeudi', 'vendredi', 'samedi', 'dimanche']) offset = datetime.datetime.today().weekday() days.rotate(-1-offset) days.appendleft('') - + movies = {} for day in days: - print('=== {}'.format(day.title())) - self.getMovies(day) + movies[day] = self.getMovies(day) + logging.info('Found the following movies: {}'.format(movies)) + return movies - def getMovies(self, day=''): logging.info('Connecting to {}'.format(self.TV_GUIDE_URL)) r = requests.get(self.TV_GUIDE_URL.format(day)) r.raise_for_status() html = BeautifulSoup(r.text, 'html.parser') + movies = [] for channel in html.select('.bloc_cnt'): if len(channel.select('em')): - for movie in channel.find_all(FreeboxMoviePlanner._tag_is_film): + for movie in channel.find_all( + FreeboxMoviePlanner._tag_is_film): movie_title = movie.select('.texte_titre a')[0]['title'] - - movie_info = '{} - {} ({})'.format( - movie_title, - movie.select('.texte_cat a')[0].string, - channel.select('em')[0].string.replace('Programme ','') - ) - logging.info('Found movie: {}'.format(movie_info)) - + + thismovie = {} + thismovie['title'] = movie_title + thismovie['genre'] = movie.select('.texte_cat a')[0].string + thismovie['channel'] = channel.select('em')[0].string.replace( 'Programme ', '') + logging.info('Found movie: {}'.format(thismovie)) + tmdb_details = self._getMovieRating(movie_title) - if tmdb_details: - if( - float(tmdb_details['vote_average']) - < self.config['minimum-rating'] - ): - logging.warning('Bad rating ({}), skipping {}'.format( - tmdb_details['vote_average'], - movie_title - )) - else: - print(movie_info) - print(' TMDB: {} - {}\n {}'.format( - tmdb_details['vote_average'], - tmdb_details['original_title'], - '\n '.join(textwrap.wrap( - tmdb_details['overview'],75) - ) - )) - print("---") + if not tmdb_details: + logging.warning('No TMDB match for {}'.format( movie_title)) + continue + + thismovie['rating'] = tmdb_details['vote_average'] + thismovie['original_title'] = tmdb_details['original_title'] + thismovie['overview'] = '\n '.join(textwrap.wrap(tmdb_details['overview'], 75)) + if( + float(tmdb_details['vote_average']) + < self.config['minimum-rating'] + ): + logging.warning( + 'Bad rating ({}), skipping {}'.format( + tmdb_details['vote_average'], movie_title)) else: - logging.warning('No TMDB match for {}'.format( - movie_title - )) + movies.append(thismovie) + return movies - def _getMovieRating(self, movie): logging.info("Searching for '{}' on TMDB".format(movie)) search = tmdbsimple.Search() @@ -99,11 +111,11 @@ class FreeboxMoviePlanner: else: return [] - + if __name__ == '__main__': logging.basicConfig( - level=logging.ERROR, + level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s' ) fmp = FreeboxMoviePlanner() - fmp.getAllMovies() + fmp.printAllMovies(fmp.getAllMovies())