Home | Login
9/8/2014

Phoenix Timelapse Tumblr

#! /usr/bin/python

# Script to download pictures and make a gif

from urllib import urlretrieve
from os import system
from time import sleep
import pytumblr

fiveMinuteCounter = 0 
freewayCameraId = 1

threeHourCounter = 1

blogName = "phoenix-timelapse.tumblr.com"

# Authenticate via OAuth
client = pytumblr.TumblrRestClient(
  # Secret API Keys go here
)

homePath = '/home/nak/az511lapse/frames/'

def generateGif():
  path = homePath + str(threeHourCounter) + '/'

  system('convert -coalesce -layers optimize -scale 200 ' + path + '*.jpg ' + path + 'out.gif')
  return

def postPhotoSet():

  # Post photoset of gifs
  postData = []
  postTags = ['cam-' + str(freewayCameraId), 'traffic cam']

  for i in range(1,5):
    gifPath = homePath + str(i) + '/out.gif'
    postData.append(gifPath)
  
  client.create_photo(blogName, tags=postTags, data=postData)
  system('rm frames/1/* frames/2/* frames/3/* frames/4/*')  # delete all the old gifs and frames
  return

def downloadCam():
  global freewayCameraId
  global fiveMinuteCounter
  print "download id:" + str(freewayCameraId)

  # URL to desired camera
  url = 'http://vods.az511.com/adot_' + str(freewayCameraId).zfill(3) + '.jpg'

  path = homePath + str(threeHourCounter) + '/'
  frameNumber = str(fiveMinuteCounter).zfill(2)

  try:
    httpobj = urlretrieve(url, path + frameNumber  + '.jpg');

    if httpobj[1].gettype() == 'text/plain':  #if the camera is down
      freewayCameraId += 1
      downloadCam()

  except:
    print 'Error downloading: ' + url
    system('rm frames/' + str(threeHourCounter) + '/*')
    freewayCameraId += 1
    fiveMinuteCounter = 0
    downloadCam()
    pass

  return


while(1):

  fiveMinuteCounter += 1
  if fiveMinuteCounter >= 37:

    if freewayCameraId > 194:  # switch to the next camera
      freewayCameraId = 1
    else:
      freewayCameraId += 1

    fiveMinuteCounter = 0

    # At this point we've taken 36 pictures
    generateGif()

    threeHourCounter += 1
    if threeHourCounter >= 5:
      # Here we have 4 directories 1 - 4 with a gif for each
      threeHourCounter = 1

      # post to tumblr
      postPhotoSet()

  # Download Camera JPG
  downloadCam()

  # Sleep
  sleep(120)

I should re-write this sometime.  It still gets stuck in random infinite loops sometimes when it can't download a picture or something...

#python #tumblr #timelapse #code