Exemplo n.º 1
0
    def _make_photo_from_native(self, photo: ActivityPhotoPrimary,
                                ride: Ride) -> Optional[RidePhoto]:
        """
        Writes a data native (source=1) primary photo to db.

        :param photo: The primary photo from an activity.
        :param ride: The db model object for ride.
        :return: The newly added ride photo object.
        """
        # '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:
            self.logger.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

        self.logger.debug("Creating (primary) native ride photo: {}".format(p))

        return p
Exemplo n.º 2
0
    def _make_photo_from_instagram(self, photo: ActivityPhotoPrimary,
                                   ride: Ride) -> Optional[RidePhoto]:
        """
        Writes an instagram primary photo to db.

        :param photo: The primary photo from an activity.
        :param ride: The db model object for ride.
        :return: The newly added ride photo object.
        """

        # 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},

        p = RidePhoto()
        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

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

        return p
Exemplo 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.orm.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.orm.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.orm.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))

    meta.scoped_session().add(p)
    meta.scoped_session().flush()
    return p
Exemplo n.º 4
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.orm.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.orm.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.orm.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))

    meta.scoped_session().add(p)
    meta.scoped_session().flush()
    return p
Exemplo n.º 5
0
    def write_ride_photos_nonprimary(self, activity_photos, ride):
        """
        Writes out non-primary photos (currently only instagram) associated with a ride to the database.

        :param activity_photos: Photos for an activity.
        :type activity_photos: list[stravalib.orm.ActivityPhoto]

        :param ride: The db model object for ride.
        :type ride: bafs.orm.Ride
        """
        # [{u'activity_id': 414980300,
        #   u'activity_name': u'Pimmit Run CX',
        #   u'caption': u'Pimmit Run cx',
        #   u'created_at': u'2015-10-17T20:51:02Z',
        #   u'created_at_local': u'2015-10-17T16:51:02Z',
        #   u'id': 106409096,
        #   u'ref': u'https://instagram.com/p/88qaqZvrBI/',
        #   u'resource_state': 2,
        #   u'sizes': {u'0': [150, 150]},
        #   u'source': 2,
        #   u'type': u'InstagramPhoto',
        #   u'uid': u'1097938959360503880_297644011',
        #   u'unique_id': None,
        #   u'uploaded_at': u'2015-10-17T17:55:45Z',
        #   u'urls': {u'0': u'https://instagram.com/p/88qaqZvrBI/media?size=t'}}]

        raise NotImplementedError("This needs to be sorted out.")

        meta.engine.execute(
            RidePhoto.__table__.delete().where(
                and_(RidePhoto.ride_id == ride.id, RidePhoto.primary == False)
            )
        )

        # insta_client = insta.configured_instagram_client()

        for activity_photo in activity_photos:

            # If it's already in the db, then skip it.
            existing = meta.scoped_session().query(RidePhoto).get(activity_photo.uid)
            if existing:
                self.logger.info(
                    "Skipping photo {} because it's already in database: {}".format(
                        activity_photo, existing
                    )
                )
                continue

            try:
                media = insta_client.media(activity_photo.uid)

                photo = RidePhoto(
                    id=activity_photo.uid,
                    ride_id=ride.id,
                    ref=activity_photo.ref,
                    caption=activity_photo.caption,
                )

                photo.img_l = media.get_standard_resolution_url()
                photo.img_t = media.get_thumbnail_url()

                meta.scoped_session().add(photo)

                self.logger.debug(
                    "Writing (non-primary) ride photo: {p_id}: {photo!r}".format(
                        p_id=photo.id, photo=photo
                    )
                )

                meta.scoped_session().flush()
            except (InstagramAPIError, InstagramClientError) as e:
                if e.status_code == 400:
                    self.logger.warning(
                        "Skipping photo {0} for ride {1}; user is set to private".format(
                            activity_photo, ride
                        )
                    )
                elif e.status_code == 404:
                    self.logger.warning(
                        "Skipping photo {0} for ride {1}; not found".format(
                            activity_photo, ride
                        )
                    )
                else:
                    self.logger.exception(
                        "Error fetching instagram photo {0} (skipping)".format(
                            activity_photo
                        )
                    )

        ride.photos_fetched = True
Exemplo n.º 6
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.orm.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.orm.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.orm.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))

    meta.scoped_session().add(p)
    meta.scoped_session().flush()

    return p
Exemplo n.º 7
0
def write_ride_photos_nonprimary(activity_photos, ride):
    """
    Writes out non-primary photos (currently only instagram) associated with a ride to the database.

    :param activity_photos: Photos for an activity.
    :type activity_photos: list[stravalib.orm.ActivityPhoto]

    :param ride: The db model object for ride.
    :type ride: bafs.orm.Ride
    """
    # [{u'activity_id': 414980300,
    #   u'activity_name': u'Pimmit Run CX',
    #   u'caption': u'Pimmit Run cx',
    #   u'created_at': u'2015-10-17T20:51:02Z',
    #   u'created_at_local': u'2015-10-17T16:51:02Z',
    #   u'id': 106409096,
    #   u'ref': u'https://instagram.com/p/88qaqZvrBI/',
    #   u'resource_state': 2,
    #   u'sizes': {u'0': [150, 150]},
    #   u'source': 2,
    #   u'type': u'InstagramPhoto',
    #   u'uid': u'1097938959360503880_297644011',
    #   u'unique_id': None,
    #   u'uploaded_at': u'2015-10-17T17:55:45Z',
    #   u'urls': {u'0': u'https://instagram.com/p/88qaqZvrBI/media?size=t'}}]

    meta.engine.execute(RidePhoto.__table__.delete().where(and_(RidePhoto.ride_id == ride.id,
                                                              RidePhoto.primary == False)))

    insta_client = insta.configured_instagram_client()

    for activity_photo in activity_photos:

        # If it's already in the db, then skip it.
        existing = meta.scoped_session().query(RidePhoto).get(activity_photo.uid)
        if existing:
            log.info("Skipping photo {} because it's already in database: {}".format(activity_photo, existing))
            continue

        try:
            media = insta_client.media(activity_photo.uid)

            photo = RidePhoto(id=activity_photo.uid,
                              ride_id=ride.id,
                              ref=activity_photo.ref,
                              caption=activity_photo.caption)

            photo.img_l = media.get_standard_resolution_url()
            photo.img_t = media.get_thumbnail_url()

            meta.scoped_session().add(photo)

            log.debug("Writing (non-primary) ride photo: {p_id}: {photo!r}".format(p_id=photo.id, photo=photo))

            meta.scoped_session().flush()
        except (InstagramAPIError, InstagramClientError) as e:
            if e.status_code == 400:
                log.warning("Skipping photo {0} for ride {1}; user is set to private".format(activity_photo, ride))
            elif e.status_code == 404:
                log.warning("Skipping photo {0} for ride {1}; not found".format(activity_photo, ride))
            else:
                log.exception("Error fetching instagram photo {0} (skipping)".format(activity_photo))


    ride.photos_fetched = True
Exemplo n.º 8
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.orm.ActivityPhotoPrimary
    :param ride: The db model object for ride.
    :type ride: bafs.orm.Ride
    :return: The newly added ride photo object.
    :rtype: bafs.orm.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))

    meta.scoped_session().add(p)
    meta.scoped_session().flush()

    return p