refactor description parsing
[disclosr.git] / documents / genericScrapers.py
blob:a/documents/genericScrapers.py -> blob:b/documents/genericScrapers.py
--- a/documents/genericScrapers.py
+++ b/documents/genericScrapers.py
@@ -2,16 +2,20 @@
 sys.path.insert(0, os.path.join(os.path.dirname(__file__) or '.', '../'))
 import scrape
 from bs4 import BeautifulSoup
-import parsedatetime as pdt
 from time import mktime
-from datetime import datetime
 import feedparser
 import abc
+import unicodedata, re
+import dateutil
+from dateutil.parser import *
+from datetime import *
 
 class GenericDisclogScraper(object):
         __metaclass__ = abc.ABCMeta
 	agencyID = None
 	disclogURL = None
+	def remove_control_chars(self, input):
+		return "".join([i for i in input if ord(i) in range(32, 127)])
         def getAgencyID(self):
                 """ disclosr agency id """
 		if self.agencyID == None:
@@ -79,21 +83,34 @@
                 	descriptiontxt = descriptiontxt + " \n" + string
                 doc.update({'description': descriptiontxt})
 		return
+        def getTitle(self, content, entry, doc):
+                doc.update({'title': content.string})
+		return
+	def getTable(self, soup):
+		return soup.table
+	def getDate(self, content, entry, doc):
+		edate = parse(content.string.strip(), dayfirst=True, fuzzy=True).strftime("%Y-%m-%d")
+		print edate
+		doc.update({'date': edate})
+		return
 
 	def doScrape(self):
-		cal = pdt.Calendar()
 		foidocsdb = scrape.couch['disclosr-foidocuments']
 		(url,mime_type,content) = scrape.fetchURL(scrape.docsdb, self.getURL(), "foidocuments", self.getAgencyID())
 		if content != None:
 			if mime_type == "text/html" or mime_type == "application/xhtml+xml" or mime_type =="application/xml":
 			# http://www.crummy.com/software/BeautifulSoup/documentation.html
 				soup = BeautifulSoup(content)
-				for row in soup.table.find_all('tr'):
+				table = self.getTable(soup)
+				for row in table.find_all('tr'):
 					columns = row.find_all('td')
 					if len(columns) == self.getColumnCount():
 						(id, date, description, title, notes) = self.getColumns(columns)
 						print id.string
-						hash = scrape.mkhash(url+id.string)
+						if id.string == None:
+							hash = scrape.mkhash(self.remove_control_chars(url+date.string))
+						else:
+							hash = scrape.mkhash(self.remove_control_chars(url+id.string))
 						links = []
 						for atag in row.find_all("a"):
 							if atag.has_key('href'):
@@ -102,20 +119,13 @@
 							
 						if doc == None:
 							print "saving"
-                                                        dtresult = cal.parseDateText(date.string)
-							if len(dtresult) == 2:
-								(dtdate,dtr) = dtresult
-								print dtdate
-                                                        	edate = ""+str(dtdate[0])+'-'+str(dtdate[1])+'-'+str(dtdate[2])
-							else:
-								edate = datetime.strptime(date.string, "%d %B %Y").strftime("%Y-%m-%d")
-							doc = {'_id': hash, 'agencyID': self.getAgencyID(), 'url': self.getURL(), 'docID': id.string,
-			 				 "date": edate,"title": title.string}
-							self.getDescription(description,row, doc)
-
+							doc = {'_id': hash, 'agencyID': self.getAgencyID(), 'url': self.getURL(), 'docID': id.string}
                                 			if links != []:
                                         			doc.update({'links': links})
-                                			if notes != None:
+                                			self.getTitle(title,row, doc)
+                                			self.getDate(date,row, doc)
+							self.getDescription(description,row, doc)
+							if notes != None:
                                         			doc.update({ 'notes': notes.string})
 							foidocsdb.save(doc)
 						else: