python-oxweb/ox/impawards.py
2008-05-09 13:54:41 +02:00

59 lines
No EOL
2.1 KiB
Python

import re
import ox.imdb as imdb
from oxutils.cache import getUrlUnicode
from oxutils.text import findRe
def getMovieData(title = '', director = '', imdbId = ''):
data = {'posterUrls': []}
if not imdbId:
imdbId = imdb.getMovieId(title, director)
print imdbId
html = getUrlUnicode('http://impawards.com/archives/latest.html', timeout = 0)
pages = int(findRe(html, '<a href = page(.*?).html>'))
for page in range(pages + 1, 0, -1):
print page
if page <= pages:
html = getUrlUnicode('http://impawards.com/archives/page%s.html' % page, timeout = -1)
urls = parseArchivePage(html)
print urls
for url in urls:
html = getUrlUnicode(url)
d = parseMoviePage(html)
print d
if d['imdbId'] == imdbId:
data['posterUrls'].append(d['posterUrl'])
print d['posterUrl']
data['posterUrls'].sort()
return data
def parseArchivePage(html):
urls = []
results = re.compile('<a href = \.\./(.*?)>', re.DOTALL).findall(html)
for result in results:
urls.append('http://impawards.com/%s' % result)
return urls
def parseMoviePage(html):
data = {}
data['imdbId'] = findRe(html, 'imdb.com/title/tt(.*?) ')
data['title'] = findRe(html, '<font size=\+3>(.*?) \(')
data['year'] = findRe(html, '\(<a href="eligible.html">(.*?)</a>\)')
result = findRe(html, '<a href = (\w*?_xlg.html) target= _blank>')
if result:
url = 'http://impawards.com/%s/%s' % (data['year'], result)
html = getUrlUnicode(url, timeout = -1)
d = parsePosterPage(html, data['year'])
data['posterUrl'] = d['posterUrl']
else:
data['posterUrl'] = 'http://impawards.com/%s/%s' % (data['year'], findRe(html, '<td align=center><br><img SRC="(.*?)"'))
return data
def parsePosterPage(html, year):
data = {}
data['posterUrl'] = 'http://impawards.com/%s/%s' % (year, findRe(html, '<img SRC="(.*?)"'))
return data
if __name__ == '__main__':
getMovieData('Brick', 'Rian Johnson')