#!/usr/bin/env python

from bs4 import BeautifulSoup
from collections import namedtuple
import argparse
import re
import requests

Court = namedtuple('Court', ['id', 'name'])
Hit = namedtuple('Hit', ['court_name', 'num_links'])

_list_of_courts = []


def _get_links_from_page(page_url, regex_pattern):
    html_doc = requests.get(page_url).text
    soup = BeautifulSoup(html_doc, "html.parser")
    pattern = re.compile(regex_pattern)
    return soup.find_all("a", href=pattern)


def _build_list_of_courts():
    page_url = "http://www.judici.com/courts/court_list.jsp"
    regex_pattern = r"\/courts\/cases\/case_search\.jsp\?court=\w+"
    links = _get_links_from_page(page_url, regex_pattern)
    for l in links:
        court_name = l.string
        court_id = l['href'][36:]
        c = Court(court_id, court_name)
        _list_of_courts.append(c)


def _search_courts(query_str):
    hits = []
    for c in _list_of_courts:
        page_url = "https://www.judici.com/courts/cases/case_search.jsp?" + \
            "court=" + c.id + query_str
        regex_pattern = r"\/courts\/cases\/case_information.jsp\?court="
        links = _get_links_from_page(page_url, regex_pattern)
        if (len(links) > 0):
            h = Hit(c.name, len(links))
            hits.append(h)
    return hits


def _search_courts_by_case_number(case_number):
    query_str = "&case_number=" + case_number
    return _search_courts(query_str)


def _search_courts_by_name(name):
    query_str = "&litigant_name=" + name
    return _search_courts(query_str)


def main(case_number, name):

    print("Building list of participating courts...", end=" ")
    _build_list_of_courts()
    print("list built!")

    hits = []

    if case_number:
        print("Searching courts for case number " + case_number
              + "... ", end=" ")
        hits = _search_courts_by_case_number(case_number)
    elif name:
        print("Searching courts for name " + name
              + "... ", end=" ")
        hits = _search_courts_by_name(name)

    if not hits:
        print("No hits.")

    for hit in hits:
        print("At least " + str(hit.num_links) + " hits in " + hit.court_name)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Illinois Judici Search')
    parser.add_argument('-c', '--casenumber', type=str,
                        help='Case number (e.g.: 2007TR111)')
    parser.add_argument('-n', '--name', type=str,
                        help='Name (e.g.: <NAME>)')
    args = parser.parse_args()

    if not args.casenumber and not args.name:
        print("Error: this script requires at least one argument.")
        print("Call script with:")
        print("python main.py --casenumber '2007TR111', or")
        print("python main.py --name '<NAME>'")
    else:
        main(args.casenumber, args.name)

#!/usr/bin/env python

from bs4 import BeautifulSoup
from collections import namedtuple
import argparse
import re
import requests

Court = namedtuple('Court', ['id', 'name'])
Hit = namedtuple('Hit', ['court_name', 'num_links'])

_list_of_courts = []


def _get_links_from_page(page_url, regex_pattern):
    html_doc = requests.get(page_url).text
    soup = BeautifulSoup(html_doc, "html.parser")
    pattern = re.compile(regex_pattern)
    return soup.find_all("a", href=pattern)


def _build_list_of_courts():
    page_url = "http://www.judici.com/courts/court_list.jsp"
    regex_pattern = r"\/courts\/cases\/case_search\.jsp\?court=\w+"
    links = _get_links_from_page(page_url, regex_pattern)
    for l in links:
        court_name = l.string
        court_id = l['href'][36:]
        c = Court(court_id, court_name)
        _list_of_courts.append(c)


def _search_courts(query_str):
    hits = []
    for c in _list_of_courts:
        page_url = "https://www.judici.com/courts/cases/case_search.jsp?" + \
            "court=" + c.id + query_str
        regex_pattern = r"\/courts\/cases\/case_information.jsp\?court="
        links = _get_links_from_page(page_url, regex_pattern)
        if (len(links) > 0):
            h = Hit(c.name, len(links))
            hits.append(h)
    return hits


def _search_courts_by_case_number(case_number):
    query_str = "&case_number=" + case_number
    return _search_courts(query_str)


def _search_courts_by_name(name):
    query_str = "&litigant_name=" + name
    return _search_courts(query_str)


def main(case_number, name):

    print("Building list of participating courts...", end=" ")
    _build_list_of_courts()
    print("list built!")

    hits = []

    if case_number:
        print("Searching courts for case number " + case_number
              + "... ", end=" ")
        hits = _search_courts_by_case_number(case_number)
    elif name:
        print("Searching courts for name " + name
              + "... ", end=" ")
        hits = _search_courts_by_name(name)

    if not hits:
        print("No hits.")

    for hit in hits:
        print("At least " + str(hit.num_links) + " hits in " + hit.court_name)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Illinois Judici Search')
    parser.add_argument('-c', '--casenumber', type=str,
                        help='Case number (e.g.: 2007TR111)')
    parser.add_argument('-n', '--name', type=str,
                        help='Name (e.g.: <NAME>)')
    args = parser.parse_args()

    if not args.casenumber and not args.name:
        print("Error: this script requires at least one argument.")
        print("Call script with:")
        print("python main.py --casenumber '2007TR111', or")
        print("python main.py --name '<NAME>'")
    else:
        main(args.casenumber, args.name)

#!/usr/bin/env python