cablegates/pandora/item/load.py

176 lines
5.4 KiB
Python
Raw Normal View History

2009-06-08 16:08:59 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import random
2009-06-08 16:08:59 +00:00
import os.path
2010-02-03 12:05:38 +00:00
2009-06-08 16:08:59 +00:00
from django.db import models
from django.contrib.auth.models import User
2010-07-07 22:46:41 +00:00
from ox import stripTags, findRe
import ox.web.imdb
2009-06-08 16:08:59 +00:00
import models
2010-02-03 12:05:38 +00:00
2009-06-08 16:08:59 +00:00
def debug(*msgs):
for m in msgs:
print m,
print
'''Import data from imdb into database,
param: impdb id
2010-09-23 16:01:48 +00:00
return: Item Object, None if failed
2009-06-08 16:08:59 +00:00
'''
def loadIMDb(imdbId):
if len(imdbId) != 7:
debug("IMDb ID not valid")
return None
try:
2010-09-23 16:01:48 +00:00
item = models.Item.byImdbId(imdbId)
except models.Item.DoesNotExist:
#this shound not happen, just in case previous imports failed
try:
2010-09-23 16:01:48 +00:00
imdb = models.ItemImdb.objects.get(imdbId=imdbId)
except models.ItemImdb.DoesNotExist:
imdb = models.ItemImdb()
imdb.imdbId = imdbId
imdb.save()
2010-09-23 16:01:48 +00:00
item = models.Item()
item.imdb = imdb
2009-06-08 16:08:59 +00:00
2010-07-07 22:46:41 +00:00
info = ox.web.imdb.getMovieInfo(imdbId)
2009-06-08 16:08:59 +00:00
for key in ('title',
'tagline',
'year',
'release_date',
'rating',
'votes',
2009-12-31 15:04:32 +00:00
'series_imdb',
2009-06-08 16:08:59 +00:00
'season',
'episode'):
if key in info:
2010-09-23 16:01:48 +00:00
setattr(item.imdb, key, info[key])
2009-06-08 16:08:59 +00:00
debug(key, info[key])
_info_map = {
'episode title': 'episode_title',
'series title': 'series_title',
}
for key in _info_map.keys():
if key in info:
2010-09-23 16:01:48 +00:00
setattr(item.imdb, _info_map.get(key, key), info[key])
2009-06-08 16:08:59 +00:00
2010-09-23 16:01:48 +00:00
item.imdb.plot = ox.web.imdb.getMoviePlot(imdbId)
debug("plot", item.imdb.plot)
2009-06-08 16:08:59 +00:00
2010-09-23 16:01:48 +00:00
item.imdb.runtime = ox.web.imdb.getMovieRuntimeSeconds(imdbId)
2010-07-07 22:46:41 +00:00
business = ox.web.imdb.getMovieBusinessSum(imdbId)
2009-06-08 16:08:59 +00:00
for key in ('gross', 'profit', 'budget'):
2010-09-23 16:01:48 +00:00
setattr(item.imdb, key, business[key])
2009-06-08 16:08:59 +00:00
2010-09-23 16:01:48 +00:00
item.imdb.save()
item.oxdbId = "__init__%s" % random.randint(0, 100000)
item.save()
models.AlternativeTitle.objects.filter(item=item, manual=False).delete()
2010-07-07 22:46:41 +00:00
for i in ox.web.imdb.getMovieAKATitles(imdbId):
2009-08-16 12:23:29 +00:00
t = models.AlternativeTitle()
2010-09-23 16:01:48 +00:00
t.item = item
2009-08-16 12:23:29 +00:00
t.title = i[0]
t.type = i[1]
t.save()
2009-06-08 16:08:59 +00:00
#FIXME: related tables should be cleaned to not accumulate cruft
#Country
2010-09-23 16:01:48 +00:00
models.ItemCountry.objects.filter(item=item, manual=False).delete()
2009-06-08 16:08:59 +00:00
position = 0
2009-08-16 12:23:29 +00:00
if 'country' in info:
for i in info['country']:
debug("add country", i)
country, created = models.Country.objects.get_or_create(name=i)
2010-09-23 16:01:48 +00:00
models.ItemCountry.link(item, country, position)
2009-08-16 12:23:29 +00:00
position += 1
2009-06-08 16:08:59 +00:00
#Language
2010-09-23 16:01:48 +00:00
models.ItemLanguage.objects.filter(item=item, manual=False).delete()
2009-06-08 16:08:59 +00:00
position = 0
2009-08-16 12:23:29 +00:00
if 'language' in info:
for i in info['language']:
debug("add language", i)
language, created = models.Language.objects.get_or_create(name=i)
2010-09-23 16:01:48 +00:00
models.ItemLanguage.link(item, language, position)
2009-08-16 12:23:29 +00:00
position += 1
2009-06-08 16:08:59 +00:00
#Location
2010-09-23 16:01:48 +00:00
item.locations_all.filter(manual=False).delete()
2010-07-07 22:46:41 +00:00
locations = ox.web.imdb.getMovieLocations(imdbId)
2009-06-08 16:08:59 +00:00
for i in locations:
debug("add location", i)
location, created = models.Location.objects.get_or_create(name=i)
2010-09-23 16:01:48 +00:00
location.items.add(item)
2009-06-08 16:08:59 +00:00
#Genre
2010-09-23 16:01:48 +00:00
item.genres_all.filter(manual=False).delete()
2009-08-16 12:23:29 +00:00
if 'genre' in info:
for i in info['genre']:
debug("add genre", i)
genre, created = models.Genre.objects.get_or_create(name=i)
2010-09-23 16:01:48 +00:00
genre.items.add(item)
2009-06-08 16:08:59 +00:00
#Keyword
2010-09-23 16:01:48 +00:00
item.keywords_all.filter(manual=False).delete()
2010-07-07 22:46:41 +00:00
keywords = ox.web.imdb.getMovieKeywords(imdbId)
2009-06-08 16:08:59 +00:00
for g in keywords:
debug("add keyword", g)
keyword, created = models.Keyword.objects.get_or_create(name=g)
2010-09-23 16:01:48 +00:00
keyword.items.add(item)
2009-06-08 16:08:59 +00:00
2010-09-23 16:01:48 +00:00
item.trivia_all.filter(manual=False).delete()
2009-06-08 16:08:59 +00:00
position = 0
2010-07-07 22:46:41 +00:00
trivia = ox.web.imdb.getMovieTrivia(imdbId)
2009-06-08 16:08:59 +00:00
for i in trivia:
2009-08-16 12:23:29 +00:00
debug("add trivia", i)
2009-06-08 16:08:59 +00:00
t = models.Trivia()
2010-09-23 16:01:48 +00:00
t.item = item
2009-06-08 16:08:59 +00:00
t.trivia = i
t.position = position
t.save()
position += 1
position = 0
2010-09-23 16:01:48 +00:00
models.Cast.objects.filter(item=item).filter(manual=False).delete()
2010-07-07 22:46:41 +00:00
credits = ox.web.imdb.getMovieCredits(imdbId)
2009-06-08 16:08:59 +00:00
for role in credits:
for p in credits[role]:
name = stripTags(p[0])
imdb_id = findRe(p[0], 'nm(\d{7})')
debug("add cast", name)
#FIXME: we could save character information here
character = stripTags(p[1])
person = models.Person.get_or_create(name, imdb_id)
2010-09-23 16:01:48 +00:00
models.Cast.link(item, person, role, character, position)
2009-06-08 16:08:59 +00:00
position += 1
2010-09-23 16:01:48 +00:00
item.connections_all.filter(manual=False).delete()
2010-07-07 22:46:41 +00:00
connections = ox.web.imdb.getMovieConnections(imdbId)
2009-08-16 12:23:29 +00:00
for relation in connections:
for otherId in connections[relation]:
try:
2010-09-23 16:01:48 +00:00
object = models.Item.objects.get(imdb__imdbId=otherId)
2009-08-16 12:23:29 +00:00
debug("add connection", relation, object)
2010-09-23 16:01:48 +00:00
models.Connection.get_or_create(item, relation, object)
except models.Item.DoesNotExist:
2009-08-16 12:23:29 +00:00
pass
2009-06-08 16:08:59 +00:00
2010-07-07 22:46:41 +00:00
reviews = ox.web.imdb.getMovieExternalReviews(imdbId)
2010-09-23 16:01:48 +00:00
item.reviews_all.filter(manual=False).delete()
2009-06-08 16:08:59 +00:00
for r in reviews:
debug("add review", r)
2010-09-23 16:01:48 +00:00
review = models.Review.get_or_create(item, r)
2009-06-08 16:08:59 +00:00
review.title = reviews[r]
review.save()
2010-09-23 16:01:48 +00:00
item.oxdbId = item.oxid()
item.save()
return item
2009-06-08 16:08:59 +00:00