You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.6 KiB

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib, urllib, base64
import json
result=[]
#input = [2157025439,2139398774]
input = [1970381522,2162351023]
encodeinput = json.dumps(input)
deinput = json.loads(encodeinput)
#''''''''''''''''''''''''''''''''''[id0, id1]'''''''''''''''''''''''''''''''''
#'''''''''''''''1st API''''''''''''''''''''''''
exp1 = 'Id = %d' % deinput[0]
params1 = urllib.urlencode({
'expr':exp1,
'model': 'latest',
'offset': '0',
'count': '10000',
'attributes': 'RId,F.FId,AA.AuId,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[1]
params1 = urllib.urlencode({
'expr':exp2,
'count': '10000',
'model': 'latest',
'attributes': 'F.FId,AA.AuId,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()
#'''''''''''''''3rd API''''''''''''''''''''''''
exp3 = 'RId = %d' % deinput[1]
params1 = urllib.urlencode({
'expr':exp3,
'count': '10000',
'model': 'latest',
'attributes': 'Id,F.FId,AA.AuId,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()
data3 = json.loads(data)
conn.close()
#''''''''''''''''''data 1'''''''''''''''''''''''''''
try:
ReId1=data1["entities"][0]["RId"]
except (KeyError):
ReId1 = []
try:
FId1 = data1["entities"][0]["F"]
except (KeyError):
FId1 = []
try:
AuId1= data1["entities"][0]["AA"]
except (KeyError):
AuId1=[]
try:
CJId1= data1["entities"][0]["J"]["JId"]
except (KeyError):
try:
CJId1= data1["entities"][0]["C"]["CId"]
except:
CJId1=[]
#''''''''''''''''''data 2'''''''''''''''''''''''''''
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=[]
#''''''''''''''''''data 3'''''''''id->XXXX->id->id''''''''''''''''''maybe big enough > 10w
Id3 = []
FId3 = []
AuId3 = []
CJId3 = []
for i in data3["entities"]:
# id -> F.FId -> id -> id
try:
FId_data3 = i["F"]
for tmp in FId_data3:
for x in FId1:
if x["FId"]==tmp["FId"]:
result.append([deinput[0],tmp["FId"],i["Id"],deinput[1]])
#FId3.append(tmp["FId"])#
except(KeyError):
continue
# id -> AA.AuId -> id -> id
try:
AuId_data3=i["AA"]
for tmp in AuId_data3:
for x in AuId1:
if x["AuId"]==tmp["AuId"]:
result.append([deinput[0],tmp["AuId"],i["Id"],deinput[1]])
#AuId3.append(tmp["AuId"])#'''''''''''''''''''''need optimization
except(KeyError):
continue
# id -> C.CId/J.JId -> id -> id
try:
CJId3=i["J"]["JId"]#'''''''''''''''''''''need optimization
if CJId3==CJId1:
result.append([[deinput[0],CJId3,i["Id"],deinput[1]]])
except(KeyError):
try:
CJId3=i["C"]["CId"]
if CJId3==CJId1:
result.append([[deinput[0],CJId3,i["Id"],deinput[1]]])
except(KeyError):
continue
#''''''''''''''''''operation''''''''''1 and 2 hop'''''''''''''''''
#id -> id
for Reid in ReId1:
if deinput[1] == Reid:
result.append([deinput[0],Reid])
break
#id -> id -> id
'''for Id in Id3:
for Reid in ReId1:
if Reid == Id:
result.append([deinput[0],Reid,deinput[1]])'''
#id -> F.FId -> id
for x in FId2:
#print 'x: %d ' % x['FId']
for y in FId1:
#print y['FId']
if y['FId']==x['FId']:
result.append([deinput[0],x['FId'],deinput[1]])
#id -> AA.AuId -> id
for x in AuId2:
for y in AuId1:
if y['AuId']==x['AuId']:
result.append([deinput[0],x['AuId'],deinput[1]])
#id -> C.CId/J.JId -> id
if CJId1==CJId2:
result.append([deinput[0],CJId1,deinput[1]])
#''''''''''''''''''''4th API'''''''''''''''''''''''''''''
#id -> id -> XXXXXX -> id ************************************************************need gevent
for Reid in ReId1:
exp4 = 'Id = %d' % Reid
params1 = urllib.urlencode({
'expr':exp4,
'count': '10000',
'model': 'latest',
'attributes': 'RId,F.FId,AA.AuId,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()
data4 = json.loads(data)
conn.close()
#print data4
try:
ReId4= data4["entities"][0]["RId"]
except (KeyError):
ReId4 = []
try:
FId4 = data4["entities"][0]["F"]
except (KeyError):
FId4 = []
try:
AuId4= data4["entities"][0]["AA"]
except (KeyError):
AuId4=[]
try:
CJId4= data4["entities"][0]["J"]["JId"]
except (KeyError):
try:
CJId4= data4["entities"][0]["C"]["CId"]
except:
CJId4=[]
#id -> id -> id #id -> id -> id -> id
for rid in ReId4:
if deinput[1] == rid:
result.append([deinput[0],Reid,deinput[1]])
for Id in Id3:
if rid == Id:
result.append([deinput[0],Reid,rid,deinput[1]])
#id -> id -> F.FId -> id
for x in FId2:
for y in FId4:
if y['FId']==x['FId']:
result.append([deinput[0],Reid,x['FId'],deinput[1]])
#id -> id -> AA.AuId -> id
for x in AuId2:
for y in AuId4:
if y['AuId']==x['AuId']:
result.append([deinput[0],Reid,x['AuId'],deinput[1]])
#id -> id -> C.CId/J.JId -> id
if CJId4==CJId2:
result.append([deinput[0],Reid,CJId4,deinput[1]])
deresult = json.dumps(result)
print deresult
print len(result)