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
def write_ride_efforts(strava_activity, ride): """ Writes out all effort associated with a ride to the database. :param strava_activity: The :class:`stravalib.model.Activity` that is associated with this effort. :type strava_activity: :class:`stravalib.model.Activity` :param ride: The db model object for ride. :type ride: :class:`bafs.model.Ride` """ assert isinstance(strava_activity, strava_model.Activity) assert isinstance(ride, Ride) try: # Start by removing any existing segments for the ride. db.engine.execute(RideEffort.__table__.delete().where(RideEffort.ride_id == strava_activity.id)) # Then add them back in for se in strava_activity.segment_efforts: effort = RideEffort(id=se.id, ride_id=strava_activity.id, elapsed_time=timedelta_to_seconds(se.elapsed_time), segment_name=se.segment.name, segment_id=se.segment.id) log.debug("Writing ride effort: {se_id}: {effort!r}".format(se_id=se.id, effort=effort.segment_name)) db.session.add(effort) ride.efforts_fetched = True except: log.exception("Error adding effort for ride: {0}".format(ride)) raise
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.model.ActivityPhoto] :param ride: The db model object for ride. :type ride: bafs.model.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'}}] db.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 = db.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() db.session.add(photo) log.debug("Writing (non-primary) ride photo: {p_id}: {photo!r}".format(p_id=photo.id, photo=photo)) db.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