-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
170 lines (146 loc) · 5.79 KB
/
tasks.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from pathlib import Path
from RPA.Browser.Selenium import Selenium
from RPA.Excel.Files import Files
from datetime import timedelta
import webdrivermanager
import time
import os
from dotenv import load_dotenv
load_dotenv()
OUTPUTDIR = Path("output")
browser_lib = Selenium()
def open_website(url):
driver = webdrivermanager.ChromeDriverManager()
driver.download_and_install("94.0.4606.61")
driver_path = Path(driver.link_path).joinpath(driver.driver_filenames.get(driver.get_os_name()))
options = browser_lib._get_driver_args("chrome")[0]["options"]
if driver.get_os_name() == "linux":
options.binary_location = "/usr/bin/chromium-browser"
browser_lib.set_download_directory(OUTPUTDIR.__str__())
prefs = {
"download.default_directory": OUTPUTDIR.resolve(strict=True).__str__(),
"download.directory_upgrade": True,
"download.prompt_for_download": False,
"plugins.always_open_pdf_externally": True,
}
options.add_experimental_option("prefs", prefs)
browser_lib.open_browser(url, browser="chrome", options=options, executable_path=driver_path)
def get_agencies_elements(name=None):
if name is not None:
agency = browser_lib.find_element(
f"//div[@id='agency-tiles-widget']//span[text()='{name}']/.."
)
return agency
dive_in = browser_lib.find_element("//a[@href='#home-dive-in']").click()
browser_lib.wait_until_page_contains_element(
"//a[@href='#home-dive-in' and @aria-expanded='true']"
)
browser_lib.wait_until_page_contains_element("//div[@id='agency-tiles-widget']")
browser_lib.wait_until_element_is_visible("//div[@id='agency-tiles-widget']//a")
agencies = browser_lib.find_elements(
"//div[@id='agency-tiles-widget']//a[contains(@href, '/drupal/summary')]/span"
)
return list(zip(agencies[::2], agencies[1::2]))
def get_agencies_spending():
agencies = get_agencies_elements()
agencies_bills = [(agency[0].text, agency[1].text) for agency in agencies]
return agencies_bills
def create_agencies_excel(agencies):
lib_files = Files()
try:
lib_files.create_workbook(OUTPUTDIR.joinpath("agencies.xlsx").__str__())
lib_files.rename_worksheet("Sheet", "Agencies")
lib_files.append_rows_to_worksheet(agencies)
lib_files.save_workbook()
finally:
lib_files.close_workbook()
def create_individual_investiments_excel(agency_investments):
lib_files = Files()
try:
lib_files.open_workbook(OUTPUTDIR.joinpath("agencies.xlsx").__str__())
lib_files.create_worksheet("Individual Investiments", agency_investments)
lib_files.save_workbook()
finally:
lib_files.close_workbook()
def get_agency():
agency_name = os.getenv("AGENCY_NAME")
if not agency_name:
raise Exception("Please provide an agency name in the .env file")
return agency_name
def download_business_case_pdf():
download_urls = browser_lib.find_elements(
"//div[@id='investments-table-object_wrapper']//tbody//tr//td[1]//a"
)
for url_id in download_urls:
url = browser_lib.get_element_attribute(url_id, "href")
browser_lib.execute_javascript(f"window.open('{url}')")
filename = f"{url_id.text}.pdf"
browser_lib.switch_window("NEW")
browser_lib.wait_until_element_is_visible(
"//div[@id='business-case-pdf']/a", timedelta(minutes=1)
)
browser_lib.find_element("//div[@id='business-case-pdf']/a").click()
while not OUTPUTDIR.joinpath(filename).is_file():
time.sleep(1)
browser_lib.close_window()
browser_lib.switch_window("MAIN")
print(f" [x] file {filename} downloaded")
time.sleep(1)
browser_lib.close_browser()
def get_agency_specific_spending(agency):
agency = get_agencies_elements(agency).click()
browser_lib.wait_until_page_contains_element(
"//div[@id='investments-table-object_length']/label/select",
timedelta(minutes=1),
)
browser_lib.wait_until_element_is_visible(
"//div[@id='investments-table-object_length']/label/select",
timedelta(minutes=1),
)
browser_lib.set_focus_to_element("//h4[text()='Individual Investments']")
button_show_all_entries = browser_lib.find_element(
"//div[@id='investments-table-object_length']/label/select/option[contains(text(),'All')]"
)
button_show_all_entries.click()
browser_lib.wait_until_page_contains_element(
"//a[@id='investments-table-object_last' and contains(@class, 'disabled')]",
timedelta(minutes=1),
)
browser_lib.wait_until_element_is_visible(
"//a[@id='investments-table-object_last' and contains(@class, 'disabled')]",
timedelta(minutes=1),
)
investments = browser_lib.find_elements(
"//div[@id='investments-table-object_wrapper']//tbody//tr//td"
)
return investments
def scrapy_specific_agency(agency):
investments = get_agency_specific_spending(agency)
rows = []
row = []
count = 0
for td in investments:
if count < 6:
count += 1
row.append(td.text)
else:
count = 0
rows.append(row.copy())
row.clear()
return rows
def main():
try:
agency = get_agency()
print(" [x] agency found in environment")
open_website("https://itdashboard.gov/")
agencies = get_agencies_spending()
create_agencies_excel(agencies)
print(" [x] agencies xlsx created")
individual_investiments = scrapy_specific_agency(agency)
create_individual_investiments_excel(individual_investiments)
print(" [x] agency individual investments sheet created")
download_business_case_pdf()
finally:
browser_lib.close_all_browsers()
if __name__ == "__main__":
main()