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 execute(self, options, args): # if options.rewrite: # db.engine.execute(model.RidePhoto.__table__.delete()) # db.session.query(model.Ride).update({"photos_fetched": False}) q = db.session.query(model.RidePhoto) q = q.filter_by(img_t=None) insta_client = configured_instagram_client() del_q = [] for ride_photo in q: self.logger.debug("Updating URLs for photo {}".format(ride_photo)) try: media = insta_client.media(ride_photo.id) ride_photo.img_l = media.get_standard_resolution_url() ride_photo.img_t = media.get_thumbnail_url() db.session.commit() except InstagramAPIError as e: if e.status_code == 400: self.logger.error( "Skipping photo {}; user is set to private".format( ride_photo)) del_q.append(ride_photo.id) else: self.logger.exception( "Error fetching instagram photo {0} (skipping)".format( ride_photo)) if del_q: db.engine.execute(model.RidePhoto.__table__.delete().where( model.RidePhoto.id.in_(del_q))) db.session.commit()
def execute(self, options, args): # if options.rewrite: # db.engine.execute(model.RidePhoto.__table__.delete()) # db.session.query(model.Ride).update({"photos_fetched": False}) q = db.session.query(model.RidePhoto) q = q.filter_by(img_t=None) insta_client = configured_instagram_client() del_q = [] for ride_photo in q: self.logger.debug("Updating URLs for photo {}".format(ride_photo)) try: media = insta_client.media(ride_photo.id) ride_photo.img_l = media.get_standard_resolution_url() ride_photo.img_t = media.get_thumbnail_url() db.session.commit() except InstagramAPIError as e: if e.status_code == 400: self.logger.error("Skipping photo {}; user is set to private".format(ride_photo)) del_q.append(ride_photo.id) else: self.logger.exception("Error fetching instagram photo {0} (skipping)".format(ride_photo)) if del_q: db.engine.execute(model.RidePhoto.__table__.delete().where(model.RidePhoto.id.in_(del_q))) db.session.commit()
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