python-ox/ox/web/allmovie.py

87 lines
3.3 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
2014-09-30 19:27:26 +00:00
from __future__ import print_function
2010-07-07 23:25:57 +00:00
import re
from ox import strip_tags, find_re
from ox.cache import read_url
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def get_id(url):
2012-07-08 12:16:57 +00:00
return url.split("/")[-1]
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def get_data(id):
2010-07-07 23:25:57 +00:00
'''
2012-08-15 15:15:40 +00:00
>>> get_data('129689')['cast'][1][1]
2023-07-27 16:12:13 +00:00
'Marianne'
2012-08-15 15:15:40 +00:00
>>> get_data('129689')['credits'][0][0]
2023-07-27 16:12:13 +00:00
'Jean-Luc Godard'
2012-08-15 15:15:40 +00:00
>>> get_data('129689')['posters'][0]
2023-07-27 16:12:13 +00:00
'http://image.allmusic.com/00/adg/cov200/dru800/u812/u81260bbffr.jpg'
2012-08-15 15:15:40 +00:00
>>> get_data('129689')['rating']
2023-07-27 16:12:13 +00:00
'4.5'
2010-07-07 23:25:57 +00:00
'''
2012-07-08 12:16:57 +00:00
if id.startswith('http'):
2012-08-15 15:15:40 +00:00
id = get_id(id)
2010-07-07 23:25:57 +00:00
data = {
2012-08-15 15:15:40 +00:00
"url": get_url(id)
2010-07-07 23:25:57 +00:00
}
html = read_url(data["url"], unicode=True)
2012-08-15 15:15:40 +00:00
data['aka'] = parse_list(html, 'AKA')
data['category'] = find_re(html, '<dt>category</dt>.*?<dd>(.*?)</dd>')
2012-08-15 15:15:40 +00:00
data['countries'] = parse_list(html, 'countries')
data['director'] = parse_entry(html, 'directed by')
data['genres'] = parse_list(html, 'genres')
data['keywords'] = parse_list(html, 'keywords')
data['posters'] = [find_re(html, '<img src="(http://cps-.*?)"')]
2012-08-15 15:15:40 +00:00
data['produced'] = parse_list(html, 'produced by')
data['rating'] = find_re(html, 'Stars" title="(.*?) Stars"')
2012-08-15 15:15:40 +00:00
data['released'] = parse_entry(html, 'released by')
data['releasedate'] = parse_list(html, 'release date')
data['runtime'] = parse_entry(html, 'run time').replace('min.', '').strip()
data['set'] = parse_entry(html, 'set in')
data['synopsis'] = strip_tags(find_re(html, '<div class="toggle-text" itemprop="description">(.*?)</div>')).strip()
2012-08-15 15:15:40 +00:00
data['themes'] = parse_list(html, 'themes')
data['types'] = parse_list(html, 'types')
data['year'] = find_re(html, '<span class="year">.*?(\d+)')
2012-07-08 12:16:57 +00:00
#data['stills'] = [re.sub('_derived.*?/', '', i) for i in re.compile('<a href="#" title="movie still".*?<img src="(.*?)"', re.DOTALL).findall(html)]
data['stills'] = re.compile('<a href="#" title="movie still".*?<img src="(.*?)"', re.DOTALL).findall(html)
#html = read_url("http://allmovie.com/work/%s/cast" % id, unicode=True)
2012-08-15 15:15:40 +00:00
#data['cast'] = parse_table(html)
#html = read_url("http://allmovie.com/work/%s/credits" % id, unicode=True)
2012-08-15 15:15:40 +00:00
#data['credits'] = parse_table(html)
html = read_url("http://allmovie.com/work/%s/review" % id, unicode=True)
data['review'] = strip_tags(find_re(html, '<div class="toggle-text" itemprop="description">(.*?)</div>')).strip()
2010-07-07 23:25:57 +00:00
return data
2012-08-15 15:15:40 +00:00
def get_url(id):
2012-07-08 12:16:57 +00:00
return "http://allmovie.com/work/%s" % id
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def parse_entry(html, title):
html = find_re(html, '<dt>%s</dt>.*?<dd>(.*?)</dd>' % title)
return strip_tags(html).strip()
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def parse_list(html, title):
html = find_re(html, '<dt>%s</dt>.*?<dd>(.*?)</dd>' % title.lower())
2012-08-21 06:41:49 +00:00
r = map(strip_tags, re.compile('<li>(.*?)</li>', re.DOTALL).findall(html))
2012-07-08 12:16:57 +00:00
if not r and html:
r = [strip_tags(html)]
2012-07-08 12:16:57 +00:00
return r
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def parse_table(html):
2012-08-21 06:41:49 +00:00
return [
[
strip_tags(r).strip().replace('&nbsp;', '')
for r in x.split('<td width="305">-')
]
for x in find_re(html, '<div id="results-table">(.*?)</table>').split('</tr>')[:-1]
]
2010-07-07 23:25:57 +00:00
2012-08-15 15:15:40 +00:00
def parse_text(html, title):
return strip_tags(find_re(html, '%s</td>.*?<td colspan="2"><p>(.*?)</td>' % title)).strip()
2010-07-07 23:25:57 +00:00
if __name__ == '__main__':
2014-09-30 19:27:26 +00:00
print(get_data('129689'))
# print(get_data('177524'))
2010-07-07 23:25:57 +00:00