oxdata/movieposterdb/cache.py

41 lines
980 B
Python
Raw Normal View History

2009-07-13 08:09:58 +00:00
# -*- coding: UTF-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import os
import hashlib
from django.conf import settings
from oxlib.cache import getUrlUnicode
from oxlib import findRe
import oxlib.net
import oxweb.movieposterdb
def getPoster(id, url=''):
if not url:
urls = oxweb.movieposterdb.getPosterUrls(id)
if urls:
url = urls[0]
else:
return
dirname = os.path.join(settings.DATA_ROOT, 'movieposterdb.com', id[:1], id[:4], id)
url_hash = hashlib.sha1(url).hexdigest()
filename = os.path.join(dirname, '%s.jpg' % url_hash)
filename = os.path.normpath(filename)
if not os.path.exists(filename):
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):
return
def cron():
archivePosters()
def init():
archivePosters(True)