In [2]:
import MySQLdb
import urllib2
import sys
import re
import json

def get_js_key(client_name):
    conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='mypass', db='api')
    cur = conn.cursor(MySQLdb.cursors.DictCursor)
    sql = "select * from consumer where short_name='{client_name}' and scope='js'".format(**locals())
    cur.execute(sql)
    rows = cur.fetchall()
    cur.close()
    js_key = rows[0]["consumer_key"]
    return js_key

def format_result(raw_result):
    raw_result = raw_result.strip()
    m = re.search('\{(.*)\}',raw_result)
    j = "{"+m.groups()[0]+"}"

    d = json.loads(j)
    for item in d["list"]:
        title = item["attributesName"]["title"]
        id = item["id"]
        print "[{id}][{title}]".format(**locals())
        
def get_recs(client_name):
    js_key = get_js_key(client_name)
    url = "http://localhost:8080/js/recommendations?consumer_key={js_key}&user=u1&item=6023&limit=4&jsonpCallback=j".format(**locals())
    result = urllib2.urlopen(url).read()
    
    #print js_key
    print "--- api call ------------------------------"
    print ""
    print url
    print ""
    print "--- raw result ----------------------------"
    print ""
    print result
    print ""
    print "--- formatted result ----------------------"
    print ""
    format_result(result)

client_name = "test"
print "client: {client_name}".format(**locals())


client: test

In [3]:
get_recs(client_name)


--- api call ------------------------------

http://localhost:8080/js/recommendations?consumer_key=19L73FWBMBFLSA62JNZC&user=u1&item=6023&limit=4&jsonpCallback=j

--- raw result ----------------------------

j({"size":4,"requested":4,"list":[{"id":"21576","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"SIX KILLED IN SOUTH AFRICAN GOLD MINE ACCIDENT","recommendationUuid":"1"}},{"id":"21575","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"SOVIET INDUSTRIAL GROWTH/TRADE SLOWER IN 1987","recommendationUuid":"1"}},{"id":"21574","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"JAPAN/INDIA CONFERENCE CUTS GULF WAR RISK CHARGES","recommendationUuid":"1"}},{"id":"21573","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"TOKYO DEALERS SEE DOLLAR POISED TO BREACH 140 YEN","recommendationUuid":"1"}}]})

--- formatted result ----------------------

[21576][SIX KILLED IN SOUTH AFRICAN GOLD MINE ACCIDENT]
[21575][SOVIET INDUSTRIAL GROWTH/TRADE SLOWER IN 1987]
[21574][JAPAN/INDIA CONFERENCE CUTS GULF WAR RISK CHARGES]
[21573][TOKYO DEALERS SEE DOLLAR POISED TO BREACH 140 YEN]

In [4]:
get_recs(client_name)


--- api call ------------------------------

http://localhost:8080/js/recommendations?consumer_key=19L73FWBMBFLSA62JNZC&user=u1&item=6023&limit=4&jsonpCallback=j

--- raw result ----------------------------

j({"size":4,"requested":4,"list":[{"id":"17254","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS","recommendationUuid":"2"}},{"id":"17289","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS","recommendationUuid":"2"}},{"id":"3430","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"PICKENS SEES CONTINUED SLUMP IN WORKING RIGS","recommendationUuid":"2"}},{"id":"17359","name":"","type":1,"first_action":1450206435000,"last_action":1450206435000,"popular":false,"demographics":[],"attributes":{},"attributesName":{"title":"DECLINE IN U.S. DOLLAR MAY BOOST OPEC OIL PRICE","recommendationUuid":"2"}}]})

--- formatted result ----------------------

[17254][WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS]
[17289][WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS]
[3430][PICKENS SEES CONTINUED SLUMP IN WORKING RIGS]
[17359][DECLINE IN U.S. DOLLAR MAY BOOST OPEC OIL PRICE]

In [ ]: