oxdata/lookup/models.py
2009-07-17 17:17:50 +02:00

128 lines
4.7 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
import hashlib
from django.db import models
from django.db.models import Q, Max
from django.contrib.auth.models import User
from django.utils import simplejson
import oxweb.wikipedia
import oxweb.imdb
from oxlib import stripTags
def getMovieIdByImdbId(imdb_id):
try:
m = MovieId.objects.get(imdb_id=imdb_id)
except MovieId.DoesNotExist:
m = MovieId()
m.imdb_id = imdb_id
m.save()
#m.updateFromImdb()
return m
class MovieId(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=1000, blank=True, default='')
year = models.CharField(max_length=4, blank=True, default='')
director = models.CharField(max_length=1000, blank=True, default='')
series_title = models.CharField(max_length=1000, blank=True, default='')
episode_title = models.CharField(max_length=1000, blank=True, default='')
season = models.IntegerField(default=-1)
episode = models.IntegerField(default=-1)
oxdb_id = models.CharField(max_length=42, unique=True, blank=True, null=True, default=None)
imdb_id = models.CharField(max_length=7, unique=True, blank=True, null=True, default=None)
amg_id = models.IntegerField(unique=True, blank=True, null=True, default=None)
wikipedia_id = models.CharField(unique=True, max_length=255, blank=True, null=True, default=None)
criterion_id = models.IntegerField(unique=True, blank=True, null=True, default=None)
impawards_id = models.CharField(max_length=255, unique=True, blank=True, null=True, default=None)
#FIXME: look into other ids
#what about tv.com ids/urls for tv episodes
#movieposterdb can be mapped via imdbid and does not need to be saved here
def __unicode__(self):
id = self.imdb_id
if not id:
id = self.id
return '%s (%s)' % (self.title, id)
def updateFromWikipedia(self):
if self.wikipedia_id:
wikipedia_url = oxweb.wikipedia.getUrl(self.wikipedia_id)
data = oxweb.wikipedia.getMovieData(wikipedia_url)
_key = {}
for key in ('imdb_id', 'amg_id'):
if key in data:
if data[key] and not getattr(self, _key.get(key, key)):
setattr(self, _key.get(key, key), data[key])
self.save()
def updateFromImdb(self):
if self.imdb_id:
data = oxweb.imdb.getMovieInfo(self.imdb_id)
_key = {
'episode title': 'episode_title',
'series title': 'series_title',
}
for key in ('title', 'year', 'series title', 'episode title', 'season', 'episode'):
if key in data and data[key]:
setattr(self, _key.get(key, key), data[key])
directors = []
credits = oxweb.imdb.getMovieCredits(self.imdb_id)
if 'directors' in credits:
directors = credits['directors']
self.director = ', '.join([stripTags(d[0]) for d in directors])
if not self.wikipedia_id:
self.wikipedia_id = oxweb.wikipedia.getId(oxweb.wikipedia.getUrlByImdb(self.imdb_id))
if not self.oxdb_id:
self.gen_oxdb_id()
self.save()
def gen_oxdb_id(self):
oxid_value = u"\n".join([self.title, self.director, self.year])
oxid = hashlib.sha1(oxid_value.encode('utf-8')).hexdigest()
if self.episode > -1:
oxid_value = u"\n".join([self.series_title, "%02d" % self.season])
oxid = hashlib.sha1(oxid_value.encode('utf-8')).hexdigest()[:20]
oxid_value = u"\n".join(["%02d" % self.episode, self.episode_title, self.director, self.year])
oxid += hashlib.sha1(oxid_value.encode('utf-8')).hexdigest()[:20]
self.oxdb_id = u"0x" + oxid
self.save()
def json(self):
json = {}
keys = [
'title',
'director',
'year',
'imdb_id',
'oxdb_id',
'amg_id',
'wikipedia_id',
'criterion_id',
'impawards_id',
]
if self.episode > -1:
keys += ['episode', 'season', 'episode_title', 'series_title']
for key in keys:
value = getattr(self, key)
if value:
json[key] = value
return simplejson.dumps(json, indent=4)
class Karagarga(models.Model):
movie_id = models.ForeignKey(MovieId, related_name='karagarga_ids', default=None)
karagarga_id = models.IntegerField(unique=True)
@classmethod
def maxId(cls):
return cls.objects.aggregate(Max('karagarga_id'))['karagarga_id__max']