A Python script to help you select the movies you want to record with your Freebox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.4 KiB

#!/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
import requests
import datetime
import tmdbsimple
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'
)
@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'])
offset = datetime.datetime.today().weekday()
days.rotate(-1-offset)
days.appendleft('')
movies = {}
for day in days:
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):
movie_title = movie.select('.texte_titre a')[0]['title']
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 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:
movies.append(thismovie)
return movies
def _getMovieRating(self, movie):
logging.info("Searching for '{}' on TMDB".format(movie))
search = tmdbsimple.Search()
search.movie(query=movie, language=self.config['tmdb-language'])
logging.info("Found {}".format(search.results))
if len(search.results):
return search.results[0]
else:
return []
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
format=' %(asctime)s - %(levelname)s - %(message)s'
)
fmp = FreeboxMoviePlanner()
fmp.printAllMovies(fmp.getAllMovies())