Commits

Mark Sundstrom committed 2f0a079

save Avocet's grab_image.py

Comments (0)

Files changed (2)

Avocet/grab_image.py

+#!/usr/bin/env python
+import sys
+import os
+import requests
+import datetime
+import time
+import collections
+import logging
+import logging_tree
+
+TIMELAPSE = os.path.expanduser('~/Desktop/timelapse')
+assert os.path.exists(TIMELAPSE)
+HERE = os.path.join(os.path.dirname(__file__))
+SUN_MOON_PATH = os.path.join(HERE, 'sun-moon.txt')
+assert os.path.exists(SUN_MOON_PATH)
+LOG = os.path.join(HERE, 'timelapse.log')
+
+logging.basicConfig(level=logging.DEBUG, filename=LOG,
+    format="%(asctime)s %(levelname)s %(name)s %(message)s")
+logging.info('{} startup'.format(sys.argv[0]))
+
+SunMoon = collections.namedtuple('SunMoon',
+    'sun_az sun_alt moon_az moon_alt')
+
+sun_moon = []
+beginning = datetime.datetime(2013, 1, 1)
+
+with open(SUN_MOON_PATH) as f:
+    for line in f:
+        fields = map(int, line.strip().split(','))
+        sun_moon.append(SunMoon(*fields))
+logging.debug('{} rows of sun-moon data'.format(len(sun_moon)))
+
+
+def time_index(now):
+    seconds = (now - beginning).total_seconds()
+    minutes = seconds / 60
+    index = int(minutes / 4)
+    return index
+
+logging.getLogger('requests').setLevel(logging.WARNING)
+#logging_tree.printout()
+
+while True:
+    now = datetime.datetime.utcnow()
+    index = time_index(now)
+    data = sun_moon[index]
+    logging.debug("{:%a %b %d %H:%M:%S} [{:5d}] Sun={:+3d} Moon={:+3d}".format(
+        now, index, data.sun_alt, data.moon_alt))
+    r = requests.get('http://admin@192.168.0.20/image.jpg')
+    if r.status_code != 200:
+        logging.error("Unexpected error: {0.status_code}".format(r))
+    with open(os.path.join(HERE, 'webcam.jpg'), 'wb') as f:
+        f.write(r.content)
+    if data.sun_alt > -6 or data.moon_alt > 0:
+        folder = os.path.join(TIMELAPSE, now.strftime('%Y%m%d'))
+        if not os.path.exists(folder):
+            logging.info("Creating folder {}".format(folder))
+            os.makedirs(folder)
+        name = 'river{:%Y%m%d%H%M%S}.jpg'.format(now)
+        path = os.path.join(folder, name)
+        with open(path, 'wb') as f:
+            f.write(r.content)
+        print(path)
+    if data.sun_alt > -6:
+        delay = 20
+    else:
+        if data.moon_alt > 0:
+            delay = 180
+        else:
+            delay = 60
+    time.sleep(delay)
+
 #!/bin/bash
+# Get all the images
 rsync -av \
     mark@avocet.local:Desktop/timelapse/ \
     /Volumes/Extra/timelapse/images
+
+# Make sure we have the latest version of Avocet's grab_image.py
+rsync -av \
+    mark@avocet.local:Desktop/venv/grab_image.py \
+    ~/hg/timelapse/Avocet/grab_image.py
+
+# Get Avocet's timelapse log file
+mkdir -p ~/Library/Logs/Avocet
+rsync -av \
+    mark@avocet.local:Desktop/venv/timelapse.log \
+    ~/Library/Logs/Avocet/timelapse.log