Skip to content

Instantly share code, notes, and snippets.

@wingchen
Last active December 14, 2015 05:29
Show Gist options
  • Save wingchen/5035904 to your computer and use it in GitHub Desktop.
Save wingchen/5035904 to your computer and use it in GitHub Desktop.
Fliptop API Code Example in Python The 202 retry mechanism is built up into the_202_retry_queue and FliptopAPIRetryClass. To run this script, simply type 'python fliptop_api.py' into your terminal.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
from datetime import datetime
import json
import threading
from collections import deque
import time
FLIPTOP_API_KEY = 'your fliptop api key'
# this queue represent all the email inputs from your database
# please feel free to replace this queue into any data source that
# you might have.
database_emails = deque([
'doug@fliptop.com',
'tom@fliptop.com',
'to_be_retried@fliptop.com'
])
# this queue is going to store the emails that needs to be retried
the_202_retry_queue = deque([])
# get_social_data_via_email
# it handles the actual fliptop API query,
# and different cases according to the returned
# http status code
def get_social_data_via_email(email_item, format='json'):
if email_item['retry_attempt'] == 0:
print('Looking up: ' + email_item['email'])
elif email_item['retry_attempt'] > 3:
print('Retry over 3 times, dropping the email: ' + email_item['email'])
return
else:
print('Retrying: ' + email_item['email'])
response = urllib.urlopen('http://api.fliptop.com/beta/person?api_key=%s&email=%s&format=%s' % (FLIPTOP_API_KEY, email_item['email'], format))
status_code = response.getcode()
if status_code == 200:
# 200, successful, priting the data out
# normally you would like to store the returned data into your
# database, or write it into a desinated file
person_obj = json.loads(response.read())
print('Email: ' + person_obj['email'])
print('Name: ' + person_obj['name'])
print('Company: ' + person_obj['company'])
print('Title: ' + person_obj['title'])
if 'facebook' in person_obj['memberships']:
print('facebook: ' + person_obj['memberships']['facebook'])
if 'linkedin' in person_obj['memberships']:
print('linkedin: ' + person_obj['memberships']['linkedin'])
if 'twitter' in person_obj['memberships']:
print('twitter: ' + person_obj['memberships']['twitter'])
elif status_code == 202:
# 202, put it into the_202_retry_queue
the_202_retry_queue.append({'email':email_item['email'], 'retry_attempt':email_item['retry_attempt'] + 1, 'last_try':datetime.now()})
elif status_code == 400:
print('Incorrect syntax. email: ' + email_item['email'])
elif status_code == 500:
print('Unexpected server error. Please email api@fliptop.com. email: ' + email_item['email'])
else:
print('Other error with email: ' + email_item['email'] + ' status code:' + str(response.status) + ' error message:' + response.read())
# the thread class to walk through the entire database emails
class FliptopAPIWalkThroughClass(threading.Thread):
def run(self):
while len(database_emails) > 0:
item = database_emails.popleft()
email_item = {'email':item, 'retry_attempt':0, 'last_try':datetime.now()}
get_social_data_via_email(email_item)
print('FliptopAPIWalkThroughClass Finished.')
# the thread class to conduct retries
class FliptopAPIRetryClass(threading.Thread):
def run(self):
while len(database_emails) > 0 or len(the_202_retry_queue) > 0:
# sleep 30 seconds for the next retry
time.sleep(30)
while len(the_202_retry_queue) > 0:
item = the_202_retry_queue.popleft()
diff = datetime.now() - item['last_try']
if diff.seconds > 30:
get_social_data_via_email(item)
else:
the_202_retry_queue.append(item)
print('Finish FliptopAPIRetryClass...')
# kick off the threads
# threads are used here to increase the performance
# if performance is not a concern here,
# you can just call FliptopAPIRetryClass after
# FliptopAPIWalkThroughClass is done, like the last two commented-out lines.
FliptopAPIWalkThroughClass().start()
FliptopAPIRetryClass().start()
#FliptopAPIWalkThroughClass().run()
#FliptopAPIRetryClass().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment