Last active
January 24, 2025 22:34
-
-
Save jerryz1982/8ef2736291a3230746406b3deac0fbfc to your computer and use it in GitHub Desktop.
USCIS Batch Case Status Query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright Jerry Zhao | |
# | |
# All Rights Reserved | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); you may | |
# not use this file except in compliance with the License. You may obtain | |
# a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
# License for the specific language governing permissions and limitations | |
# under the License. | |
# | |
""" | |
python uscis_batch_query.py WAC1234567890 | |
or | |
python uscis_batch_query.py WAC1234567100 200 | |
WAC1234567100:Notice Was Returned To USCIS Because The Post Office Could Not Deliver It On September 13 | |
WAC1234567101:Case Was Transferred And A New Office Has Jurisdiction On September 22 | |
WAC1234567102:Case Not in System Yet | |
... | |
""" | |
import requests | |
from bs4 import BeautifulSoup | |
import sys | |
import re | |
if len(sys.argv) < 2: | |
print "Usage: python uscis.py <Start Case Number> <OPTIONAL:Search Quantity>" | |
sys.exit(1) | |
elif len(sys.argv) < 3: | |
quantity = 1 | |
else: | |
quantity = sys.argv[2] | |
line_break = 69 | |
count_break = 100 | |
start = sys.argv[1] | |
case = re.search('([A-Z]{3})(\d{10})', start) | |
center = case.group(1) | |
number_start = case.group(2) | |
url = 'https://egov.uscis.gov/casestatus/mycasestatus.do' | |
stats = {} | |
for i in range(0, int(quantity)): | |
receipt_no = center + str(int(number_start) + i) | |
params = {'appReceiptNum': receipt_no, 'initCaseSearch': 'CHECK STATUS'} | |
result = requests.post(url, params=params) | |
parsed_page = BeautifulSoup(result.text, "html.parser") | |
find_result = parsed_page.find("div", {"class": "rows text-center"}) | |
if find_result: | |
status = find_result.find("h1").text | |
detail = find_result.find("p").text | |
date = detail.split(',')[0] | |
message = status + ' ' + date | |
else: | |
status = 'Case Not in System Yet' | |
message = status | |
if status not in stats: | |
stats[status] = 1 | |
else: | |
stats[status] = stats[status] + 1 | |
print (receipt_no + ":") + message | |
sorted_stats = sorted(stats.items(), key=lambda x:x[1], reverse=True) | |
print 'Summary:\n' | |
for status, count in sorted_stats: | |
line = len(status)/line_break + 1 | |
start = 0 | |
end = line_break | |
length = len(status) | |
for i in range(0, line): | |
if end%line_break != 0: | |
start = end | |
else: | |
start = i*line_break | |
if start+line_break <= len(status) and status[start+line_break-1] != " " and status[start+line_break] != " ": | |
end = start+line_break-1 | |
pad = 1 | |
length = length + 1 | |
else: | |
end = start + line_break | |
if end >= len(status): | |
pad = count_break - length%line_break | |
else: | |
pad = 0 | |
if pad > 1: | |
print "\n" + status[start:end] + pad*"-" + str(count) | |
else: | |
print "\n" + status[start:end] + pad*'-' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment