#!/usr/bin/env python # -*- coding: utf-8 -*- import httplib, urllib, base64 import json result=[] input = [2094437628, 2273736245] encodeinput = json.dumps(input) deinput = json.loads(encodeinput) #''''''''''''''''''''''''''''''''''[Id, AA.AuId]''''''''''''''''''''''''''''''''' #'''''''''''''''''''''''''1st API''''''''''''''''''''''''''''''''' exp1 = "Composite(AA.AuId = %d )" % deinput[1] params1 = urllib.urlencode({ 'expr':exp1, 'offset': '0', 'model': 'latest', 'count': '10000', 'attributes': 'Id,AA.AuId,AA.AfId,F.FId,C.CId,J.JId', 'subscription-key': 'f7cc29509a8443c5b3a5e56b0e38b5a6', }) conn = httplib.HTTPSConnection('oxfordhk.azure-api.net') conn.request("GET", "/academic/v1.0/evaluate?%s" % params1) response = conn.getresponse() data = response.read() data1 = json.loads(data) conn.close() #'''''''''''''''''''''''''2nd API''''''''''''''''''''''''''''''''' exp2 = 'Id = %d' % deinput[0] params1 = urllib.urlencode({ 'expr':exp2, 'model': 'latest', 'count': '10000', 'attributes': 'RId,AA.AuId,AA.AfId,F.FId,C.CId,J.JId', 'subscription-key': 'f7cc29509a8443c5b3a5e56b0e38b5a6', }) conn = httplib.HTTPSConnection('oxfordhk.azure-api.net') conn.request("GET", "/academic/v1.0/evaluate?%s" % params1) response = conn.getresponse() data = response.read() data2 = json.loads(data) conn.close() #'''''''''''''''''''''data 2'''''''''''''''''''''''''''''''' print ("Data 2") print data2 try: ReId2 = data2["entities"][0]["RId"] except (KeyError): ReId2 = [] try: FId2 = data2["entities"][0]["F"] except (KeyError): FId2 = [] try: AuId2 = data2["entities"][0]["AA"] except (KeyError): AuId2 = [] try: CJId2 = data2["entities"][0]["J"]["JId"] except (KeyError): try: CJId2 = data2["entities"][0]["C"]["CId"] except: CJId2 =[] for tmp in AuId2: if deinput[1] == tmp["AuId"]: result.append([deinput[0],deinput[1]]) # Id -> AA.AuId (1-hop) #'''''''''''''''''''''data 1'''''''''''''''''''''''''''''''' print ("Data1") print data1 AfId1=[] Id1=[] for i1 in data1["entities"]: Id1.append(i1["Id"]) for tmp in ReId2: if i1["Id"] == tmp:# Id -> Id -> AA.AuId (2-hop) result.append([deinput[0],i1["Id"],deinput[1]]) try: FId1 = i1["F"] for tmp in FId1: for x in FId2: if x["FId"] == tmp["FId"]: # Id -> F.FId -> Id -> AA.AuId (3-hop) result.append([deinput[0],x["FId"],i1["Id"],deinput[1]]) except (KeyError): continue try: for tmp in i1["AA"]: if tmp["AuId"] == deinput[1]: if tmp["AfId"] not in AfId1: AfId1.append(tmp["AfId"]) break except (KeyError): continue try: CJId1 = i1["J"]["JId"] # Id -> C.CId/J.JId -> Id -> AA.AuId (3-hop) if CJId1 == CJId2: result.append([deinput[0],CJId2,i1["Id"],deinput[1]]) except (KeyError): try: CJId1= i1["C"]["CId"] if CJId1 == CJId2: result.append([deinput[0],CJId2,i1["Id"],deinput[1]]) except: CJId1=[] for tmp in AfId1: for x in AuId2: if tmp == x["AfId"]: # Id -> AA.AuId -> AA.AfId -> AA.AuId (3-hop) result.append([deinput[0],x["AuId"],tmp,deinput[1]]) #************************************************************need gevent #''''''''''''''''''''4th API'''''''''''''''''''''''''''''''''''' for rid in ReId2: exp4 = 'Id = %d' % rid params1 = urllib.urlencode({ 'expr':exp4, 'count': '10000', 'model': 'latest', 'attributes': 'RId', 'subscription-key': 'f7cc29509a8443c5b3a5e56b0e38b5a6', }) conn = httplib.HTTPSConnection('oxfordhk.azure-api.net') conn.request("GET", "/academic/v1.0/evaluate?%s" % params1) response = conn.getresponse() data = response.read() data4 = json.loads(data) conn.close() #print data4 try: ReId4= data4["entities"][0]["RId"] except (KeyError): ReId4 = [] # AA.AuId -> id -> id -> id for rid4 in ReId4: for tmp in Id1: if rid4 == tmp: result.append([deinput[0],rid,rid4,deinput[1]]) print ("Data4") print data4 deresult = json.dumps(result) print ("Result") print deresult #print len(deresult)