##############################################################
#
# Tweeting Christmas Tree Code
# 2011 Randy Sarafan
#
# For more information visit:
# http://www.instructables.com/id/Tweeting-Christmas-Tree/
#
# This code is in the public domain 
# and you can use it however you want.
# However, I politely ask that if you modify this code
# and do something cool, please credit the original. 
# That would be appreciated.
#
##############################################################

import serial
import twitter
import time

#setup the serial port. 
#Don't forget to replace "copyandpastefromArduinoIDE" with name of port
ser = serial.Serial('/dev/tty.copyandpastefromArduinoIDE', 9600, timeout=1)

#Access the Twitter API with your unique key
api = twitter.Api(consumer_key='consumerkeyhere', consumer_secret='consumersecrethere', access_token_key='accesskey', access_token_secret='accesssecret')




while(1):
  #Look at @ mentions from Twitter api
  TweetAtMe = api.GetMentions()
  TweetList = [u.text for u in TweetAtMe]

  #Look at most recent @ mention
  RecentPost = TweetList[0]

  print RecentPost

  #splits the most recent post into discreet words
  #and stores these words in a list
  CurrentTweetList = RecentPost.split()

  #checks to see what #colors are in the list
  #and sends serial commands to the Arduino accordingly
  #currently checking for three colors, but capable of 4 (see below)
  
  if "#red" in CurrentTweetList:
    print "list contains", "#red"
    ser.write('a')
  else:
    print "no white"
    ser.write('b')

  if "#green" in CurrentTweetList:
    print "list contains", "#green"
    ser.write('c')
  else:
    print "no red"
    ser.write('d')

  if "#white" in CurrentTweetList:
    print "list contains", "#white"
    ser.write('e')
  else:
    print "no white"
    ser.write('f')

#  Conditional statement for unused color port
#  Set it to be whatever color you like

#  if "#blue" in CurrentTweetList:
#    print "list contains", "#blue"
#    ser.write('g')
#  else:
#    print "no blue"
#    ser.write('h')


  #Checks the current status of the rate at which you are allowed to access Twitter
  #Delays for minimum rate limit wait time plus one second
  #This allows you to ping twitter as much as possible without exceeding the limit and getting blocked

  rateLimitWait = api.MaximumHitFrequency()
  print rateLimitWait
  time.sleep(rateLimitWait + 1) 
  