oxdata/criterion/cache.py
2009-07-13 14:32:01 +02:00

49 lines
1.3 KiB
Python

# -*- coding: UTF-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
from django.conf import settings
from oxlib.cache import getUrlUnicode
from oxlib import findRe
import oxlib.net
import oxweb.criterion
from oxdata.lookup.models import MovieId, getMovieIdByImdbId
def getPoster(id, url=None):
dirname = os.path.join(settings.DATA_ROOT, 'criterion.com', id)
filename = os.path.join(dirname, 'poster.jpg')
filename = os.path.normpath(filename)
if not os.path.exists(filename):
if not url:
data = oxweb.criterion.getData(id)
url = data['posterUrl']
oxlib.net.saveUrl(url, filename)
return filename
def archivePosters(init=False):
for criterionId in oxweb.criterion.getIds():
data = oxweb.criterion.getData(criterionId)
imdbId = data['imdbId']
try:
m = MovieId.objects.get(criterion_id=criterionId)
if imdbId and not m.imdb_id:
m.imdb_id = imdbId
m.save()
except MovieId.DoesNotExist:
if imdbId:
m = getMovieIdByImdbId(imdbId)
else:
m = MovieId()
m.criterion_id = criterionId
m.save()
url = data['posterUrl']
getPoster(criterionId, url)
def cron():
archivePosters()
def init():
archivePosters(True)