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
|
2012-08-14 14:12:43 +00:00
|
|
|
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):
|
2010-07-07 23:25:57 +00:00
|
|
|
#this would also wor but does not cache:
|
|
|
|
'''
|
|
|
|
from urllib2 import urlopen
|
|
|
|
u = urlopen(url)
|
|
|
|
return u.url
|
|
|
|
'''
|
2012-08-15 15:15:40 +00:00
|
|
|
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):
|
2012-08-14 14:12:43 +00:00
|
|
|
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):
|
2012-08-14 13:58:05 +00:00
|
|
|
data = read_url(url)
|
2010-07-07 23:25:57 +00:00
|
|
|
r = {}
|
2012-08-14 14:12:43 +00:00
|
|
|
r['title'] = find_re(data, '<h1 class="movie_title">(.*?)</h1>')
|
2010-07-07 23:25:57 +00:00
|
|
|
if '(' in r['title']:
|
2012-08-14 14:12:43 +00:00
|
|
|
r['year'] = find_re(r['title'], '\((\d*?)\)')
|
2012-08-14 13:58:05 +00:00
|
|
|
r['title'] = strip_tags(re.sub('\((\d*?)\)', '', r['title'])).strip()
|
2012-08-14 14:12:43 +00:00
|
|
|
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)
|
|
|
|
meter = filter(lambda m: m[1].isdigit(), meter)
|
|
|
|
if meter:
|
|
|
|
r['tomatometer'] = meter[0][1]
|
2012-08-14 14:12:43 +00:00
|
|
|
r['rating'] = find_re(data, 'Average Rating: <span>([\d.]+)/10</span>')
|
|
|
|
r['user_score'] = find_re(data, '<span class="meter popcorn numeric ">(\d+)</span>')
|
|
|
|
r['user_rating'] = find_re(data, '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
|
|
|
|
|