summaryrefslogtreecommitdiff
path: root/app/photos/retriever.py
blob: 1a0757e3d50689b22d7a522c5abf177f836cd4f3 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import datetime
import os
import io
import urllib.request
import urllib.parse
import urllib.error

from django.template.defaultfilters import slugify
from django.core.exceptions import ObjectDoesNotExist
from django.utils.encoding import force_text
from django.conf import settings

from photos.models import Photo, PhotoGallery

# from https://github.com/alexis-mignon/python-flickr-api
# terribly documented, but offers a good clean OOP approach if you're willing to figure it out...
import flickr_api

# Required PIL classes may or may not be available from the root namespace
# depending on the installation
try:
    import Image
    import ImageFile
except ImportError:
    try:
        from PIL import Image
        from PIL import ImageFile
    except ImportError:
        raise ImportError("Could not import the Python Imaging Library.")

ImageFile.MAXBLOCK = 1000000

EXIF_PARAMS = {
    "FNumber": 'f/2.8',
    "Make": 'Apple',
    "Model": 'iPhone',
    "ExposureTime": '',
    "ISO": '',
    "FocalLength": '',
    "LensModel": '',
    'DateTimeOriginal': '2013:09:03 22:44:25'
}


def sync_flickr_photos(*args, **kwargs):
    flickr_api.set_keys(api_key=settings.FLICKR_API_KEY, api_secret=settings.FLICKR_API_SECRET)
    flickr_api.set_auth_handler("app/photos/flickrauth")
    user = flickr_api.test.login()
    photos = user.getPhotos(extras="date_upload,date_taken,geo")
    # reverse! reverse!
    photos.reverse()
    for photo in photos:
        info = photo.getInfo()
        try:
            row = Photo.objects.get(flickr_id=info['id'], flickr_secret=info['secret'])
            print(('already have ' + info['id'] + ' moving on'))
        except ObjectDoesNotExist:
            get_photo(photo)


def get_photo(photo):
    info = photo.getInfo()
    geo = photo.getLocation()
    location, region = get_geo(float(geo['latitude']), float(geo['longitude']))
    exif = exif_handler(photo.getExif())
    p, created = Photo.objects.get_or_create(
        title=info['title'],
        flickr_id=info['id'],
        flickr_owner=info['owner']['id'],
        flickr_server=info['server'],
        flickr_secret=info['secret'],
        flickr_originalsecret=info['originalsecret'],
        flickr_farm=info['farm'],
        pub_date=flickr_datetime_to_datetime(info['taken']),
        description=info['description'],
        exif_aperture=exif['FNumber'],
        exif_make=exif['Make'],
        exif_model=exif['Model'],
        exif_exposure=exif['ExposureTime'],
        exif_iso=exif['ISO'],
        exif_lens=exif['LensModel'],
        exif_focal_length=exif['FocalLength'],
        exif_date=flickr_datetime_to_datetime(exif["DateTimeOriginal"].replace(':', '-', 2)),
        lat=float(geo['latitude']),
        lon=float(geo['longitude']),
        region=region,
        location=location,
    )
    if created:
        for tag in info['tags']:
            p.tags.add(tag['raw'])
    p.save()
    make_local_copies(p)
    #retina image:
    #slideshow_image(p, 2000, 1600, 75)
    #normal image
    print((p.title))
    return p


def sync_sets(*args, **kwargs):
    flickr_api.set_keys(api_key=settings.FLICKR_API_KEY, api_secret=settings.FLICKR_API_SECRET)
    flickr_api.set_auth_handler("app/photos/flickrauth")
    user = flickr_api.test.login()
    photosets = user.getPhotosets()
    # reverse! reverse!
    photosets.reverse()
    disregard = [
        'POTD 2008',
        'Snow Day',
        'Wedding',
        'Some random stuff',
        'Lilah & Olivia',
        '6 months+',
        '6-9 months',
        '9-18 months',
    ]
    for photoset in photosets:
        if photoset['title'] in disregard:
            pass
        else:
            try:
                row = PhotoGallery.objects.get(set_id__exact=photoset['id'])
                print(('%s %s %s' % ('already have', row.set_title, 'moving on...')))
                # okay it already exists, but is it up-to-date?
                #get_photos_in_set(row,set.id)
            except ObjectDoesNotExist:
                s = PhotoGallery.objects.create(
                    set_id=force_text(photoset['id']),
                    set_title=force_text(photoset['title']),
                    set_desc=force_text(photoset['description']),
                    set_slug=slugify(force_text(photoset['title'])),
                    primary=force_text(photoset['primary']),
                    pub_date=datetime.datetime.fromtimestamp(float(photoset['date_create']))
                )

                get_photos_in_set(photoset, s)
                #create the gallery thumbnail image:
                photo = Photo.objects.get(flickr_id__exact=str(photoset['primary']))
                make_gallery_thumb(photo, s)


def get_photos_in_set(flickr_photoset, photoset):
    for photo in flickr_photoset.getPhotos():
        try:
            p = Photo.objects.get(flickr_id__exact=str(photo['id']))
        except ObjectDoesNotExist:
            p = get_photo(photo)
        if p.is_public:
            photoset.photos.add(p)
            slideshow_image(p, 1000, 800, 95)


################################################
## Various meta data and geo helper functions ##
################################################


def exif_handler(data):
    converted = {}
    try:
        for t in data:
            converted[t['tag']] = t['raw']
    except:
        pass
    for k, v in list(EXIF_PARAMS.items()):
        if k not in converted:
            converted[k] = v
    return converted


