oxdata/poster/models.py
2009-07-15 20:19:56 +02:00

115 lines
4.1 KiB
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os.path
import hashlib
from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
import oxweb.criterion
import oxweb.movieposterdb
import oxweb.karagarga
import oxweb.imdb
from oxdata.lookup.models import MovieId, Karagarga
def getPosters(movie_id):
getPosterUrls(movie_id)
posters = {}
for p in PosterCache.objects.all().filter(movie_id=movie_id):
if p.site not in posters:
posters[p.site] = []
poster = p.get()
if poster:
pjson = {}
pjson['url'] = poster._get_url()
pjson['width'] = poster.width
pjson['height'] = poster.height
posters[p.site].append(pjson)
for p in posters:
if not posters[p]:
del posters[p]
return posters
def poster_path(url, filename):
url_hash = hashlib.sha1(url).hexdigest()
ext = 'jpg'
if filename.endswith('.png'):
ext = 'png'
name = "%s.%s" % (url_hash, ext)
return os.path.join('posters', url_hash[:2], url_hash[2:4], url_hash[4:6], name)
class PosterCache(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
movie_id = models.ForeignKey(MovieId, related_name='postercache')
url = models.CharField(max_length=1024)
site = models.CharField(max_length=255)
site_id = models.CharField(max_length=42)
image = models.ImageField(max_length=255, upload_to=lambda i, f: poster_path(i.url, f))
failed = models.BooleanField(default=False)
def get(self):
if not self.image and not self.failed:
try:
import oxlib.net
name = hashlib.sha1(self.url).hexdigest()
data = oxlib.net.getUrl(self.url)
self.image.save(name, ContentFile(data))
except:
self.failed = True
self.save()
return self.image
class Poster(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
movie_id = models.ForeignKey(MovieId, related_name='poster')
poster = models.ForeignKey(PosterCache, default=None, blank=True)
upload = models.ImageField(max_length=255, upload_to=lambda i, f: poster_path('upload/%s' % i.id, f))
oxdb = models.ImageField(max_length=255, upload_to=lambda i, f: poster_path('oxdb/%s' % i.id, f))
def get(self):
if self.image:
return self.image
if self.poster:
return self.poster.get()
#FIXME: select poster
return None
def getPosterUrls(m):
def addPoster(url, site, site_id):
p = PosterCache(url=url, site=site, site_id=site_id, movie_id=m)
p.save()
if m.imdb_id:
poster = oxweb.imdb.getMoviePoster(m.imdb_id)
if poster:
if PosterCache.objects.all().filter(url=poster).count() == 0:
addPoster(poster, 'imdb.com', m.imdb_id)
for poster in oxweb.movieposterdb.getData(m.imdb_id)['posters']:
if PosterCache.objects.all().filter(url=poster).count() == 0:
addPoster(poster, 'movieposterdb.com', m.imdb_id)
if m.criterion_id:
for poster in oxweb.criterion.getData(m.criterion_id)['posters']:
if PosterCache.objects.all().filter(url=poster).count() == 0:
addPoster(poster, 'criterion.com', m.criterion_id)
if m.wikipedia_id:
poster = oxweb.wikipedia.getPosterUrl(m.wikipedia_id)
if poster:
if PosterCache.objects.all().filter(url=poster).count() == 0:
addPoster(poster, 'wikipedia.org', m.wikipedia_id)
for kg in Karagarga.objects.all().filter(movie_id=m):
data = oxweb.karagarga.getData(kg.karagarga_id)
if data:
for poster in data['posters']:
if PosterCache.objects.all().filter(url=poster).count() == 0:
addPoster(poster, 'karagarga.net', kg.karagarga_id)
else:
kg.delete()