Last active
March 22, 2016 04:53
-
-
Save chrisg32/5224a4b48f5f0bcf08c6 to your computer and use it in GitHub Desktop.
A simple python script that I created when I had jury duty (Texas) and had to check daily to see if I need to report in.
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
import cookielib | |
import socket | |
import urllib | |
import urllib2 | |
#r requires BeautifulSoup4 | |
from bs4 import BeautifulSoup | |
#your jury summons info | |
participant_number = "#########" | |
zip_code = "#####" | |
# jury duty url | |
url = 'https://jury.txs.uscourts.gov/AppearWeb/Default.aspx' | |
# setup socket connection timeout | |
timeout = 15 | |
socket.setdefaulttimeout(timeout) | |
# setup cookie handler | |
cookie_jar = cookielib.LWPCookieJar() | |
cookie = urllib2.HTTPCookieProcessor(cookie_jar) | |
# create an urllib2 opener() | |
opener = urllib2.build_opener(cookie) | |
# build the header for the GET request | |
http_header = { | |
"Host": "jury.txs.uscourts.gov", | |
"Connection": "keep-alive", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | |
"Upgrade-Insecure-Requests": "1", | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36", | |
"DNT": "1", | |
"Accept-Encoding": "gzip, deflate", | |
"Accept-Language": "en-US,en;q=0.8" | |
} | |
# create the GET request | |
req = urllib2.Request(url, None, http_header) | |
# submit the GET request | |
res = opener.open(req) | |
# read the response | |
html = res.read() | |
#use BS4 to parse html | |
soup = BeautifulSoup(html, "html.parser") | |
# get the hidden form elements that we have to include to be a valid site visitor | |
viewstate = soup.find("input", {"id" : "__VIEWSTATE"})["value"]; | |
viewstategen = soup.find("input", {"id" : "__VIEWSTATEGENERATOR"})["value"]; | |
eventval = soup.find("input", {"id" : "__EVENTVALIDATION"})["value"]; | |
# build the POST request header | |
http_header2 = { | |
"Host": "jury.txs.uscourts.gov", | |
"Connection": "keep-alive", | |
"Cache-Control": "max-age=0", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | |
"Origin": "https://jury.txs.uscourts.gov", | |
"Upgrade-Insecure-Requests": "1", | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36", | |
"Content-Type": "application/x-www-form-urlencoded", | |
"DNT": "1", | |
"Referer": "https://jury.txs.uscourts.gov/AppearWeb/Default.aspx", | |
"Accept-Encoding": "gzip, deflate", | |
"Accept-Language": "en-US,en;q=0.8" | |
} | |
# build to POST form data | |
params = { | |
"ctl02$txtPart": participant_number, | |
"ctl02$txtZip": zip_code, | |
"ctl02$btnInstructions" : "Reporting Instructions", | |
"__VIEWSTATE": viewstate, | |
"__VIEWSTATEGENERATOR": viewstategen, | |
"__EVENTVALIDATION": eventval | |
} | |
# create the POST HTTP request | |
req2 = urllib2.Request(url, urllib.urlencode(params), http_header) | |
# submit the request | |
res = opener.open(req2) | |
# read the response | |
html = res.read() | |
# parse the response | |
soup = BeautifulSoup(html, "html.parser") | |
#find the jury duty message | |
message = soup.find("span", {"id" : "ctl02_lblMsg"}).contents[0] | |
#print the output | |
print message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment