Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

100 lines
3.2 KiB

  1. import urllib2
  2. import urllib
  3. import urlparse
  4. import datetime, time
  5. import cgi
  6. import re
  7. from BeautifulSoup import BeautifulSoup
  8. from PlanningUtils import PlanningApplication, \
  9. PlanningAuthorityResults, \
  10. getPostcodeFromText
  11. date_format = "%d/%m/%Y"
  12. class MaldonParser:
  13. comment_email_address = "dc.planning@maldon.gov.uk"
  14. info_url = "http://forms.maldon.gov.uk:8080/PlanApp/jsp/searchPlan.jsp"
  15. def __init__(self, *args):
  16. self.authority_name = "Maldon District Council"
  17. self.authority_short_name = "Maldon"
  18. self.base_url = "http://forms.maldon.gov.uk:8080/PlanApp/jsp/searchPlanApp-action.do"
  19. self._split_base_url = urlparse.urlsplit(self.base_url)
  20. self._results = PlanningAuthorityResults(self.authority_name, self.authority_short_name)
  21. def getResultsByDayMonthYear(self, day, month, year):
  22. search_date = datetime.date(year, month, day)
  23. search_date_string = search_date.strftime(date_format)
  24. search_data = urllib.urlencode(
  25. [("RegisteredDateFrom", search_date_string),
  26. ("RegisteredDateTo", search_date_string),
  27. ]
  28. )
  29. split_search_url = self._split_base_url[:3] + (search_data, '')
  30. search_url = urlparse.urlunsplit(split_search_url)
  31. response = urllib2.urlopen(search_url)
  32. soup = BeautifulSoup(response.read())
  33. # Not a very good way of finding the table, but it works for the moment.
  34. results_table = soup.find("table", cellpadding="5px")
  35. trs = results_table.findAll("tr")[1:]
  36. tr_counter = 0
  37. while tr_counter < len(trs):
  38. tr = trs[tr_counter]
  39. if tr_counter % 2 == 0:
  40. application = PlanningApplication()
  41. application.date_received = search_date
  42. application.comment_url = self.comment_email_address
  43. tds = tr.findAll("td")
  44. application.council_reference = tds[0].b.string.strip()
  45. application.address = ' '.join(tds[2].string.split())
  46. application.postcode = getPostcodeFromText(application.address)
  47. # This is what it ought to be, but you can't get there without a sodding cookie.
  48. # I guess we'll have to send people to the front page
  49. # application.info_url = urlparse.urljoin(self.base_url, tr.find("a", title="Click here to view application details")['href'])
  50. application.info_url = self.info_url
  51. else:
  52. description = tr.td.string
  53. if tr.td.string is not None:
  54. application.description = tr.td.string.strip()
  55. else:
  56. application.description = "Description Missing"
  57. self._results.addApplication(application)
  58. tr_counter += 1
  59. return self._results
  60. def getResults(self, day, month, year):
  61. return self.getResultsByDayMonthYear(int(day), int(month), int(year)).displayXML()
  62. if __name__ == '__main__':
  63. parser = MaldonParser()
  64. print parser.getResults(02,6,2008)
  65. # TODO
  66. # 1) Check that it works ok on a no results page.
  67. # 2) Email the council about non-linkable info page.