Refactor code: separate fetching and printing
This commit is contained in:
@ -15,6 +15,7 @@ import textwrap
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
|
||||||
|
|
||||||
class FreeboxMoviePlanner:
|
class FreeboxMoviePlanner:
|
||||||
TV_GUIDE_URL = 'https://www.programme-television.org/{}?bouquet=tnt'
|
TV_GUIDE_URL = 'https://www.programme-television.org/{}?bouquet=tnt'
|
||||||
|
|
||||||
@ -24,15 +25,32 @@ class FreeboxMoviePlanner:
|
|||||||
self.config = json.load(config_file)
|
self.config = json.load(config_file)
|
||||||
tmdbsimple.API_KEY = self.config['tmdb-api']
|
tmdbsimple.API_KEY = self.config['tmdb-api']
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _tag_is_film(tag):
|
def _tag_is_film(tag):
|
||||||
return (
|
return (
|
||||||
tag.has_attr('data-nature')
|
tag.has_attr('data-nature')
|
||||||
and
|
and
|
||||||
tag['data-nature']=='films-telefilms'
|
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):
|
def getAllMovies(self):
|
||||||
days = deque(['lundi', 'mardi', 'mercredi',
|
days = deque(['lundi', 'mardi', 'mercredi',
|
||||||
@ -40,54 +58,48 @@ class FreeboxMoviePlanner:
|
|||||||
offset = datetime.datetime.today().weekday()
|
offset = datetime.datetime.today().weekday()
|
||||||
days.rotate(-1-offset)
|
days.rotate(-1-offset)
|
||||||
days.appendleft('')
|
days.appendleft('')
|
||||||
|
movies = {}
|
||||||
for day in days:
|
for day in days:
|
||||||
print('=== {}'.format(day.title()))
|
movies[day] = self.getMovies(day)
|
||||||
self.getMovies(day)
|
logging.info('Found the following movies: {}'.format(movies))
|
||||||
|
return movies
|
||||||
|
|
||||||
def getMovies(self, day=''):
|
def getMovies(self, day=''):
|
||||||
logging.info('Connecting to {}'.format(self.TV_GUIDE_URL))
|
logging.info('Connecting to {}'.format(self.TV_GUIDE_URL))
|
||||||
r = requests.get(self.TV_GUIDE_URL.format(day))
|
r = requests.get(self.TV_GUIDE_URL.format(day))
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
html = BeautifulSoup(r.text, 'html.parser')
|
html = BeautifulSoup(r.text, 'html.parser')
|
||||||
|
movies = []
|
||||||
for channel in html.select('.bloc_cnt'):
|
for channel in html.select('.bloc_cnt'):
|
||||||
if len(channel.select('em')):
|
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_title = movie.select('.texte_titre a')[0]['title']
|
||||||
|
|
||||||
movie_info = '{} - {} ({})'.format(
|
thismovie = {}
|
||||||
movie_title,
|
thismovie['title'] = movie_title
|
||||||
movie.select('.texte_cat a')[0].string,
|
thismovie['genre'] = movie.select('.texte_cat a')[0].string
|
||||||
channel.select('em')[0].string.replace('Programme ','')
|
thismovie['channel'] = channel.select('em')[0].string.replace( 'Programme ', '')
|
||||||
)
|
logging.info('Found movie: {}'.format(thismovie))
|
||||||
logging.info('Found movie: {}'.format(movie_info))
|
|
||||||
|
|
||||||
tmdb_details = self._getMovieRating(movie_title)
|
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(
|
if(
|
||||||
float(tmdb_details['vote_average'])
|
float(tmdb_details['vote_average'])
|
||||||
< self.config['minimum-rating']
|
< self.config['minimum-rating']
|
||||||
):
|
):
|
||||||
logging.warning('Bad rating ({}), skipping {}'.format(
|
logging.warning(
|
||||||
tmdb_details['vote_average'],
|
'Bad rating ({}), skipping {}'.format(
|
||||||
movie_title
|
tmdb_details['vote_average'], movie_title))
|
||||||
))
|
|
||||||
else:
|
else:
|
||||||
print(movie_info)
|
movies.append(thismovie)
|
||||||
print(' TMDB: {} - {}\n {}'.format(
|
return movies
|
||||||
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
|
|
||||||
))
|
|
||||||
|
|
||||||
|
|
||||||
def _getMovieRating(self, movie):
|
def _getMovieRating(self, movie):
|
||||||
logging.info("Searching for '{}' on TMDB".format(movie))
|
logging.info("Searching for '{}' on TMDB".format(movie))
|
||||||
@ -102,8 +114,8 @@ class FreeboxMoviePlanner:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.ERROR,
|
level=logging.INFO,
|
||||||
format=' %(asctime)s - %(levelname)s - %(message)s'
|
format=' %(asctime)s - %(levelname)s - %(message)s'
|
||||||
)
|
)
|
||||||
fmp = FreeboxMoviePlanner()
|
fmp = FreeboxMoviePlanner()
|
||||||
fmp.getAllMovies()
|
fmp.printAllMovies(fmp.getAllMovies())
|
||||||
|
Reference in New Issue
Block a user