コード例 #1
0
ファイル: views.py プロジェクト: dbdeveloper/i-am-cc
def save_image_info(api_result, info):
    if InstagramPhoto.objects.filter(photo_id=api_result.get('id')):
        # Photo already exists, let's skip it
        # TODO: In the future perhaps we can allow re-licensing of
        # existing photos.
        return
    caption = api_result.get('caption')
    if caption:
        caption = caption.get('text', None)
    photo = InstagramPhoto(
        license_info=info,
        caption=caption,
        created_time=from_unix_time(int(api_result.get('created_time'))),
        filter=api_result.get('filter'),
        photo_id=api_result.get('id'),
        image_low_resolution=api_result.get('images', {}
            )['low_resolution']['url'],
        image_standard_resolution=api_result.get('images', {}
            )['standard_resolution']['url'],
        image_thumbnail=api_result.get('images', {}
            )['thumbnail']['url'],
        link=api_result.get('link'),
        tags=json.dumps(api_result.get('tags')),
        location=json.dumps(api_result.get('location')),
    )
    photo.save()
コード例 #2
0
ファイル: views.py プロジェクト: dbdeveloper/i-am-cc
def generate_image_info(username=None, limit=None):
    d = {}
    if username:
        d = {'instagram_username': username}
    # last_used_in_api helps us limit the # of API calls
    infos = InstagramInfo.objects.filter(**d).exclude(end_date=None).\
        order_by('last_used_in_api')[:MAX_API_PER_GENERATION]

    for info in infos:
        last_saved = info.start_date
        if InstagramPhoto.objects.filter(license_info=info):
            latest_saved_photo = InstagramPhoto.objects.filter(
                license_info=info).order_by('-created_time')[0]
            last_saved = latest_saved_photo.created_time

        recent = cache.get('api_rc_%s' % info.instagram_id)
        if recent is None:
            # Get the most recent since we last cached from the API
            try:
               recent_resp = instagram.api.users(info.instagram_id).media.\
                   recent.get(
                       access_token=get_access_token(info.user),
                       max_timestamp=to_unix_time(info.end_date),
                       min_timestamp=to_unix_time(last_saved))
            except:
                return
            recent = recent_resp['data']
            # One hour cache per-user
            cache.set('api_rc_%s' % info.instagram_id, recent, 60 * 60)

        for item in recent:
            # The API returns items even if they're before min_timestamp
            # sometimes, so we have to check by hand here.
            created_time = from_unix_time(int(item['created_time']))
            if (created_time < info.end_date and
                created_time > info.start_date and created_time > last_saved):
                save_image_info(item, info)

        info.last_used_in_api = datetime.now().replace(tzinfo=utc)
        info.save()