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
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
import simplejson
|
|
|
|
import oxweb.wikipedia
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
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 id: id = self.id
|
|
|
|
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:
|
|
|
|
wikipedia_url = "http://en.wikipedia.org/wiki/%s" % self.wikipedia_id
|
|
|
|
data = oxweb.wikipedia.getMovieData(wikipedia_url)
|
2009-07-13 08:09:58 +00:00
|
|
|
_key = {}
|
|
|
|
for key in ('imdb_id',
|
|
|
|
'amg_id',
|
|
|
|
'rottentomatoes_id'):
|
|
|
|
if key in data:
|
|
|
|
if data[key]:
|
|
|
|
setattr(self, _key.get(key, key), data[key])
|
|
|
|
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.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()
|
|
|
|
|
2009-07-13 08:09:58 +00:00
|
|
|
def json(self):
|
|
|
|
json = {}
|
|
|
|
for key in ('imdb_id',
|
|
|
|
'amg_id',
|
|
|
|
'oxdb_id',
|
|
|
|
'wikipedia_url',
|
|
|
|
'impawards_url',
|
|
|
|
'rottentomatoes_id'):
|
|
|
|
value = getattr(self, key)
|
|
|
|
if value:
|
|
|
|
json[key] = value
|
|
|
|
return simplejson.dumps(json, indent=4)
|
|
|
|
|
2009-07-13 12:32:01 +00:00
|
|
|
def getMappingByKaragargaId(id):
|
|
|
|
kg = Karagarga.objects.get(imdb_id=imdb_id)
|
|
|
|
return kg.mapping
|
|
|
|
|
|
|
|
class Karagarga(models.Model):
|
|
|
|
mapping = models.ForeignKey(MovieId, related_name='karagarga_ids', default=None)
|
|
|
|
karagarga_id = models.IntegerField(unique=True)
|
|
|
|
|