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)