示例#1
0
def update():
    flickr = FlickrClient(settings.FLICKR_API_KEY)
    
    # Preload the list of licenses
    licenses = licenses = flickr.photos.licenses.getInfo()
    licenses = dict((l["id"], smart_unicode(l["url"])) for l in licenses["licenses"]["license"])
    
    # Handle update by pages until we see photos we've already handled
    # FIXME!
    last_update_date = datetime.datetime(1969, 12, 31, 19, 0) 
    page = 1
    while True:
        #log.debug("Fetching page %s of photos", page)
        resp = flickr.groups.pools.getPhotos(group_id=settings.FLICKR_GROUP_ID, extras="license,date_taken", per_page="500", page=str(page))
        photos = resp["photos"]
        if page > photos["pages"]:
            #log.debug("Ran out of photos; stopping.")
            break
            
        for photodict in photos["photo"]:
            timestamp = parsers.parsedate(str(photodict["datetaken"]))
            if timestamp < last_update_date:
                #log.debug("Hit an old photo (taken %s; last update was %s); stopping.", timestamp, last_update_date)
                break
            
            photo_id = parsers.safeint(photodict["id"])
            license = licenses[photodict["license"]]
            secret = smart_unicode(photodict["secret"])
            _handle_photo(flickr, photo_id, secret, license, timestamp)
            
        page += 1
示例#2
0
def fetch_single_flickr_photo(photo_id, flickr_id, request):
    flickr = FlickrClient(settings.FLICKR_API_KEY)
    #log.debug('logged in user is %s.', request.user)

    licenses = licenses = flickr.photos.licenses.getInfo()
    licenses = dict((l["id"], smart_unicode(l["url"])) for l in licenses["licenses"]["license"])      

    resp = flickr.photos.getInfo(flickr_id=flickr_id, photo_id=photo_id, extras="license,date_taken")
        
    #timestamp = parsers.parsedate(resp["photo"]["dates"]["taken"])
    timestamp = datetime.datetime.now()
    user = request.user
    
    photo_id = parsers.safeint(resp["photo"]["id"])
    license = licenses[resp["photo"]["license"]]
    secret = smart_unicode(resp["photo"]["secret"])
    
    _handle_photo(flickr, photo_id, secret, license, timestamp, user)
    return Photo.objects.latest() #FIXME: concurrency problem?
示例#3
0
def _handle_photo(flickr, photo_id, secret, license, timestamp, user=None):
    info = flickr.photos.getInfo(photo_id=photo_id, secret=secret)["photo"]
    server_id = parsers.safeint(info["server"])
    farm_id = parsers.safeint(info["farm"])
    taken_by = smart_unicode(info["owner"]["username"])
    title = smart_unicode(info["title"]["_content"])
    description = smart_unicode(info["description"]["_content"])
    comment_count = parsers.safeint(info["comments"]["_content"])
    date_uploaded = datetime.datetime.fromtimestamp(parsers.safeint(info["dates"]["posted"]))
    date_lastupdate = datetime.datetime.fromtimestamp(parsers.safeint(info["dates"]["lastupdate"]))
    date_received = datetime.datetime.now()
    try:
        latitude      = float(info["location"]["latitude"])
    except KeyError: 
        latitude      = '42.39873'
        
    try:
        longitude     = float(info["location"]["longitude"])
    except KeyError:
        longitude     = '-71.10764'
    
    try:
        accuracy      = parsers.safeint(info["location"]["accuracy"])
    except KeyError:    
        accuracy      = None
    
    try:
        neighbourhood = smart_unicode(info["location"]["neighbourhood"]["_content"])
    except KeyError:
        neighbourhood = None
        
    #log.debug("Handling photo: %r (taken %s)" % (title, timestamp))
    #log.debug("User is: %s" % user)
    photo, created = Photo.objects.get_or_create(
        photo_id      = str(photo_id),
        defaults = dict(
            server_id     = server_id,
            farm_id       = farm_id,
            secret        = secret,
            taken_by      = taken_by,
            cc_license    = license,
            title         = title,
            description   = description,
            comment_count = comment_count,
            date_uploaded = date_uploaded,
            date_received  = date_received,
            latitude      = latitude,
            longitude     = longitude,
            accuracy      = accuracy,
            neighbourhood = neighbourhood,
        )
    )
    if created:
        photo.exif = _convert_exif(flickr.photos.getExif(photo_id=photo_id, secret=secret))
    else:
        photo.server_id     = server_id
        photo.farm_id       = farm_id
        photo.secret        = secret
        photo.taken_by      = taken_by
        photo.cc_license    = license
        photo.title         = title
        photo.description   = description
        photo.comment_count = comment_count
        photo.date_uploaded = date_uploaded
        photo.date_received  = date_received
        photo.latitude      = latitude
        photo.longitude     = longitude
        photo.accuracy      = accuracy
        photo.neighbourhood = neighbourhood
    
    photo.save()

    # only None when we run flickr.update()
    # in this case it will default to SCC admin user
    if user is None:
        return SharedItem.objects.create_or_update(
            instance = photo, 
            timestamp = date_received,
        )
    else:
        return SharedItem.objects.create_or_update(
            user = user,
            instance = photo, 
            timestamp = date_received,
        )
