Files
FreeboxMoviePlanner/FreeboxMoviePlanner.py

100 lines
3.7 KiB
Python
Executable File

#!/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 tmdbsimple
import textwrap
from bs4 import BeautifulSoup
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'
)
def getAllMovies(self):
days = ['', 'dimanche', 'lundi', 'mardi', 'mercredi',
'jeudi', 'vendredi', 'samedi']
for day in days:
print('=== {}'.format(day.title()))
self.getMovies(day)
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')
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']
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))
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("---")
else:
logging.warning('No TMDB match for {}'.format(
movie_title
))
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.ERROR,
format=' %(asctime)s - %(levelname)s - %(message)s'
)
fmp = FreeboxMoviePlanner()
fmp.getAllMovies()