tide_generator.py 2.7 KB

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