#################################
# Twitter Wiggler: Twitter Plays With Bug The Cat
# v1.0
# python3 <--- remember that for print statements!
# Nadine Lessio and Bijun Chen
#
# Some Limitations:
# --> This only works if your account is public
#################################

### IMPORTS  #########################################################################

import random, sys, json, datetime, re, os, unicodedata, logging, time, struct, requests
import serial
from random import choice
from time import sleep

### twython ###
from twython import Twython, TwythonError
from twython import TwythonStreamer

### LOGGING ##########################################

logging.basicConfig(filename='twitterWarn.log',level=logging.INFO)

### SERIAL ############################################

try:
	ser = serial.Serial('/dev/cu.usbmodem1421', 9600, timeout=0)	## open port
	time.sleep(2)													## sleep two seconds
	print("Connection to arduino established succesfully!\n")	## signal success
	
except Exception as e:
	print(e)
	#sys.exit() 														## exit the program if there is an error


### GET KEYS  #########################################################################

### local ###
### try and find a way to do os.environ things locally
keys = []

with open('keys.txt','r') as my_file:
	keys = my_file.read().splitlines()

APP_KEY = keys[0]
APP_SECRET = keys[1]
OAUTH_TOKEN = keys[2]
OAUTH_TOKEN_SECRET = keys[3]

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.verify_credentials()


### CAT STREAMER TRACKER ###############################################################

class catStream(TwythonStreamer):
	""" Twitter Streamer Class """

	## can be accessed by all methods in the class
	numberOfReplies = 0												# number of replies
	topLimit = 20													# setting this to 20 but could be like 100 or 1000 

	def on_success(self, data):

		if 'text' in data:

			## get your data as a str
			body = data['text']
			user = data['user']['screen_name']

			## set up some specific things to track
			acct = "@smolcatbug"
	
			if body.startswith(acct):								# if it is an @reply not a mention 	
				bodyStrip = re.sub(r'[^\w\s]','',body.lower())		# strip everything but words
				
				self.numberOfReplies+=1								# update the counter by 1

				if not self.numberOfReplies % 2:					# update the position every other tweet
					print("multiple of two:",self.numberOfReplies)
					ser.write(struct.pack('>B',5))	
				
				
				## this will only find the first incident of each word. whatever comes first. ok with that. 
				## particular comands will not increase position. 

				if "faster" in bodyStrip:
					print("faster")
					ser.write(struct.pack('>B', 3))					# any one can increase servo speed
				elif "slower" in bodyStrip:
					print("slower")
					ser.write(struct.pack('>B', 4))					# anyone can decrease servo speed
				elif "stop" in bodyStrip and user == "smolcatbug":
					print("stop")
					ser.write(struct.pack('>B', 7))					# only smolcatbug can stop the servo
				elif "go" in bodyStrip and user == "smolcatbug":
					print("go")
					ser.write(struct.pack('>B', 2))					# only smolcatbug can start the servo
				else:
					print("no specific commands")
					

		## This is to basically manipulate people to keep interacting with it.
		## it also puts in some natural high and low movements into the toy

		if self.numberOfReplies >= self.topLimit:
			self.numberOfReplies = self.topLimit	# keep it high.
			self.hard_reset()						# start winding it down ?

		print("!on_success: ", self.numberOfReplies)

	def hard_reset(self):
		""" reset everything """
		ser.write(struct.pack('>B', 8))
		self.numberOfReplies = 0;			# reset the reply number
		self.windDownCount = 3				# reset the wind down count 

	def on_error(self, status_code, data):
		""" Print The Error Code """
		print("error_code: ",status_code)


