From e8be5a1f36de22d61b7911b2b68f83b25408f306 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 14 Jul 2009 19:52:09 +0200 Subject: [PATCH] get PosterUrls, this has to be moved somewhere else --- poster/models.py | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/poster/models.py b/poster/models.py index 4ca9a73..713f10f 100644 --- a/poster/models.py +++ b/poster/models.py @@ -8,7 +8,12 @@ from django.db.models import Q from django.contrib.auth.models import User from django.core.files.base import ContentFile -from oxdata.lookup.models import MovieId +import oxweb.criterion +import oxweb.movieposterdb +import oxweb.karagarga +import oxweb.imdb + +from oxdata.lookup.models import MovieId, Karagarga class Poster(models.Model): @@ -19,7 +24,7 @@ class Poster(models.Model): url = models.CharField(max_length=255) def poster_path(instance, filename): - id = instance.serviceId + id = instance.service_id return os.path.join('poster', instance.service, id[:1], id[:4], id, filename) class PosterCache(models.Model): @@ -29,14 +34,45 @@ class PosterCache(models.Model): movie_id = models.ForeignKey(MovieId, related_name='postercache') url = models.CharField(max_length=1024) service = models.CharField(max_length=255) - serviceId = models.CharField(max_length=42) + service_id = models.CharField(max_length=42) image = models.ImageField(max_length=255, upload_to=poster_path) - def download(self): + def get(self): if not self.image: import oxlib.net - name = "%s.jpg" % hashlib.sha1(self.url).hexdigest() + ext = 'jpg' + if self.url.endswith('.png'): + ext = 'png' + name = "%s.%s" % (hashlib.sha1(self.url).hexdigest(), ext) data = oxlib.net.getUrl(self.url) self.image.save(name, ContentFile(data)) + return self.image + + +def getPosterUrls(m): + def addPoster(url, service, service_id): + p = PosterCache(url=url, service=service, service_id=service_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) + 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)