add some cache data
This commit is contained in:
parent
86ab85024d
commit
294dbc8bac
11 changed files with 279 additions and 9 deletions
55
criterion/cache.py
Normal file
55
criterion/cache.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# -*- 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 IdMapping
|
||||
|
||||
|
||||
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']
|
||||
if not os.path.exists(dirname):
|
||||
os.makedirs(dirname)
|
||||
data = oxlib.net.getUrl(url)
|
||||
f = open(filename, 'w')
|
||||
f.write(data)
|
||||
f.close()
|
||||
return filename
|
||||
|
||||
def archivePosters(init=False):
|
||||
for criterionId in oxweb.criterion.getIds():
|
||||
try:
|
||||
m = IdMapping.objects.get(criterion_id=criterionId)
|
||||
except IdMapping.DoesNotExist:
|
||||
data = oxweb.criterion.getData(criterionId)
|
||||
imdbId = data['imdbId']
|
||||
if imdbId:
|
||||
try:
|
||||
m = IdMapping.objects.get(imdb_id=imdbId)
|
||||
except IdMapping.DoesNotExist:
|
||||
m = IdMapping()
|
||||
m.imdb_id = imdbId
|
||||
else:
|
||||
m = IdMapping()
|
||||
m.criterion_id = criterionId
|
||||
m.save()
|
||||
url = data['posterUrl']
|
||||
getPoster(criterionId, url)
|
||||
|
||||
def cron():
|
||||
archivePosters()
|
||||
|
||||
def init():
|
||||
archivePosters(True)
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue