| 1 | import os |
|---|
| 2 | import logging |
|---|
| 3 | import cached_url, thumbnail |
|---|
| 4 | from urllib2 import urlopen |
|---|
| 5 | from StringIO import StringIO |
|---|
| 6 | |
|---|
| 7 | #logging.basicConfig(level=logging.DEBUG, |
|---|
| 8 | # format='%(asctime)s %(levelname)s %(message)s', |
|---|
| 9 | # filename='cached.log', |
|---|
| 10 | # filemode='w') |
|---|
| 11 | |
|---|
| 12 | console = logging.StreamHandler() |
|---|
| 13 | console.setLevel(logging.DEBUG) |
|---|
| 14 | formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') |
|---|
| 15 | console.setFormatter(formatter) |
|---|
| 16 | logging.getLogger('').addHandler(console) |
|---|
| 17 | |
|---|
| 18 | def get_content(url, width=None, height=None, scale=None, control=False): |
|---|
| 19 | """get cached content""" |
|---|
| 20 | key = '%s::%s::%s::%s' % (url, str(height), str(width), str(scale)) |
|---|
| 21 | content = cached_url.mc.get(key) |
|---|
| 22 | if content is not None: |
|---|
| 23 | logging.debug('%s cached' % key) |
|---|
| 24 | return content |
|---|
| 25 | infos, content = cached_url.get_content(url, control) |
|---|
| 26 | try: |
|---|
| 27 | content = thumbnail.generate(StringIO(content), width, height, scale) |
|---|
| 28 | except IOError: |
|---|
| 29 | content = None |
|---|
| 30 | cached_url.mc.set(key, (infos, content)) |
|---|
| 31 | logging.debug('%s caching' % key) |
|---|
| 32 | return infos, content |
|---|