python-ox/ox/web/freebase.py

43 lines
1.3 KiB
Python
Raw Normal View History

2010-07-17 12:02:08 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import json
from ox.cache import read_url
from ox import find_re
2010-07-17 12:02:08 +00:00
2012-08-15 15:15:40 +00:00
class Freebase(dict):
2010-07-17 12:02:08 +00:00
def __init__(self, id, timeout=-1):
url = "http://ids.freebaseapps.com/get_ids?id=/authority/imdb/title/tt%s" % id
'''
"http://graph.freebase.com/imdb.title.tt%s" % id
might also be of interest at some point, right now not much info
'''
data = read_url(url, unicode=True)
2010-07-17 12:02:08 +00:00
try:
data = json.loads(data)
except ValueError:
return
'''
for key in data:
self[key] = data[key]
'''
for key in ('id', 'guid', 'name'):
self[key] = data[key]
keys = {
'wikipedia': '/wikipedia/en',
'netflix': '/authority/netflix/movie',
'nytimes': '/source/nytimes/movie',
'metacritic': '/source/metacritic/movie',
}
for key in keys:
2024-09-11 21:52:01 +00:00
links = filter(lambda x: x['namespace'] == keys[key], data['ids'])
2010-07-17 12:02:08 +00:00
if links:
self[key] = links[0]['uri']
if 'nytimes' in self:
self['nytimes'] = self['nytimes'].replace('_/overview', '%s/overview' % self['name'].replace(' ', '-'))
2024-09-11 21:52:01 +00:00
self['amgId'] = find_re(self['nytimes'], r'movie/(\d+)/')
2010-07-17 12:02:08 +00:00