python-ox/ox/web/rottentomatoes.py

49 lines
1.8 KiB
Python
Raw Normal View History

2010-07-07 23:25:57 +00:00
# -*- coding: UTF-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
2012-08-15 15:15:40 +00:00
from ox.cache import read_url
from ox import find_re, strip_tags
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def get_url(id=None, imdb=None):
if imdb:
url = "http://www.rottentomatoes.com/alias?type=imdbid&s=%s" % imdb
data = read_url(url)
if "movie_title" in data:
movies = re.compile('(/m/.*?/)').findall(data)
if movies:
return "http://www.rottentomatoes.com" + movies[0]
2010-07-07 23:25:57 +00:00
return None
2012-07-08 11:09:58 +00:00
def get_og(data, key):
return find_re(data, '<meta property="og:%s".*?content="(.*?)"' % key)
2012-07-08 11:09:58 +00:00
2012-08-15 15:15:40 +00:00
def get_data(url):
data = read_url(url)
2010-07-07 23:25:57 +00:00
r = {}
r['title'] = find_re(data, '<h1 class="movie_title">(.*?)</h1>')
2010-07-07 23:25:57 +00:00
if '(' in r['title']:
2024-09-11 21:52:01 +00:00
r['year'] = find_re(r['title'], r'\((\d*?)\)')
r['title'] = strip_tags(re.sub(r'\((\d*?)\)', '', r['title'])).strip()
r['summary'] = strip_tags(find_re(data, '<p id="movieSynopsis" class="movie_synopsis" itemprop="description">(.*?)</p>')).strip()
2012-07-08 11:09:58 +00:00
r['summary'] = r['summary'].replace('\t', ' ').replace('\n', ' ').replace(' ', ' ').replace(' ', ' ')
if not r['summary']:
r['summary'] = get_og(data, 'description')
meter = re.compile('<span id="all-critics-meter" class="meter(.*?)">(.*?)</span>').findall(data)
2016-06-08 13:32:46 +00:00
meter = [m for m in meter if m[1].isdigit()]
2012-07-08 11:09:58 +00:00
if meter:
r['tomatometer'] = meter[0][1]
2024-09-11 21:52:01 +00:00
r['rating'] = find_re(data, r'Average Rating: <span>([\d.]+)/10</span>')
r['user_score'] = find_re(data, r'<span class="meter popcorn numeric ">(\d+)</span>')
r['user_rating'] = find_re(data, r'Average Rating: ([\d.]+)/5')
2012-07-08 11:09:58 +00:00
poster = get_og(data, 'image')
if poster and not 'poster_default.gif' in poster:
r['posters'] = [poster]
for key in r.keys():
if not r[key]:
del r[key]
2010-07-07 23:25:57 +00:00
return r