oxdata/lookup/models.py
2009-07-13 10:29:42 +02:00

67 lines
2.4 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os.path
from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
import simplejson
import oxweb.wikipedia
def getMappingByImdb(imdb_id):
try:
m = IdMapping.objects.get(imdb_id=imdb_id)
except IdMapping.DoesNotExist:
m = IdMapping()
m.imdb_id = imdb_id
m.save()
return m
class IdMapping(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
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_url = 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)
movieposterdb_url = models.CharField(max_length=255, unique=True, blank=True, null=True, default=None)
impawards_url = models.CharField(max_length=255, unique=True, blank=True, null=True, default=None)
rottentomatoes_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
karagarga_id = models.IntegerField(unique=True, blank=True, null=True, default=None)
def __unicode__(self):
return self.imdb_id
def updateFromWikipedia(self):
if self.wikipedia_url:
data = oxweb.wikipedia.getMovieData(self.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 json(self):
json = {}
for key in ('imdb_id',
'amg_id',
'oxdb_id',
'wikipedia_url',
'movieposterdb_id',
'impawards_url',
'rottentomatoes_id'):
value = getattr(self, key)
if value:
json[key] = value
return simplejson.dumps(json, indent=4)