Automatically exported from code.google.com/p/planningalerts
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

83 行
2.9 KiB

  1. import urllib2
  2. import urllib
  3. import urlparse
  4. import datetime
  5. import cgi
  6. from BeautifulSoup import BeautifulSoup
  7. from PlanningUtils import PlanningApplication, \
  8. PlanningAuthorityResults, \
  9. getPostcodeFromText
  10. search_date_format = "%d%m%y"
  11. reg_date_format = "%d/%m/%y"
  12. class BerwickParser:
  13. comments_email_address = "planning@berwick-upon-tweed.gov.uk"
  14. def __init__(self, *args):
  15. self.authority_name = "Berwick-upon-Tweed Borough Council"
  16. self.authority_short_name = "Berwick"
  17. self.base_url = "http://www.berwick-upon-tweed.gov.uk/planning/register/wl/%s.htm"
  18. self._results = PlanningAuthorityResults(self.authority_name, self.authority_short_name)
  19. def getResultsByDayMonthYear(self, day, month, year):
  20. search_day = datetime.date(year, month, day)
  21. monday_before = search_day - datetime.timedelta(search_day.weekday())
  22. thursday = monday_before + datetime.timedelta(3)
  23. if search_day.weekday() > 3: # i.e. It is friday, saturday, or sunday
  24. # We need to add a week
  25. thursday = thursday + datetime.timedelta(7)
  26. this_url = self.base_url %(thursday.strftime(search_date_format))
  27. # Now get the search page
  28. response = urllib2.urlopen(this_url)
  29. soup = BeautifulSoup(response.read())
  30. # Each app is stored in a table of its own. The tables don't have
  31. # any useful attributes, so we'll find all the NavigableString objects
  32. # which look like " Application Number:" and then look at the
  33. #tables they are in.
  34. nav_strings = soup.findAll(text=" Application Number:")
  35. for nav_string in nav_strings:
  36. application = PlanningApplication()
  37. application.council_reference = nav_string.findNext("p").string.strip()
  38. result_table = nav_string.findPrevious("table")
  39. application.date_received = datetime.datetime.strptime(result_table.find(text=" Registration Date: ").findNext("p").contents[0].strip(), reg_date_format)
  40. application.osgb_x = result_table.find(text=" Easting:").findNext("p").string.strip()
  41. application.osgb_y = result_table.find(text=" Northing:").findNext("p").string.strip()
  42. application.description = result_table.find(text=" Proposed Development:").findNext("p").string.strip()
  43. application.address = result_table.find(text=" Location:").findNext("p").string.strip()
  44. application.postcode = getPostcodeFromText(application.address)
  45. application.info_url = this_url
  46. application.comment_url = self.comments_email_address
  47. self._results.addApplication(application)
  48. return self._results
  49. def getResults(self, day, month, year):
  50. return self.getResultsByDayMonthYear(int(day), int(month), int(year)).displayXML()
  51. if __name__ == '__main__':
  52. parser = BerwickParser()
  53. print parser.getResults(21,5,2008)