示例#4
0
def _handle_photo_with_geo(flickr, photo_id, secret, license, timestamp, geometry, user=None):
    info = flickr.photos.getInfo(photo_id=photo_id, secret=secret)["photo"]
    server_id = parsers.safeint(info["server"])
    farm_id = parsers.safeint(info["farm"])
    taken_by = smart_unicode(info["owner"]["username"])
    title = smart_unicode(info["title"]["_content"])
    description = smart_unicode(info["description"]["_content"])
    comment_count = parsers.safeint(info["comments"]["_content"])
    date_uploaded = datetime.datetime.fromtimestamp(parsers.safeint(info["dates"]["posted"]))
    date_lastupdate = datetime.datetime.fromtimestamp(parsers.safeint(info["dates"]["lastupdate"]))
    date_received = datetime.datetime.now()
    try:
        latitude      = float(info["location"]["latitude"])
    except KeyError: 
        latitude      = geometry.x
        
    try:
        longitude     = float(info["location"]["longitude"])
    except KeyError:
        longitude     = geometry.y
    
    try:
        accuracy      = parsers.safeint(info["location"]["accuracy"])
    except KeyError:    
        accuracy      = None
    
    try:
        neighbourhood = smart_unicode(info["location"]["neighbourhood"]["_content"])
    except KeyError:
        neighbourhood = None
        
    photo, created = Photo.objects.get_or_create(
        photo_id      = str(photo_id),
        defaults = dict(
            server_id     = server_id,
            farm_id       = farm_id,
            secret        = secret,
            taken_by      = taken_by,
            cc_license    = license,
            title         = title,
            description   = description,
            comment_count = comment_count,
            date_uploaded = date_uploaded,
            date_received  = date_received,
            latitude      = latitude,
            longitude     = longitude,
            accuracy      = accuracy,
            neighbourhood = neighbourhood,
        )
    )
    if created:
        photo.exif = _convert_exif(flickr.photos.getExif(photo_id=photo_id, secret=secret))
    else:
        photo.server_id     = server_id
        photo.farm_id       = farm_id
        photo.secret        = secret
        photo.taken_by      = taken_by
        photo.cc_license    = license
        photo.title         = title
        photo.description   = description
        photo.comment_count = comment_count
        photo.date_uploaded = date_uploaded
        photo.date_received  = date_received
        photo.latitude      = latitude
        photo.longitude     = longitude
        photo.accuracy      = accuracy
        photo.neighbourhood = neighbourhood
    
    photo.save()
    
    if user is None:
        return SharedItem.objects.create_or_update(
            instance = photo, 
            timestamp = date_received,
        )
    else:
        return SharedItem.objects.create_or_update(
            user = user,
            instance = photo, 
            timestamp = date_received,
        )