-
Notifications
You must be signed in to change notification settings - Fork 11
/
image_fetcher.py
75 lines (64 loc) · 2.37 KB
/
image_fetcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# image_fetcher.py
# Copyright (C) 2011 Simon Newton
# Handles fetching images from remote sites.
from __future__ import with_statement
import logging
from google.appengine.api import files
from google.appengine.api import images
from google.appengine.api import urlfetch
class ImageFetcher(object):
"""Return the image for a model.
TODO(simon): this is fine for now but we should switch to using task queues
or map reduce so the fetches aren't in the serving path.
"""
# width to resize images to
RESIZE_WIDTH = 200
def __init__(self):
pass
def FetchAndSaveImage(self, url):
"""
Args:
url: The url to fetch.
Returns:
The new blob key, or None if the fetch failed.
"""
logging.info('fetching %s' % url)
try:
image_response = urlfetch.fetch(url)
except urlfetch.Error:
return None
if image_response.status_code != 200:
logging.info('image fetch failed. %s -> %d' %
(url, image_response.status_code))
return None
img = images.Image(image_response.content)
# shrink things if needed
if img.width > self.RESIZE_WIDTH:
img.resize(width=self.RESIZE_WIDTH)
# we need at least one transform, so use I'm feeling lucky :)
img.im_feeling_lucky()
try:
thumbnail = img.execute_transforms(output_encoding=images.PNG)
except SystemError, e:
# this fires on the dev server if it can't load the PIL module
logging.info(e)
return None
file_name = files.blobstore.create(mime_type='image/png')
with files.open(file_name, 'a') as f:
f.write(thumbnail)
files.finalize(file_name)
return files.blobstore.get_blob_key(file_name)