def instagram_push_update(): data = request.stream.read() instagram_client_secret = Configuration.value_for_name('instagram_client_secret') server_signature = request.headers.get('X-Hub-Signature', None) signature = hmac.new(str(instagram_client_secret), data, digestmod=hashlib.sha1).hexdigest() if server_signature != signature: logger.warn('Got PuSH subscribe POST from instagram w/o valid signature: sent=%s != expected=%s', server_signature, signature) raise ndb.Return('') logger.info('Got PuSH body: %s', data) logger.info('Got PuSH headers: %s', request.headers) parsed_feed = json.loads(data) user_ids = [int(x.get('object_id')) for x in parsed_feed] feeds = InstagramFeed.query(InstagramFeed.user_id.IN(user_ids)) cursor = None more = True keys = [] while more: feed_keys, cursor, more = feeds.fetch_page(100, keys_only=True, start_cursor=cursor) keys += feed_keys keys = ','.join([x.urlsafe() for x in keys]) if keys: yield Queue('poll').add_async(Task(url=url_for('tq_feed_poll'), method='POST', params={'keys': keys})) raise ndb.Return('ok')
def instagram_subscribe(): mode = request.args['hub.mode'] challenge = request.args['hub.challenge'] verify_token = request.args.get('hub.verify_token') if mode == 'subscribe': instagram_verify_token = Configuration.value_for_name('instagram_verify_token') if verify_token and verify_token != instagram_verify_token: logger.info('Failed verification feed.verify_token:%s GET verify_token:%s', instagram_verify_token, verify_token) return "Failed Verification", 400 logger.info('Responding to instagram challange: %s', challenge) return challenge