python-ox/ox/web/flixter.py

75 lines
1.9 KiB
Python
Raw Permalink Normal View History

2010-09-04 10:42:37 +00:00
# -*- coding: UTF-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
from lxml.html import document_fromstring
from ox.cache import read_url
from ox import find_re, strip_tags
2010-09-04 10:42:37 +00:00
from ox.web.imdb import ImdbCombined
2012-08-15 15:15:40 +00:00
def get_data(id, timeout=-1):
2010-09-04 10:42:37 +00:00
'''
2012-08-15 15:15:40 +00:00
>>> get_data('the-matrix')['poster']
2010-09-04 10:42:37 +00:00
'http://content7.flixster.com/movie/16/90/52/1690525_gal.jpg'
2012-08-15 15:15:40 +00:00
>>> get_data('0133093')['poster']
2010-09-04 10:42:37 +00:00
'http://content7.flixster.com/movie/16/90/52/1690525_gal.jpg'
2012-08-15 15:15:40 +00:00
>>> get_data('2-or-3-things-i-know-about-her')['poster']
2010-09-04 10:42:37 +00:00
'http://content6.flixster.com/movie/10/95/43/10954392_gal.jpg'
2012-08-15 15:15:40 +00:00
>>> get_data('0078875')['rottentomatoes_id']
2010-09-04 10:42:37 +00:00
'http://www.rottentomatoes.com/m/the-tin-drum/'
'''
if len(id) == 7:
try:
int(id)
2012-08-15 15:15:40 +00:00
id = get_id(imdb=id)
2010-09-04 10:42:37 +00:00
except:
pass
data = {
2012-08-15 15:15:40 +00:00
"url": get_url(id),
2010-09-04 10:42:37 +00:00
}
2012-09-09 16:48:40 +00:00
html = read_url(data['url'], timeout=timeout, unicode=True)
2010-09-04 10:42:37 +00:00
doc = document_fromstring(html)
props = {
'og:title': 'title',
'og:image': 'poster',
'og:url': 'rottentomatoes_id',
}
for meta in doc.head.findall('meta'):
prop = meta.attrib.get('property', None)
content = meta.attrib.get('content', '')
if prop in props and content:
data[props[prop]] = content
for p in doc.body.find_class('synopsis'):
data['synopsis'] = p.text.strip()
if 'poster' in data and data['poster']:
data['poster'] = data['poster'].replace('_pro.jpg', '_gal.jpg')
if not 'title' in data:
return None
return data
2012-08-15 15:15:40 +00:00
def get_id(url=None, imdb=None):
2010-09-04 10:42:37 +00:00
'''
2012-08-15 15:15:40 +00:00
>>> get_id(imdb='0133093')
2023-07-27 16:12:13 +00:00
'the-matrix'
2010-09-04 10:42:37 +00:00
2012-08-15 15:15:40 +00:00
#>>> get_id(imdb='0060304')
2023-07-27 16:12:13 +00:00
#'2-or-3-things-i-know-about-her'
2010-09-04 10:42:37 +00:00
'''
2012-08-15 15:15:40 +00:00
if imdb:
i = ImdbCombined(imdb)
title = i['title']
return title.replace(' ', '-').lower().replace("'", '')
2010-09-04 10:42:37 +00:00
return url.split('/')[-1]
2012-08-15 15:15:40 +00:00
def get_url(id):
2010-09-04 10:42:37 +00:00
return "http://www.flixster.com/movie/%s"%id