-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsf_query.py
102 lines (86 loc) · 3.03 KB
/
nsf_query.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
program_elements_codes = {
"Algebra and Number Theory": "1264",
"Analysis": "1281",
"Applied Mathematics": "1266",
"Combinatorics": "7970",
"Computational Mathematics": "1271",
"Foundations": "1268",
"Geometric Analysis": "1265",
"Mathematical Biology": "7334",
"Probability": "1263",
"Statistics": "1269",
"Topology": "1267"
}
def generate_query_url(program_name, start_year, expired='true'):
URL = 'https://www.nsf.gov/awardsearch/advancedSearchResult?'
Query = {
'PIId': '',
'PIFirstName': '',
'PILastName': '',
'PIOrganization': '',
'PIState': '',
'PIZip': '',
'PICountry': '',
'ProgOrganization':'03040000',
'ProgEleCode': program_elements_codes[program_name],
'BooleanElement': 'All',
'ProgRefCode': '',
'BooleanRef': 'All',
'Program': '',
'ProgOfficer': '',
'Keyword': '',
'AwardNumberOperator': '',
'AwardAmount': '',
'AwardInstrument': '',
'ActiveAwards': 'true',
'ExpiredAwards': expired,
'OriginalAwardDateOperator': '',
'OriginalAwardDateFrom': '',
'OriginalAwardDateTo': '',
'StartDateOperator': 'Range',
'StartDateFrom': '01%2F01%2F' + str(start_year),
'StartDateTo': '12%2F31%2F' + str(start_year),
'ExpDateOperator': ''
}
for q in Query:
URL += q + '=' + Query[q] + '&'
return URL[:-1]
def get_awards_csv(program_name, year, headless=True):
if headless:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')
driver = webdriver.Chrome(options=chrome_options)
else:
driver = webdriver.Chrome()
print(generate_query_url(program_name, year))
driver.get(generate_query_url(program_name, year))
time.sleep(10)
driver.get('https://www.nsf.gov/awardsearch/ExportResultServlet?exportType=csv')
time.sleep(10)
driver.close()
cur_dir = os.path.dirname(__file__)
prog_dir = program_name.replace(" ", "-")
if not os.path.isdir(os.path.join(cur_dir, prog_dir)):
os.mkdir(os.path.join(cur_dir, prog_dir))
target_file = os.path.join(os.path.join(cur_dir, prog_dir), "Awards-" + program_name.replace(" ", "-") +"-" + str(year) + ".csv")
if os.path.exists(target_file) and os.path.exists("Awards.csv"):
# check rows in csv
old_num_of_rows = len(pd.read_csv(target_file, encoding='latin-1'))
new_num_of_rows = len(pd.read_csv("Awards.csv", encoding='latin-1'))
if old_num_of_rows != new_num_of_rows:
os.remove(target_file)
os.rename("Awards.csv", target_file)
return 0
else:
os.remove("Awards.csv")
return 1
elif os.path.exists("Awards.csv"):
os.rename("Awards.csv", target_file)
return 2
else:
return 3