'''
CourtID: ill
Court Short Name: Ill.
Author: Rebecca Fordon
Reviewer: 
History:
* 2016-06-22: Created by Rebecca Fordon
'''

from datetime import datetime
import re
from juriscraper.OralArgumentSite import OralArgumentSite


class Site(OralArgumentSite):
    def __init__(self, *args, **kwargs):
        super(Site, self).__init__(*args, **kwargs)
        self.court_id = self.__module__
        self.url = 'http://www.illinoiscourts.gov/Media/On_Demand.asp' 

    def _get_download_urls(self):
        path = "//table[4]//table//tr[position()>1]/td[6]//a/@href"
        return list(self.html.xpath(path))

    def _get_case_dates(self):
        dates = []
        path = "//table[4]//table//tr[position()>1]/td[1]//div/text()"
        for s in self.html.xpath(path):
            date_format = '%m/%d/%y'
            try: 
                d = datetime.strptime(s, date_format).date()
                dates.append(d)
            except ValueError: 
                print(ValueError)
                print(s)
                continue
        return dates

    def _get_case_names(self):
        path = '//table[4]//table//tr[position()>1]/td[3]//div/text()'
        cases = []
        for case in self.html.xpath(path):
            if case.strip():
                cases.append(case)
        return cases

    def _get_docket_numbers(self):
        path = "//table[4]//table//tr[position()>1]/td[2]//div"  # right now this is giving a problem because one of the fields has a new line in the middle of it
        dockets = []
        for docket in self.html.xpath(path): 
            docket = docket.text_content() 
            dockets.append(docket)
        return dockets

@@ -66,16 +66,28 @@
 viewer: 
+Mike Lissner
 %0AHistory
@@ -167,18 +167,8 @@
 ime%0A
-import re%0A
 from
@@ -456,17 +456,94 @@
 and.asp'
- 
+%0A        self.xpath_root = '(//table%5B@id=%22nicetable%22%5D)%5B2%5D//tr%5Bposition() %3E 1%5D'
 %0A%0A    de
@@ -589,52 +589,35 @@
 h = 
-%22//table%5B4%5D//table//tr%5Bposition()%3E1%5D
+self.xpath_root + %22
 /td%5B6%5D//
 a/@h
@@ -612,18 +612,16 @@
 /td%5B6%5D//
-a/
 @href%22%0A 
@@ -732,52 +732,35 @@
 h = 
-%22//table%5B4%5D//table//tr%5Bposition()%3E1%5D
+self.xpath_root + %22
 /td%5B1%5D//
 div/
@@ -755,20 +755,16 @@
 /td%5B1%5D//
-div/
 text()%22%0A
@@ -807,45 +807,8 @@
 h):%0A
-            date_format = '%25m/%25d/%25y'%0A
     
@@ -819,17 +819,16 @@
     try:
- 
 %0A       
@@ -865,19 +865,18 @@
 (s, 
-date_format
+'%25m/%25d/%25y'
 ).da
@@ -884,40 +884,8 @@
 e()%0A
-                dates.append(d)%0A
     
@@ -910,17 +910,16 @@
 ueError:
- 
 %0A       
@@ -931,27 +931,17 @@
     
-print(ValueError)%0A 
+continue%0A
     
@@ -952,19 +952,13 @@
     
-   print(s)
+else:
 %0A   
@@ -966,32 +966,39 @@
             
-continue
+dates.append(d)
 %0A        ret
@@ -1058,52 +1058,35 @@
 h = 
-'//table%5B4%5D//table//tr%5Bposition()%3E1%5D
+self.xpath_root + '
 /td%5B3%5D//
 div/
@@ -1085,12 +1085,8 @@
 3%5D//
-div/
 text
@@ -1291,155 +1291,34 @@
 h = 
-%22//table%5B4%5D//table//tr%5Bposition()%3E1%5D/td%5B2%5D//div%22  # right now this is giving a problem because one of the fields has a new line in the middle of it
+self.xpath_root + %22/td%5B2%5D%22
 %0A   
@@ -1379,17 +1379,16 @@
 h(path):
- 
 %0A       
@@ -1422,17 +1422,16 @@
 ontent()
- 
 %0A       

Clean up to ill. Generally great work. A fine first scraper.