コード例 #1
0
	try:
		# Setting up a connection to Twitter using the Twitter API
		api = TwitterAPI(consumer_key, consumer_secret, access_token, access_token_secret)

		print bcolors.OKGREEN + "Initialisation OK!" + bcolors.ENDC
		print bcolors.OKBLUE + 'Twitter Stream Request running' + bcolors.ENDC

		# A request object which opens a stream to Twitter tracking the hashtag in question
		r = api.request('statuses/filter', {'track':stringToTrack})

		# Checks if text within the stream item is populated and issues a command to the Arduino
		for item in r.get_iterator():
			if 'text' in item:
				print item['user']['screen_name'].encode('utf-8') + ' tweeted: ' + item['text'].encode('utf-8')# Print screen name and the tweet text

				# It is possible to check the tweets for further commands using regular expressions to send multiple commands to the Arduino
				
				if availableArduino:
					print "Arduino turning on the LED"
					ser.write(bytes(1)) # The command is a simple byte intepretation of the integer 1
					sleep(arduinoWaitTime) # Wait before sending next command
					ser.write(bytes(0))
					sleep(arduinoWaitTime) # Wait before sending next command
	except IncompleteRead:
		# Oh well, reconnect and keep trucking
		print "IncompleteRead occurred"
	except KeyboardInterrupt:
		# Or however you want to exit this loop
		api.disconnect()
		exit()