Esempio n. 1
0
def _write_instagram_photo_primary(photo, ride):
    """
    Writes an instagram primary photo to db.

    :param photo: The primary photo from an activity.
    :type photo: stravalib.model.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.model.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.model.RidePhoto
    """
    # Here is when we have an Instagram photo as primary:
    #  u'photos': {u'count': 1,
    #   u'primary': {u'id': 106409096,
    #    u'source': 2,
    #    u'unique_id': None,
    #    u'urls': {u'100': u'https://instagram.com/p/88qaqZvrBI/media?size=t',
    #     u'600': u'https://instagram.com/p/88qaqZvrBI/media?size=l'}},
    #   u'use_prima ry_photo': False},

    insta_client = insta.configured_instagram_client()
    shortcode = re.search(r'/p/([^/]+)/', photo.urls['100']).group(1)

    media = None
    try:
        #log.debug("Fetching Instagram media for shortcode: {}".format(shortcode))
        media = insta_client.media_shortcode(shortcode)
    except (InstagramAPIError, InstagramClientError) as e:
        if e.status_code == 400:
            log.warning("Instagram photo {} for ride {}; user is set to private".format(shortcode, ride))
        elif e.status_code == 404:
            log.warning("Photo {} for ride {}; shortcode not found".format(shortcode, ride))
        else:
            log.exception("Error fetching instagram photo {}".format(photo))

    p = RidePhoto()

    if media:
        p.id = media.id
        p.ref = media.link
        p.img_l = media.get_standard_resolution_url()
        p.img_t = media.get_thumbnail_url()
        if media.caption:
            p.caption = media.caption.text
    else:
        p.id = photo.id
        p.ref = re.match(r'(.+/)media\?size=.$', photo.urls['100']).group(1)
        p.img_l = photo.urls['600']
        p.img_t = photo.urls['100']

    p.ride_id = ride.id
    p.primary = True
    p.source = photo.source

    log.debug("Writing (primary) Instagram ride photo: {!r}".format(p))

    db.session.add(p)
    db.session.flush()

    return p
Esempio n. 2
0
def _write_strava_photo_primary(photo, ride):
    """
    Writes a strava native (source=1) primary photo to db.

    :param photo: The primary photo from an activity.
    :type photo: stravalib.model.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.model.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.model.RidePhoto
    """
    # 'photos': {u'count': 1,
    #   u'primary': {u'id': None,
    #    u'source': 1,
    #    u'unique_id': u'35453b4b-0fc1-46fd-a824-a4548426b57d',
    #    u'urls': {u'100': u'https://dgtzuqphqg23d.cloudfront.net/Vvm_Mcfk1SP-VWdglQJImBvKzGKRJrHlNN4BqAqD1po-128x96.jpg',
    #     u'600': u'https://dgtzuqphqg23d.cloudfront.net/Vvm_Mcfk1SP-VWdglQJImBvKzGKRJrHlNN4BqAqD1po-768x576.jpg'}},
    #   u'use_primary_photo': False},

    if not photo.urls:
        log.warning("Photo {} present, but has no URLs (skipping)".format(photo))
        return None

    p = RidePhoto()
    p.id = photo.unique_id
    p.primary = True
    p.source = photo.source
    p.ref = None
    p.img_l = photo.urls['600']
    p.img_t = photo.urls['100']
    p.ride_id = ride.id

    log.debug("Writing (primary) Strava ride photo: {}".format(p))

    db.session.add(p)
    db.session.flush()
    return p
Esempio n. 3
0
def _write_strava_photo_primary(photo, ride):
    """
    Writes a strava native (source=1) primary photo to db.

    :param photo: The primary photo from an activity.
    :type photo: stravalib.model.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.model.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.model.RidePhoto
    """
    # 'photos': {u'count': 1,
    #   u'primary': {u'id': None,
    #    u'source': 1,
    #    u'unique_id': u'35453b4b-0fc1-46fd-a824-a4548426b57d',
    #    u'urls': {u'100': u'https://dgtzuqphqg23d.cloudfront.net/Vvm_Mcfk1SP-VWdglQJImBvKzGKRJrHlNN4BqAqD1po-128x96.jpg',
    #     u'600': u'https://dgtzuqphqg23d.cloudfront.net/Vvm_Mcfk1SP-VWdglQJImBvKzGKRJrHlNN4BqAqD1po-768x576.jpg'}},
    #   u'use_primary_photo': False},

    if not photo.urls:
        log.warning("Photo {} present, but has no URLs (skipping)".format(photo))
        return None

    p = RidePhoto()
    p.id = photo.unique_id
    p.primary = True
    p.source = photo.source
    p.ref = None
    p.img_l = photo.urls['600']
    p.img_t = photo.urls['100']
    p.ride_id = ride.id

    log.debug("Writing (primary) Strava ride photo: {}".format(p))

    db.session.add(p)
    db.session.flush()
    return p
Esempio n. 4
0
def _write_instagram_photo_primary(photo, ride):
    """
    Writes an instagram primary photo to db.

    :param photo: The primary photo from an activity.
    :type photo: stravalib.model.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.model.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.model.RidePhoto
    """
    # Here is when we have an Instagram photo as primary:
    #  u'photos': {u'count': 1,
    #   u'primary': {u'id': 106409096,
    #    u'source': 2,
    #    u'unique_id': None,
    #    u'urls': {u'100': u'https://instagram.com/p/88qaqZvrBI/media?size=t',
    #     u'600': u'https://instagram.com/p/88qaqZvrBI/media?size=l'}},
    #   u'use_prima ry_photo': False},


    media = None

    # This doesn't work any more; Instagram changed their API to use OAuth.
    #insta_client = insta.configured_instagram_client()
    #shortcode = re.search(r'/p/([^/]+)/', photo.urls['100']).group(1)
    # try:
    #     #log.debug("Fetching Instagram media for shortcode: {}".format(shortcode))
    #     media = insta_client.media_shortcode(shortcode)
    # except (InstagramAPIError, InstagramClientError) as e:
    #     if e.status_code == 400:
    #         log.warning("Instagram photo {} for ride {}; user is set to private".format(shortcode, ride))
    #     elif e.status_code == 404:
    #         log.warning("Photo {} for ride {}; shortcode not found".format(shortcode, ride))
    #     else:
    #         log.exception("Error fetching instagram photo {}".format(photo))

    p = RidePhoto()

    if media:
        p.id = media.id
        p.ref = media.link
        p.img_l = media.get_standard_resolution_url()
        p.img_t = media.get_thumbnail_url()
        if media.caption:
            p.caption = media.caption.text
    else:
        p.id = photo.id
        p.ref = re.match(r'(.+/)media\?size=.$', photo.urls['100']).group(1)
        p.img_l = photo.urls['600']
        p.img_t = photo.urls['100']

    p.ride_id = ride.id
    p.primary = True
    p.source = photo.source

    log.debug("Writing (primary) Instagram ride photo: {!r}".format(p))

    db.session.add(p)
    db.session.flush()

    return p