-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl_uscis.py
147 lines (122 loc) · 4.22 KB
/
crawl_uscis.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
import requests
from bs4 import BeautifulSoup, Tag
import json
from concurrent.futures import ThreadPoolExecutor
import argparse
def url_from_rid(receipt_id):
return (
f"https://egov.uscis.gov/casestatus/mycasestatus.do?appReceiptNum={receipt_id}"
)
def get_status(rid):
url = url_from_rid(rid)
result = {}
response = requests.get(url)
soup = BeautifulSoup(response.text, "lxml")
for div in soup.find("div", attrs={"class": ["appointment-sec", "text-center"]}):
p = div.find("p")
if isinstance(p, Tag):
result["Description"] = p.text.split("<p>")[-1].split("</p")[0]
h = div.find("h1")
if isinstance(h, Tag):
result["Title"] = h.text.split("<h1>")[-1].split("</h1")[0]
for div in soup.find("div", {"id": "formErrorMessages"}):
l = div.find("li")
if isinstance(l, Tag):
result["error"] = l.text.split("<li>")[-1].split("</li")[0]
return result
def accum_stats(title, stats, before=True):
if before:
key = "Before"
else:
key = "After"
if "Approved" in title:
stats[key]["Approved"] += 1
elif "Request for Additional Evidence" in title:
stats[key]["RFE"] += 1
elif "Received" in title:
stats[key]["Received"] += 1
elif "Updated To Show Fingerprints" in title:
stats[key]["Fingerprints taken"] += 1
elif "Rejected" in title:
stats[key]["Rejected"] += 1
else:
stats[key]["Other"] += 1
return stats
def crawl_uscis(receipt_prefix, receipt_nums, crawl_range):
print("Your Summary:\n")
for receipt_num in receipt_nums:
results = get_status(receipt_prefix + str(receipt_num))
for k, v in results.items():
print(f"{k}: {v}")
print("\n")
stats = {
"Before": {
"Approved": 0,
"RFE": 0,
"Received": 0,
"Other": 0,
"Rejected": 0,
"Fingerprints taken": 0,
},
"After": {
"Approved": 0,
"RFE": 0,
"Received": 0,
"Other": 0,
"Rejected": 0,
"Fingerprints taken": 0,
},
}
results = {}
with ThreadPoolExecutor(max_workers=8) as executor:
futures = []
last_receipt_num = int(receipt_nums[-1])
for i in range(last_receipt_num - crawl_range, last_receipt_num + crawl_range):
result = futures.append(
(i, executor.submit(get_status, receipt_prefix + str(i)))
)
for future in futures:
receipt_idx, result_future = future
result = result_future.result()
results[receipt_idx] = result
if not any(
[
check in result["Description"]
for check in ["I-131", "I-485", "I-765"]
]
):
continue
if "Title" in result: # Just checking to make sure it's not an error
stats = accum_stats(
result["Title"], stats, receipt_idx > last_receipt_num
)
print("Analyzing neighbors:\n")
for k, v in stats.items():
print(f"\nStats from {k} your receipt number")
all_apps = sum(v.values())
for v1, v2 in v.items():
print(f"{v1}:{v2}/{all_apps}")
return results
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Command line interface to compute comparative metrics on sets of queries in database."
)
parser.add_argument(
"--receipt_prefix", help="USCIS given receipt prefix.", type=str, default="MSC"
)
parser.add_argument(
"--receipt_numbers",
help="comma separated USCIS given receipt numbers",
type=str,
default="2290263023,2290263024,2290263025",
)
parser.add_argument(
"--crawl_range", help="+- receipt numbers to crawl", type=int, default=100
)
args = parser.parse_args()
results = crawl_uscis(
args.receipt_prefix, args.receipt_numbers.split(","), args.crawl_range
)
with open("uscis_crawls.json", "w") as f:
json.dump(results, f)
print("\nYour Neighbors summary stored to uscis_crawls.json")