def flickr_datetime_to_datetime(fdt):
    from datetime import datetime
    from time import strptime
    date_parts = strptime(fdt, '%Y-%m-%d %H:%M:%S')
    return datetime(*date_parts[0:6])


def get_geo(lat, lon):
    from locations.models import Location, Region
    from django.contrib.gis.geos import Point
    pnt_wkt = Point(lon, lat)
    try:
        location = Location.objects.get(geometry__contains=pnt_wkt)
    except Location.DoesNotExist:
        location = None
    try:
        region = Region.objects.get(geometry__contains=pnt_wkt)
    except Region.DoesNotExist:
        region = None
    return location, region


#######################################################################
## Photo retrieval functions to pull down images from Flickr servers ##
#######################################################################


def slideshow_image(photo, max_width, max_height, quality):
    slide_dir = settings.IMAGES_ROOT + '/slideshow/' + photo.pub_date.strftime("%Y")
    if not os.path.isdir(slide_dir):
        os.makedirs(slide_dir)

    # Is it a retina image or not?
    if max_width >= 1001 or max_height >= 801:
        filename = '%s/%sx2.jpg' % (slide_dir, photo.flickr_id)
    else:
        filename = '%s/%s.jpg' % (slide_dir, photo.flickr_id)

    flickr_photo = photo.get_original_url()
    fname = urllib.request.urlopen(flickr_photo)
    im = io.StringIO(fname.read())  # constructs a StringIO holding the image
    img = Image.open(im)
    cur_width, cur_height = img.size
    #if image landscape
    if cur_width > cur_height:
        new_width = max_width
        #check to make sure we aren't upsizing
        if cur_width > new_width:
            ratio = float(new_width) / cur_width
            x = (cur_width * ratio)
            y = (cur_height * ratio)
            resized = img.resize((int(x), int(y)), Image.ANTIALIAS)
            resized.save(filename, 'JPEG', quality=quality, optimize=True)
        else:
            img.save(filename)
    else:
        #image portrait
        new_height = max_height
        #check to make sure we aren't upsizing
        if cur_height > new_height:
            ratio = float(new_height) / cur_height
            x = (cur_width * ratio)
            y = (cur_height * ratio)
            resized = img.resize((int(x), int(y)), Image.ANTIALIAS)
            resized.save(filename, 'JPEG', quality=quality, optimize=True)
        else:
            img.save(filename)
    photo.slideshowimage_width = photo.get_width
    photo.slideshowimage_height = photo.get_height
    photo.slideshowimage_margintop = photo.get_margin_top
    photo.slideshowimage_marginleft = photo.get_margin_left
    photo.save()
    #now resize the local copy


def make_local_copies(photo):
    orig_dir = settings.IMAGES_ROOT + '/flickr/full/' + photo.pub_date.strftime("%Y")
    if not os.path.isdir(orig_dir):
        os.makedirs(orig_dir)
    full = photo.get_original_url()
    fname = urllib.request.urlopen(full)
    im = io.StringIO(fname.read())  # constructs a StringIO holding the image
    img = Image.open(im)
    local_full = '%s/%s.jpg' % (orig_dir, photo.flickr_id)
    img.save(local_full)
    #save large size
    large_dir = settings.IMAGES_ROOT + '/flickr/large/' + photo.pub_date.strftime("%Y")
    if not os.path.isdir(large_dir):
        os.makedirs(large_dir)
    large = photo.get_large_url()
    fname = urllib.request.urlopen(large)
    im = io.StringIO(fname.read())  # constructs a StringIO holding the image
    img = Image.open(im)
    local_large = '%s/%s.jpg' % (large_dir, photo.flickr_id)
    if img.format == 'JPEG':
        img.save(local_large)
    #save medium size
    med_dir = settings.IMAGES_ROOT + '/flickr/med/' + photo.pub_date.strftime("%Y")
    if not os.path.isdir(med_dir):
        os.makedirs(med_dir)
    med = photo.get_medium_url()
    fname = urllib.request.urlopen(med)
    im = io.StringIO(fname.read())  # constructs a StringIO holding the image
    img = Image.open(im)
    local_med = '%s/%s.jpg' % (med_dir, photo.flickr_id)
    img.save(local_med)


def make_gallery_thumb(photo, set):
    crop_dir = settings.IMAGES_ROOT + '/gallery_thumbs/'
    if not os.path.isdir(crop_dir):
        os.makedirs(crop_dir)
    remote = photo.get_original_url()
    print(remote)
    fname = urllib.request.urlopen(remote)
    im = io.StringIO(fname.read())  # constructs a StringIO holding the image
    img = Image.open(im)
    #calculate crop:
    cur_width, cur_height = img.size
    new_width, new_height = 291, 350
    ratio = max(float(new_width) / cur_width, float(new_height) / cur_height)
    x = (cur_width * ratio)
    y = (cur_height * ratio)
    xd = abs(new_width - x)
    yd = abs(new_height - y)
    x_diff = int(xd / 2)
    y_diff = int(yd / 2)
    box = (int(x_diff), int(y_diff), int(x_diff + new_width), int(y_diff + new_height))

    # create resized file
    resized = img.resize((int(x), int(y)), Image.ANTIALIAS).crop(box)
    # save resized file
    resized_filename = '%s/%s.jpg' % (crop_dir, set.id)
    try:
        if img.format == 'JPEG':
            resized.save(resized_filename, 'JPEG', quality=95, optimize=True)
        else:
            resized.save(resized_filename)
    except IOError as e:
        if os.path.isfile(resized_filename):
            os.unlink(resized_filename)
        raise e
    # os.unlink(img)