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.

Medway.py 2.8 KiB

16 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import urllib2
  2. import urllib
  3. import urlparse
  4. import datetime, time
  5. from BeautifulSoup import BeautifulSoup
  6. from PlanningUtils import PlanningApplication, \
  7. PlanningAuthorityResults, \
  8. getPostcodeFromText
  9. date_format = "%d/%m/%Y"
  10. class MedwayParser:
  11. comment_email_address = "planning.representations@medway.gov.uk"
  12. def __init__(self, *args):
  13. self.authority_name = "Medway Council"
  14. self.authority_short_name = "Medway"
  15. self.base_url = "http://www.medway.gov.uk/index/environment/planning/planapp/planonline.htm"
  16. self._split_base_url = urlparse.urlsplit(self.base_url)
  17. self._results = PlanningAuthorityResults(self.authority_name, self.authority_short_name)
  18. def getResultsByDayMonthYear(self, day, month, year):
  19. search_date = datetime.date(year, month, day)
  20. search_date_string = search_date.strftime(date_format)
  21. "appstat=&decision=&appdec=&ward=&parish=&dadfrom=&dadto=&davfrom=01%2F06%2F2008&davto=02%2F06%2F2008&searchbut=Search"
  22. search_data = urllib.urlencode(
  23. [("searchtype", "1"),
  24. ("appstat", ""),
  25. ("decision", ""),
  26. ("appdec", ""),
  27. ("ward", ""),
  28. ("parish", ""),
  29. ("dadfrom", ""),
  30. ("dadto", ""),
  31. ("davfrom", search_date_string),
  32. ("davto", search_date_string),
  33. ("searchbut", "Search"),
  34. ]
  35. )
  36. split_search_url = self._split_base_url[:3] + (search_data, '')
  37. search_url = urlparse.urlunsplit(split_search_url)
  38. response = urllib2.urlopen(search_url)
  39. soup = BeautifulSoup(response.read())
  40. results_table = soup.find(text="Application No").parent.parent.parent
  41. trs = results_table.findAll("tr")[1:]
  42. tr_counter = 0
  43. while tr_counter < len(trs):
  44. tr = trs[tr_counter]
  45. if tr_counter % 2 == 0:
  46. application = PlanningApplication()
  47. application.date_received = search_date
  48. application.comment_url = self.comment_email_address
  49. tds = tr.findAll("td")
  50. application.info_url = urlparse.urljoin(self.base_url, tr.a['href'])
  51. application.council_reference = tr.a.string.strip()
  52. application.address = tds[1].string.strip()
  53. application.postcode = getPostcodeFromText(application.address)
  54. application.description = tds[2].string.strip()
  55. self._results.addApplication(application)
  56. tr_counter += 1
  57. return self._results
  58. def getResults(self, day, month, year):
  59. return self.getResultsByDayMonthYear(int(day), int(month), int(year)).displayXML()
  60. if __name__ == '__main__':
  61. parser = MedwayParser()
  62. print parser.getResults(02,6,2008)