-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetphotos.py
42 lines (37 loc) · 1.13 KB
/
getphotos.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
from PIL import Image
import urllib2
import os
from sakura import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'sakura.settings'
from tweets.models import Photo
photos = Photo.objects.filter(converted=False)
for idx, photo in enumerate(photos):
if idx < 0:
continue
url = photo.origin_path
error = False
for i in range(3):
try:
res = urllib2.urlopen(url)
break
except urllib2.URLError:
if i < 2:
pass
else:
print 'cannot get a picture: ', url
error = True
if error:
continue
try:
origin = open('static/photos/tmp.jpg', 'wb')
origin.write(res.read())
origin.close()
original = Image.open('static/photos/tmp.jpg')
original.thumbnail((360, original.size[1]), Image.ANTIALIAS)
saved_name = 'photos/%s.jpg' % (photo.id)
original.save('static/' + saved_name)
photo.converted_path = settings.STATIC_URL + saved_name
photo.converted = True
photo.save()
except IOError:
print 'cannot create thumbnail for ', url