110 lines
4.1 KiB
Python
110 lines
4.1 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
|
|
from django.contrib.auth.models import User
|
|
|
|
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()
|
|
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 = "http://en.wikipedia.org/wiki/%s" % self.wikipedia_id
|
|
data = oxweb.wikipedia.getMovieData(wikipedia_url)
|
|
_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()
|
|
|
|
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 = oxweb.imdb.getMovieCredits(self.imdb_id)['directors']
|
|
self.director = ', '.join([stripTags(d[0]) for d in directors])
|
|
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 = {}
|
|
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)
|
|
|
|
class Karagarga(models.Model):
|
|
movie_id = models.ForeignKey(MovieId, related_name='karagarga_ids', default=None)
|
|
karagarga_id = models.IntegerField(unique=True)
|
|
|