Refactor code: separate fetching and printing

This commit is contained in:
2019-10-05 16:54:08 +02:00
parent ade622d0dd
commit cd50bd9b54

View File

@ -15,6 +15,7 @@ import textwrap
from bs4 import BeautifulSoup
from collections import deque
class FreeboxMoviePlanner:
TV_GUIDE_URL = 'https://www.programme-television.org/{}?bouquet=tnt'
@ -24,7 +25,6 @@ class FreeboxMoviePlanner:
self.config = json.load(config_file)
tmdbsimple.API_KEY = self.config['tmdb-api']
@staticmethod
def _tag_is_film(tag):
return (
@ -33,6 +33,24 @@ class FreeboxMoviePlanner:
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',
@ -40,54 +58,48 @@ class FreeboxMoviePlanner:
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 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
))
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("---")
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))
@ -102,8 +114,8 @@ class FreeboxMoviePlanner:
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())