pandora/oxdb/backend/load.py

174 lines
5.3 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
from django.db import models
from django.contrib.auth.models import User
2009-12-31 15:04:32 +00:00
import oxweb.imdb
from oxlib import stripTags, findRe
2009-06-08 16:08:59 +00:00
import models
def debug(*msgs):
for m in msgs:
print m,
print
'''Import data from imdb into database,
param: impdb id
return: Movie Object, None if failed
'''
def loadIMDb(imdbId):
if len(imdbId) != 7:
debug("IMDb ID not valid")
return None
try:
movie = models.Movie.byImdbId(imdbId)
except models.Movie.DoesNotExist:
#this shound not happen, just in case previous imports failed
try:
imdb = models.MovieImdb.objects.get(imdbId=imdbId)
except models.MovieImdb.DoesNotExist:
imdb = models.MovieImdb()
imdb.imdbId = imdbId
imdb.save()
2009-06-08 16:08:59 +00:00
movie = models.Movie()
movie.imdb = imdb
2009-06-08 16:08:59 +00:00
2009-12-31 15:04:32 +00:00
info = oxweb.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:
setattr(movie.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:
setattr(movie.imdb, _info_map.get(key, key), info[key])
2009-06-08 16:08:59 +00:00
2009-12-31 15:04:32 +00:00
movie.imdb.plot = oxweb.imdb.getMoviePlot(imdbId)
debug("plot", movie.imdb.plot)
2009-06-08 16:08:59 +00:00
2009-12-31 15:04:32 +00:00
movie.imdb.runtime = oxweb.imdb.getMovieRuntimeSeconds(imdbId)
business = oxweb.imdb.getMovieBusinessSum(imdbId)
2009-06-08 16:08:59 +00:00
for key in ('gross', 'profit', 'budget'):
setattr(movie.imdb, key, business[key])
2009-06-08 16:08:59 +00:00
movie.imdb.save()
movie.oxdbId = "__init__%s" % random.randint(0, 100000)
2009-06-08 16:08:59 +00:00
movie.save()
models.AlternativeTitle.objects.filter(movie=movie, manual=False).delete()
2009-12-31 15:04:32 +00:00
for i in oxweb.imdb.getMovieAKATitles(imdbId):
2009-08-16 12:23:29 +00:00
t = models.AlternativeTitle()
t.movie = movie
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
models.MovieCountry.objects.filter(movie=movie, 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 = models.Country.get_or_create(i)
models.MovieCountry.link(movie, country, position)
position += 1
2009-06-08 16:08:59 +00:00
#Language
models.MovieLanguage.objects.filter(movie=movie, 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 = models.Language.get_or_create(i)
models.MovieLanguage.link(movie, language, position)
position += 1
2009-06-08 16:08:59 +00:00
#Location
movie.locations_all.filter(manual=False).delete()
2009-12-31 15:04:32 +00:00
locations = oxweb.imdb.getMovieLocations(imdbId)
2009-06-08 16:08:59 +00:00
for i in locations:
debug("add location", i)
location = models.Location.get_or_create(i)
location.movies.add(movie)
#Genre
movie.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 = models.Genre.get_or_create(i)
genre.movies.add(movie)
2009-06-08 16:08:59 +00:00
#Keyword
movie.keywords_all.filter(manual=False).delete()
2009-12-31 15:04:32 +00:00
keywords = oxweb.imdb.getMovieKeywords(imdbId)
2009-06-08 16:08:59 +00:00
for g in keywords:
debug("add keyword", g)
keyword = models.Keyword.get_or_create(g)
keyword.movies.add(movie)
movie.trivia_all.filter(manual=False).delete()
2009-06-08 16:08:59 +00:00
position = 0
2009-12-31 15:04:32 +00:00
trivia = oxweb.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()
t.movie = movie
t.trivia = i
t.position = position
t.save()
position += 1
position = 0
models.Cast.objects.filter(movie=movie).filter(manual=False).delete()
2009-12-31 15:04:32 +00:00
credits = oxweb.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)
models.Cast.link(movie, person, role, character, position)
position += 1
movie.connections_all.filter(manual=False).delete()
2009-12-31 15:04:32 +00:00
connections = oxweb.imdb.getMovieConnections(imdbId)
2009-08-16 12:23:29 +00:00
for relation in connections:
for otherId in connections[relation]:
try:
object = models.Movie.objects.get(imdb__imdbId=otherId)
2009-08-16 12:23:29 +00:00
debug("add connection", relation, object)
models.Connection.get_or_create(movie, relation, object)
except models.Movie.DoesNotExist:
pass
2009-06-08 16:08:59 +00:00
2009-12-31 15:04:32 +00:00
reviews = oxweb.imdb.getMovieExternalReviews(imdbId)
movie.reviews_all.filter(manual=False).delete()
2009-06-08 16:08:59 +00:00
for r in reviews:
debug("add review", r)
review = models.Review.get_or_create(movie, r)
review.title = reviews[r]
review.save()
movie.oxdbId = movie.oxid()
movie.save()
return movie