#!/usr/bin/env python
#MUST BE RUN USING PYTHON3

#This script demonstrates parsing data from a tweet and doing something with that data.

#Import the TwythonStreamer class and Raspberry Pi GPIO functions
from twython import TwythonStreamer
import RPi.GPIO as GPIO

#Set the GPIO mode to BCM (addressed as GPIO outputs, not pin numbers)
GPIO.setmode(GPIO.BCM)
#Set GPIO 4 to output
GPIO.setup(4, GPIO.OUT)

#Import authentication information from auth.py
from auth import (
        conKey,
        conSec,
        accTok,
        accSec
)

#Create a new instance of the TwythonStreamer class to behave in a specific manner
class MyStreamer(TwythonStreamer):
    #If the streamer finds a tweet that meets the filter conditions, it runs the on_success method
    def on_success(self, data):
        #If there is some text in the tweet
        if 'text' in data:
            #Make the username user + screen name
            username = data['user']['screen_name']
            #Get the text of the tweet
            tweet = data['text']
            #Remove the name from the tweet
            newTweet =  tweet.replace('@HocProj', '')
            #Print the name and tweet
            print("@%s: %s" % (username, tweet))
            #If the tweet is an even number, LED off, otherwise LED on
            if (int(newTweet) % 2 == 0):
                GPIO.output(4,GPIO.LOW)
                print("Even")
            else:
                GPIO.output(4,GPIO.HIGH)
                print("Odd")

#Create a stream which inherits the properties of the MyStreamer class, with the authentication information
stream = MyStreamer(
        conKey,
        conSec,
        accTok,
        accSec
)

#Filter all statuses in the stream to only include ones which mention our handle
stream.statuses.filter(track='@HOCProj')
