python-oxweb/oxweb/metacritic.py

46 lines
1.4 KiB
Python
Raw Normal View History

2008-07-29 17:04:23 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
from urllib import quote
from oxlib.cache import readUrl, readUrlUnicode
from oxlib import findRe, decodeHtml, stripTags
2008-07-29 17:04:23 +00:00
def getMetacriticShowUrl(title):
title = quote(title)
url = "http://www.metacritic.com/search/process?ty=6&ts=%s&tfs=tvshow_title&x=0&y=0&sb=0&release_date_s=&release_date_e=&metascore_s=&metascore_e=" % title
data = readUrl(url)
2008-07-29 17:04:23 +00:00
return findRe(data, '(http://www.metacritic.com/tv/shows/.*?)\?')
def getData(title, url=None):
if not url:
url = getMetacriticShowUrl(title)
if not url:
return None
data = readUrlUnicode(url)
2008-07-29 17:04:23 +00:00
score = findRe(data, 'ALT="Metascore: (.*?)"')
if score:
score = int(score)
else:
score = -1
reviews = re.compile(
'<div class="scoreandreview"><div class="criticscore">(.*?)</div>'
'.*?<span class="publication">(.*?)</span>'
'.*?<span class="criticname">(.*?)</span></div>'
'.*?<div class="quote">(.*?)<br>'
'.*?<a href="(.*?)" ', re.DOTALL).findall(data)
metacritics = []
for review in reviews:
metacritics.append({
'score': int(review[0]),
'publication':review[1],
'critic':decodeHtml(review[2]),
'quote': stripTags(review[3]).strip(),
'link': review[4],
})
return dict(score = score, critics = metacritics, url = url)