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.
 

159 lines
5.0 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 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:
TV_GUIDE_URL = 'https://www.programme-television.org/{}?bouquet=tnt'
@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
@staticmethod
def _tag_is_film(tag):
"""
Helper to check if a tag is a film
"""
return (
tag.has_attr('data-nature')
and
tag['data-nature'] == 'films-telefilms'
)
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):
days = deque(['lundi', 'mardi', 'mercredi',
'jeudi', 'vendredi', 'samedi', 'dimanche'])
offset = datetime.datetime.today().weekday()
days.rotate(-1-offset)
days.appendleft('')
for day in days:
self.movies += TVGuideScraper.getMovies(day)
logging.info('Found the following movies: {}'.format(self.movies))
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]
def _findMovieOnTMDB(self, movie):
logging.info("Searching for '{}' on TMDB".format(movie))
search = tmdbsimple.Search()
search.movie(query=movie, language=self.config['tmdb-language'])
if len(search.results):
logging.info("Found '{}'".format(
search.results[0]['title']
))
return search.results[0]
else:
logging.warning("'{}' not found on TMDB!".format(movie))
return []
if __name__ == '__main__':
logging.basicConfig(
level=logging.INFO,
format=' %(asctime)s - %(levelname)s - %(message)s'
)
fmp = FreeboxMoviePlanner()
fmp.scapeAllMovies()
fmp.findMoviesOnTMDB()
fmp.filterBadRatings()
fmp.printAllMovies()