-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiCall.py
82 lines (76 loc) · 2.51 KB
/
apiCall.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import requests
import json
import itertools
import math
import fileIO
import re
import urllib
# Get Number of Comments on regulation, given API_KEY and DOCKET_ID
def getLastNComments(API_KEY, DOCKET_ID, LIMIT, END_NUMBER):
# Call the URL and get the number of comments
getRegulationUrl = "http://api.data.gov:80/regulations/v3/documents.json?api_key="+API_KEY+"&dktid="+DOCKET_ID+"&rpp="+LIMIT+"&po="+END_NUMBER
response = urllib.urlopen(getRegulationUrl)
commentData = json.loads(response.read())
# Return commentData
return commentData
# Get Sentiment Value from API
def getSentimentResults(jsonString):
url = 'http://sentiment.vivekn.com/api/batch/'
response = requests.post(url, data=jsonString)
return response.json()
# Get Ethnicity from TextMap API
def getEthnicityResults(authorList):
url = 'http://www.textmap.com/ethnicity_api/api'
curList0 = json.dumps(authorList[0:2])
response1 = requests.post(url, data=curList0)
print "Len: %d" %(len(response.json()))
return response1.json()
# Call API from Genderize
def callGenderAPI(inputString):
url = "https://api.genderize.io/?%s" % (inputString)
response = requests.request('GET',url)
return response.json()
# Write Gender Results to File
def processResults(results, myNameList):
for index in range( len(results) ):
myGender = results[index]['gender']
myName = myNameList[index]
myOutString = str(myName)+","+str(myGender)
fileIO.writeFile(myOutString, 'results/genderAPI.csv')
# Get Gender by First Name
def getGender(authorList):
counter = 0
apiLimit = 0
apiString = ""
myNameList = []
# Preprocess the data and use only
for index in range ( len(authorList) ):
counter += 1
fname = authorList[index]
myNameList.append(fname)
# Get only the first name
name = fname.split(' ')[0].lstrip(' ')
re.sub(r'\W+', '', name)
# Generate string in required API format
apiString = apiString + "name[%d]=%s&" % (counter, name)
# Check for API Limit
if apiLimit > 900:
break
# Set limit to 10 names per call
if (counter == 9):
# Call the API and write results to File
results = callGenderAPI(apiString)
myJsonDict = processResults(results, myNameList)
print "Processing \n"
# Reset counters after 10
apiString = ""
counter = 0
apiLimit += 10
myDocIdList = []
myNameList = []
# Call API last time for remaining names
if apiString != "":
callGenderAPI(apiString)
# Write Gender Results to File
print "Writing to results/genderResults.json... \n"
fileIO.writeJsonFile(myJsonDict, 'results/genderResults.json')