oxdata/lookup/models.py

129 lines
4.7 KiB
Python
Raw Normal View History

2009-07-13 08:09:58 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
2009-07-13 12:53:58 +00:00
import os
import hashlib
2009-07-13 08:09:58 +00:00
from django.db import models
2009-07-15 16:39:28 +00:00
from django.db.models import Q, Max
2009-07-13 08:09:58 +00:00
from django.contrib.auth.models import User
2009-07-15 16:39:28 +00:00
from django.utils import simplejson
2009-07-13 08:09:58 +00:00
import oxweb.wikipedia
import oxweb.imdb
from oxlib import stripTags
2009-07-13 08:09:58 +00:00
2009-07-13 12:32:01 +00:00
def getMovieIdByImdbId(imdb_id):
2009-07-13 08:29:42 +00:00
try:
2009-07-13 12:32:01 +00:00
m = MovieId.objects.get(imdb_id=imdb_id)
except MovieId.DoesNotExist:
m = MovieId()
2009-07-13 08:29:42 +00:00
m.imdb_id = imdb_id
m.save()
2009-07-15 16:39:28 +00:00
#m.updateFromImdb()
2009-07-13 08:29:42 +00:00
return m
2009-07-13 12:32:01 +00:00
class MovieId(models.Model):
2009-07-13 08:09:58 +00:00
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
2009-07-13 12:53:58 +00:00
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='')
2009-07-13 14:44:09 +00:00
series_title = models.CharField(max_length=1000, blank=True, default='')
episode_title = models.CharField(max_length=1000, blank=True, default='')
2009-07-13 12:53:58 +00:00
season = models.IntegerField(default=-1)
episode = models.IntegerField(default=-1)
2009-07-13 08:09:58 +00:00
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)
2009-07-13 12:32:01 +00:00
wikipedia_id = models.CharField(unique=True, max_length=255, blank=True, null=True, default=None)
2009-07-13 08:09:58 +00:00
criterion_id = models.IntegerField(unique=True, blank=True, null=True, default=None)
2009-07-13 12:32:01 +00:00
impawards_id = models.CharField(max_length=255, unique=True, blank=True, null=True, default=None)
2009-07-13 08:09:58 +00:00
#FIXME: look into other ids
#what about tv.com ids/urls for tv episodes
2009-07-13 12:32:01 +00:00
#movieposterdb can be mapped via imdbid and does not need to be saved here
2009-07-13 08:09:58 +00:00
def __unicode__(self):
2009-07-13 14:44:09 +00:00
id = self.imdb_id
if not id:
id = self.id
2009-07-13 14:44:09 +00:00
return '%s (%s)' % (self.title, id)
2009-07-13 08:09:58 +00:00
def updateFromWikipedia(self):
2009-07-13 12:32:01 +00:00
if self.wikipedia_id:
2009-07-14 16:34:49 +00:00
wikipedia_url = oxweb.wikipedia.getUrl(self.wikipedia_id)
2009-07-13 12:32:01 +00:00
data = oxweb.wikipedia.getMovieData(wikipedia_url)
2009-07-13 08:09:58 +00:00
_key = {}
2009-07-14 16:34:49 +00:00
for key in ('imdb_id', 'amg_id'):
2009-07-13 08:09:58 +00:00
if key in data:
2009-07-14 16:34:49 +00:00
if data[key] and not getattr(self, _key.get(key, key)):
2009-07-13 08:09:58 +00:00
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])
2009-07-14 16:34:49 +00:00
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])
2009-07-15 16:39:28 +00:00
if not self.wikipedia_id:
self.wikipedia_id = oxweb.wikipedia.getId(oxweb.wikipedia.getUrlByImdb(self.imdb_id))
2009-07-14 16:34:49 +00:00
if not self.oxdb_id:
self.gen_oxdb_id()
self.save()
2009-07-13 12:53:58 +00:00
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])
2009-07-13 12:53:58 +00:00
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()
2009-07-13 08:09:58 +00:00
def json(self):
2009-07-14 16:34:49 +00:00
json = {}
keys = [
'title',
'director',
'year',
'imdb_id',
'oxdb_id',
2009-07-17 15:17:50 +00:00
'amg_id',
2009-07-14 16:34:49 +00:00
'wikipedia_id',
2009-07-17 15:17:50 +00:00
'criterion_id',
2009-07-14 16:34:49 +00:00
'impawards_id',
]
if self.episode > -1:
keys += ['episode', 'season', 'episode_title', 'series_title']
for key in keys:
2009-07-13 08:09:58 +00:00
value = getattr(self, key)
if value:
json[key] = value
return simplejson.dumps(json, indent=4)
2009-07-13 12:32:01 +00:00
class Karagarga(models.Model):
movie_id = models.ForeignKey(MovieId, related_name='karagarga_ids', default=None)
2009-07-13 12:32:01 +00:00
karagarga_id = models.IntegerField(unique=True)
2009-07-15 16:39:28 +00:00
@classmethod
def maxId(cls):
return cls.objects.aggregate(Max('karagarga_id'))['karagarga_id__max']