cStream = catStream(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
cStream.statuses.filter(track="@smolcatbug")								 # only works if you are public

24951f807ec2a7747bb3f7cf04ffd6f93c4920b2
<mask token>


class catStream(TwythonStreamer):
    <mask token>
    <mask token>
    <mask token>

    def on_success(self, data):
        if 'text' in data:
            body = data['text']
            user = data['user']['screen_name']
            acct = '@smolcatbug'
            if body.startswith(acct):
                bodyStrip = re.sub('[^\\w\\s]', '', body.lower())
                self.numberOfReplies += 1
                if not self.numberOfReplies % 2:
                    print('multiple of two:', self.numberOfReplies)
                    ser.write(struct.pack('>B', 5))
                if 'faster' in bodyStrip:
                    print('faster')
                    ser.write(struct.pack('>B', 3))
                elif 'slower' in bodyStrip:
                    print('slower')
                    ser.write(struct.pack('>B', 4))
                elif 'stop' in bodyStrip and user == 'smolcatbug':
                    print('stop')
                    ser.write(struct.pack('>B', 7))
                elif 'go' in bodyStrip and user == 'smolcatbug':
                    print('go')
                    ser.write(struct.pack('>B', 2))
                else:
                    print('no specific commands')
        if self.numberOfReplies >= self.topLimit:
            self.numberOfReplies = self.topLimit
            self.hard_reset()
        print('!on_success: ', self.numberOfReplies)
    <mask token>
    <mask token>


<mask token>

<mask token>


class catStream(TwythonStreamer):
    <mask token>
    <mask token>
    <mask token>

    def on_success(self, data):
        if 'text' in data:
            body = data['text']
            user = data['user']['screen_name']
            acct = '@smolcatbug'
            if body.startswith(acct):
                bodyStrip = re.sub('[^\\w\\s]', '', body.lower())
                self.numberOfReplies += 1
                if not self.numberOfReplies % 2:
                    print('multiple of two:', self.numberOfReplies)
                    ser.write(struct.pack('>B', 5))
                if 'faster' in bodyStrip:
                    print('faster')
                    ser.write(struct.pack('>B', 3))
                elif 'slower' in bodyStrip:
                    print('slower')
                    ser.write(struct.pack('>B', 4))
                elif 'stop' in bodyStrip and user == 'smolcatbug':
                    print('stop')
                    ser.write(struct.pack('>B', 7))
                elif 'go' in bodyStrip and user == 'smolcatbug':
                    print('go')
                    ser.write(struct.pack('>B', 2))
                else:
                    print('no specific commands')
        if self.numberOfReplies >= self.topLimit:
            self.numberOfReplies = self.topLimit
            self.hard_reset()
        print('!on_success: ', self.numberOfReplies)

    def hard_reset(self):
        """ reset everything """
        ser.write(struct.pack('>B', 8))
        self.numberOfReplies = 0
        self.windDownCount = 3

    def on_error(self, status_code, data):
        """ Print The Error Code """
        print('error_code: ', status_code)


<mask token>

<mask token>
logging.basicConfig(filename='twitterWarn.log', level=logging.INFO)
try:
    ser = serial.Serial('/dev/cu.usbmodem1421', 9600, timeout=0)
    time.sleep(2)
    print('Connection to arduino established succesfully!\n')
except Exception as e:
    print(e)
<mask token>
with open('keys.txt', 'r') as my_file:
    keys = my_file.read().splitlines()
<mask token>
twitter.verify_credentials()


class catStream(TwythonStreamer):
    """ Twitter Streamer Class """
    numberOfReplies = 0
    topLimit = 20

    def on_success(self, data):
        if 'text' in data:
            body = data['text']
            user = data['user']['screen_name']
            acct = '@smolcatbug'
            if body.startswith(acct):
                bodyStrip = re.sub('[^\\w\\s]', '', body.lower())
                self.numberOfReplies += 1
                if not self.numberOfReplies % 2:
                    print('multiple of two:', self.numberOfReplies)
                    ser.write(struct.pack('>B', 5))
                if 'faster' in bodyStrip:
                    print('faster')
                    ser.write(struct.pack('>B', 3))
                elif 'slower' in bodyStrip:
                    print('slower')
                    ser.write(struct.pack('>B', 4))
                elif 'stop' in bodyStrip and user == 'smolcatbug':
                    print('stop')
                    ser.write(struct.pack('>B', 7))
                elif 'go' in bodyStrip and user == 'smolcatbug':
                    print('go')
                    ser.write(struct.pack('>B', 2))
                else:
                    print('no specific commands')
        if self.numberOfReplies >= self.topLimit:
            self.numberOfReplies = self.topLimit
            self.hard_reset()
        print('!on_success: ', self.numberOfReplies)

    def hard_reset(self):
        """ reset everything """
        ser.write(struct.pack('>B', 8))
        self.numberOfReplies = 0
        self.windDownCount = 3

    def on_error(self, status_code, data):
        """ Print The Error Code """
        print('error_code: ', status_code)


<mask token>
cStream.statuses.filter(track='@smolcatbug')

import random, sys, json, datetime, re, os, unicodedata, logging, time, struct, requests
import serial
from random import choice
from time import sleep
from twython import Twython, TwythonError
from twython import TwythonStreamer
logging.basicConfig(filename='twitterWarn.log', level=logging.INFO)
try:
    ser = serial.Serial('/dev/cu.usbmodem1421', 9600, timeout=0)
    time.sleep(2)
    print('Connection to arduino established succesfully!\n')
except Exception as e:
    print(e)
keys = []
with open('keys.txt', 'r') as my_file:
    keys = my_file.read().splitlines()
APP_KEY = keys[0]
APP_SECRET = keys[1]
OAUTH_TOKEN = keys[2]
OAUTH_TOKEN_SECRET = keys[3]
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.verify_credentials()


class catStream(TwythonStreamer):
    """ Twitter Streamer Class """
    numberOfReplies = 0
    topLimit = 20

    def on_success(self, data):
        if 'text' in data:
            body = data['text']
            user = data['user']['screen_name']
            acct = '@smolcatbug'
            if body.startswith(acct):
                bodyStrip = re.sub('[^\\w\\s]', '', body.lower())
                self.numberOfReplies += 1
                if not self.numberOfReplies % 2:
                    print('multiple of two:', self.numberOfReplies)
                    ser.write(struct.pack('>B', 5))
                if 'faster' in bodyStrip:
                    print('faster')
                    ser.write(struct.pack('>B', 3))
                elif 'slower' in bodyStrip:
                    print('slower')
                    ser.write(struct.pack('>B', 4))
                elif 'stop' in bodyStrip and user == 'smolcatbug':
                    print('stop')
                    ser.write(struct.pack('>B', 7))
                elif 'go' in bodyStrip and user == 'smolcatbug':
                    print('go')
                    ser.write(struct.pack('>B', 2))
                else:
                    print('no specific commands')
        if self.numberOfReplies >= self.topLimit:
            self.numberOfReplies = self.topLimit
            self.hard_reset()
        print('!on_success: ', self.numberOfReplies)

    def hard_reset(self):
        """ reset everything """
        ser.write(struct.pack('>B', 8))
        self.numberOfReplies = 0
        self.windDownCount = 3

    def on_error(self, status_code, data):
        """ Print The Error Code """
        print('error_code: ', status_code)


cStream = catStream(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
cStream.statuses.filter(track='@smolcatbug')

#################################
# Twitter Wiggler: Twitter Plays With Bug The Cat
# v1.0
# python3 <--- remember that for print statements!
# Nadine Lessio and Bijun Chen
#
# Some Limitations:
# --> This only works if your account is public
#################################

### IMPORTS  #########################################################################

import random, sys, json, datetime, re, os, unicodedata, logging, time, struct, requests
import serial
from random import choice
from time import sleep

### twython ###
from twython import Twython, TwythonError
from twython import TwythonStreamer

### LOGGING ##########################################

logging.basicConfig(filename='twitterWarn.log',level=logging.INFO)

### SERIAL ############################################

try:
	ser = serial.Serial('/dev/cu.usbmodem1421', 9600, timeout=0)	## open port
	time.sleep(2)													## sleep two seconds
	print("Connection to arduino established succesfully!\n")	## signal success
	
except Exception as e:
	print(e)
	#sys.exit() 														## exit the program if there is an error


### GET KEYS  #########################################################################

### local ###
### try and find a way to do os.environ things locally
keys = []

with open('keys.txt','r') as my_file:
	keys = my_file.read().splitlines()

APP_KEY = keys[0]
APP_SECRET = keys[1]
OAUTH_TOKEN = keys[2]
OAUTH_TOKEN_SECRET = keys[3]

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.verify_credentials()


### CAT STREAMER TRACKER ###############################################################

class catStream(TwythonStreamer):
	""" Twitter Streamer Class """

	## can be accessed by all methods in the class
	numberOfReplies = 0												# number of replies
	topLimit = 20													# setting this to 20 but could be like 100 or 1000 

	def on_success(self, data):

		if 'text' in data:

			## get your data as a str
			body = data['text']
			user = data['user']['screen_name']

			## set up some specific things to track
			acct = "@smolcatbug"
	
			if body.startswith(acct):								# if it is an @reply not a mention 	
				bodyStrip = re.sub(r'[^\w\s]','',body.lower())		# strip everything but words
				
				self.numberOfReplies+=1								# update the counter by 1

				if not self.numberOfReplies % 2:					# update the position every other tweet
					print("multiple of two:",self.numberOfReplies)
					ser.write(struct.pack('>B',5))	
				
				
				## this will only find the first incident of each word. whatever comes first. ok with that. 
				## particular comands will not increase position. 

				if "faster" in bodyStrip:
					print("faster")
					ser.write(struct.pack('>B', 3))					# any one can increase servo speed
				elif "slower" in bodyStrip:
					print("slower")
					ser.write(struct.pack('>B', 4))					# anyone can decrease servo speed
				elif "stop" in bodyStrip and user == "smolcatbug":
					print("stop")
					ser.write(struct.pack('>B', 7))					# only smolcatbug can stop the servo
				elif "go" in bodyStrip and user == "smolcatbug":
					print("go")
					ser.write(struct.pack('>B', 2))					# only smolcatbug can start the servo
				else:
					print("no specific commands")
					

		## This is to basically manipulate people to keep interacting with it.
		## it also puts in some natural high and low movements into the toy

		if self.numberOfReplies >= self.topLimit:
			self.numberOfReplies = self.topLimit	# keep it high.
			self.hard_reset()						# start winding it down ?

		print("!on_success: ", self.numberOfReplies)

	def hard_reset(self):
		""" reset everything """
		ser.write(struct.pack('>B', 8))
		self.numberOfReplies = 0;			# reset the reply number
		self.windDownCount = 3				# reset the wind down count 

	def on_error(self, status_code, data):
		""" Print The Error Code """
		print("error_code: ",status_code)


cStream = catStream(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
cStream.statuses.filter(track="@smolcatbug")								 # only works if you are public
