2019-09-24 00:10:50 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""
|
|
|
|
Simple script that extracts information from Télé 7 jours and TMDB
|
|
|
|
to help choosing the movies you want to record with your Freebox
|
|
|
|
|
|
|
|
Todo :
|
|
|
|
* Prompt the user for movies he wants to record and plan them with the FB API
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
import logging
|
2019-09-23 22:29:13 +02:00
|
|
|
import requests
|
2019-09-28 23:45:49 +02:00
|
|
|
import datetime
|
2019-09-24 00:10:50 +02:00
|
|
|
import tmdbsimple
|
|
|
|
import textwrap
|
2019-09-23 22:29:13 +02:00
|
|
|
from bs4 import BeautifulSoup
|
2019-09-28 23:45:49 +02:00
|
|
|
from collections import deque
|
2019-09-23 22:29:13 +02:00
|
|
|
|
2019-10-05 16:54:08 +02:00
|
|
|
|
2019-10-06 00:59:41 +02:00
|
|
|
class Movie:
|
|
|
|
def __init__(self):
|
|
|
|
self.day = ''
|
|
|
|
self.title = ''
|
|
|
|
self.genre = ''
|
|
|
|
self.channel = ''
|
|
|
|
self.rating = ''
|
|
|
|
self.original_title = ''
|
|
|
|
self.overview = ''
|
|
|
|
self.good = False
|
|
|
|
self.tmdb_id = ''
|
|
|
|
self.url = ''
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return '{}: {} - {} ({})\n TMDB: {} - {}\n @ {}\n {}'.format(
|
|
|
|
'Today' if self.day == '' else self.day,
|
|
|
|
self.title,
|
|
|
|
self.genre,
|
|
|
|
self.channel,
|
|
|
|
self.rating,
|
|
|
|
self.original_title,
|
|
|
|
self.url,
|
|
|
|
self.overview
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return 'Movie <{}({})>'.format(self.title, self.rating)
|
|
|
|
|
|
|
|
|
|
|
|
class TVGuideScraper:
|
2019-09-23 22:29:13 +02:00
|
|
|
TV_GUIDE_URL = 'https://www.programme-television.org/{}?bouquet=tnt'
|
2019-10-05 16:54:08 +02:00
|
|
|
|
2019-10-06 00:59:41 +02:00
|
|
|
@staticmethod
|
|
|
|
def getMovies(day=''):
|
|
|
|
logging.info('Connecting to {}'.format(TVGuideScraper.TV_GUIDE_URL))
|
|
|
|
r = requests.get(TVGuideScraper.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 movietag in channel.find_all(TVGuideScraper._tag_is_film):
|
|
|
|
movie = Movie()
|
|
|
|
movie.title = \
|
|
|
|
movietag.select('.texte_titre a')[0]['title']
|
|
|
|
movie.genre = movietag.select('.texte_cat a')[0].string
|
|
|
|
movie.channel = channel.select('em')[0]\
|
|
|
|
.string.replace('Programme ', '')
|
|
|
|
movie.day = day.title()
|
|
|
|
|
|
|
|
logging.info('Found movie: {0!r}'.format(movie))
|
|
|
|
|
|
|
|
movies.append(movie)
|
|
|
|
|
|
|
|
return movies
|
2019-09-24 00:10:50 +02:00
|
|
|
|
2019-09-23 22:29:13 +02:00
|
|
|
@staticmethod
|
|
|
|
def _tag_is_film(tag):
|
2019-10-06 00:59:41 +02:00
|
|
|
"""
|
|
|
|
Helper to check if a tag is a film
|
|
|
|
"""
|
2019-09-23 22:29:13 +02:00
|
|
|
return (
|
|
|
|
tag.has_attr('data-nature')
|
2019-10-05 16:54:08 +02:00
|
|
|
and
|
|
|
|
tag['data-nature'] == 'films-telefilms'
|
2019-09-23 22:29:13 +02:00
|
|
|
)
|
2019-10-05 16:54:08 +02:00
|
|
|
|
2019-10-06 00:59:41 +02:00
|
|
|
|
|
|
|
class FreeboxMoviePlanner:
|
|
|
|
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']
|
|
|
|
self.movies = []
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
result = 'FreeboxMoviePlanner <Movies:\n'
|
|
|
|
for movie in self.movies:
|
|
|
|
result += ' {!r}\n'.format(movie)
|
|
|
|
result += '>'
|
|
|
|
return result
|
|
|
|
|
|
|
|
def printAllMovies(self):
|
|
|
|
for movie in self.movies:
|
|
|
|
print(movie)
|
|
|
|
print()
|
|
|
|
|
|
|
|
def scapeAllMovies(self):
|
2019-09-28 23:45:49 +02:00
|
|
|
days = deque(['lundi', 'mardi', 'mercredi',
|
2019-10-05 16:54:08 +02:00
|
|
|
'jeudi', 'vendredi', 'samedi', 'dimanche'])
|
2019-09-28 23:45:49 +02:00
|
|
|
offset = datetime.datetime.today().weekday()
|
|
|
|
days.rotate(-1-offset)
|
|
|
|
days.appendleft('')
|
2019-09-24 00:35:57 +02:00
|
|
|
for day in days:
|
2019-10-06 00:59:41 +02:00
|
|
|
self.movies += TVGuideScraper.getMovies(day)
|
|
|
|
logging.info('Found the following movies: {}'.format(self.movies))
|
2019-09-28 23:45:49 +02:00
|
|
|
|
2019-10-06 00:59:41 +02:00
|
|
|
def findMoviesOnTMDB(self):
|
|
|
|
for movie in self.movies:
|
|
|
|
tmdb_details = self._findMovieOnTMDB(movie.title)
|
|
|
|
if tmdb_details:
|
|
|
|
movie.rating = tmdb_details['vote_average']
|
|
|
|
movie.original_title = \
|
|
|
|
tmdb_details['original_title']
|
|
|
|
movie.overview = '\n '.join(textwrap.wrap(
|
|
|
|
tmdb_details['overview'], 75)
|
|
|
|
)
|
|
|
|
movie.tmdb_id = tmdb_details['id']
|
|
|
|
movie.good = \
|
|
|
|
float(movie.rating) >= self.config['minimum-rating']
|
|
|
|
movie.url = 'https://www.themoviedb.org/movie/{}?language={}' \
|
|
|
|
.format(movie.tmdb_id, self.config['tmdb-language'])
|
|
|
|
|
|
|
|
def filterBadRatings(self):
|
|
|
|
self.movies = [movie for movie in self.movies if movie.good]
|
2019-09-28 23:45:49 +02:00
|
|
|
|
2019-10-06 00:59:41 +02:00
|
|
|
def _findMovieOnTMDB(self, movie):
|
2019-09-24 00:10:50 +02:00
|
|
|
logging.info("Searching for '{}' on TMDB".format(movie))
|
|
|
|
search = tmdbsimple.Search()
|
|
|
|
search.movie(query=movie, language=self.config['tmdb-language'])
|
|
|
|
if len(search.results):
|
2019-10-06 00:59:41 +02:00
|
|
|
logging.info("Found '{}'".format(
|
|
|
|
search.results[0]['title']
|
|
|
|
))
|
2019-09-24 00:10:50 +02:00
|
|
|
return search.results[0]
|
|
|
|
else:
|
2019-10-06 00:59:41 +02:00
|
|
|
logging.warning("'{}' not found on TMDB!".format(movie))
|
2019-09-24 00:10:50 +02:00
|
|
|
return []
|
2019-09-28 23:45:49 +02:00
|
|
|
|
2019-10-05 16:54:08 +02:00
|
|
|
|
2019-09-23 22:29:13 +02:00
|
|
|
if __name__ == '__main__':
|
2019-09-24 00:10:50 +02:00
|
|
|
logging.basicConfig(
|
2019-10-05 16:54:08 +02:00
|
|
|
level=logging.INFO,
|
2019-09-24 00:10:50 +02:00
|
|
|
format=' %(asctime)s - %(levelname)s - %(message)s'
|
|
|
|
)
|
2019-09-23 22:29:13 +02:00
|
|
|
fmp = FreeboxMoviePlanner()
|
2019-10-06 00:59:41 +02:00
|
|
|
fmp.scapeAllMovies()
|
|
|
|
fmp.findMoviesOnTMDB()
|
|
|
|
fmp.filterBadRatings()
|
|
|
|
fmp.printAllMovies()
|