tide_generator_100113.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import re
  2. import sys, os
  3. sys.path.append("/var/lib/django")
  4. from django.core.management import setup_environ
  5. from pbp_com import settings
  6. setup_environ(settings)
  7. import re
  8. import urllib
  9. import urllib2
  10. import time
  11. from datetime import datetime, timedelta
  12. import dateutil
  13. from almanac.models import Day, AstroChart, TideChart, TideDay, Tide
  14. from directory.models import Place
  15. from BeautifulSoup import BeautifulStoneSoup
  16. ### SET YOUR TIDE FILE NAME HERE ###
  17. tides = BeautifulStoneSoup(open("bar_harbor_2014.xml"))
  18. ### SET THE YEAR YOUR ARE UPLOADING HERE ###
  19. year = "2014"
  20. place = Place.objects.get(slug="bar-harbor-public-wharf")
  21. def fix_time(str, today, chart):
  22. """if chart.dst_start < today.date < chart.dst_end:
  23. delta=timedelta(hours=1)
  24. else:
  25. delta=timedelta(hours=0)"""
  26. return time.strftime(
  27. "%H:%M", datetime(*time.strptime(str, "%I:%M %p")[0:5]).timetuple()
  28. )
  29. """ End of fix_time() """
  30. tide_table = []
  31. try:
  32. chart = TideChart.objects.get(year__exact=year, place__exact=place)
  33. except TideChart.DoesNotExist:
  34. raise Exception(
  35. "Currently you have to manually add a tide chart to hold this info before in can be imported."
  36. )
  37. try:
  38. astro_chart = AstroChart.objects.get(year__exact=year)
  39. except AstroChart.DoesNotExist:
  40. raise Exception(
  41. "You must create an astronomical chart for the year you wish to input tide data for."
  42. )
  43. days = Day.objects.filter(date__year=chart.year)
  44. for day in days:
  45. print "Processing %s" % day.date
  46. days_tides = tides.findAll(text=re.sub("-", "/", str(day.date)))
  47. print days_tides
  48. try:
  49. tide_1 = days_tides[0].parent.parent
  50. except:
  51. pass
  52. tide_2 = days_tides[1].parent.parent
  53. tide_3 = days_tides[2].parent.parent
  54. try:
  55. tide_4 = days_tides[3].parent.parent
  56. except:
  57. pass
  58. tideday = TideDay.objects.create(day=day, tide_chart=chart)
  59. if tide_1:
  60. tideday.tide_1 = Tide.objects.create(
  61. time=fix_time(tide_1.time.contents[0], datetime.today(), astro_chart),
  62. level=tide_1.predictions_in_ft.contents[0],
  63. state=tide_1.highlow.contents[0],
  64. )
  65. tideday.tide_2 = Tide.objects.create(
  66. time=fix_time(tide_2.time.contents[0], datetime.today(), astro_chart),
  67. level=tide_2.predictions_in_ft.contents[0],
  68. state=tide_2.highlow.contents[0],
  69. )
  70. tideday.tide_3 = Tide.objects.create(
  71. time=fix_time(tide_3.time.contents[0], datetime.today(), astro_chart),
  72. level=tide_3.predictions_in_ft.contents[0],
  73. state=tide_3.highlow.contents[0],
  74. )
  75. if tide_4:
  76. tideday.tide_4 = Tide.objects.create(
  77. time=fix_time(tide_4.time.contents[0], datetime.today(), astro_chart),
  78. level=tide_4.predictions_in_ft.contents[0],
  79. state=tide_4.highlow.contents[0],
  80. )
  81. tideday.save()