Added a camera, and therefore some more code. It now tests for a file size before it tries to upload to Twitter.
import requestsfrom time import sleepfrom subprocess import callimport RPi.GPIO as GPIOimport twitterfrom picamera import PiCameraimport osdef tell_joke(): try: # Toggle red light GPIO.setup(red,GPIO.OUT,initial=GPIO.HIGH) # Setup variables voice_api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXX' #insert your key here. joke_api_url = 'https://icanhazdadjoke.com/' headers = {'user-agent': 'twitter.com/tellafunny','Accept': 'application/json'} # Get joke from API and assign it to a variable r = requests.get(joke_api_url,headers=headers) joke = r.json()['joke'] # Send joke to Voice RSS payload = {'key': voice_api_key, 'f': '44khz_16bit_mono', 'hl': 'en-us', 'c': 'WAV', 'src': joke } v = requests.get('https://api.voicerss.org',params=payload) # Creating wav file. f = open('/home/pi/Documents/response.wav','wb') f.write(v.content) f.close # Turn off red light GPIO.setup(red,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(blue,GPIO.OUT,initial=GPIO.HIGH) # Start recording camera.start_recording('/home/pi/Documents/reaction_video.h264') print('Started recording') # Play joke call(['aplay','/home/pi/Documents/response.wav']) # End recording camera.wait_recording(7) camera.stop_recording() # Wrap video in MP4 call(['chmod', '755', '/home/pi/Documents/reaction_video.h264']) call(['MP4Box', '-add', '/home/pi/Documents/reaction_video.h264', '/home/pi/Documents/video.mp4']) call(['chmod', '755', '/home/pi/Documents/video.mp4']) # Get file size statinfo = os.stat('/home/pi/Documents/video.mp4') if statinfo.st_size < 15000000: # Tweet joke and reaction video = open('/home/pi/Documents/video.mp4','rb') api.PostUpdate(status=joke,media=video) else: print('Video to large to upload.') # Delete files call(['rm', '/home/pi/Documents/reaction_video.h264']) call(['rm', '/home/pi/Documents/video.mp4']) # Toggle Lights GPIO.setup(red,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(blue,GPIO.OUT,initial=GPIO.LOW) except: print('Something broke.') GPIO.cleanup() # Delete files call(['rm', '/home/pi/Documents/reaction_video.h264']) call(['rm', '/home/pi/Documents/video.mp4']) # Setup GPIO ModeGPIO.setmode(GPIO.BCM)# Setup Cameracamera = PiCamera()camera.rotation = 180camera.resolution = (640,480)# Assign color names and variablesgreen=17blue=27red=22button=25# Assign Twitter variables. APP_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXX' # I will write up how to do this when I am done.APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'OAUTH_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'OAUTH_TOKEN_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXX'# Create Twitter objectapi = twitter.Api(consumer_key=APP_KEY,consumer_secret=APP_SECRET,access_token_key=OAUTH_TOKEN,access_token_secret=OAUTH_TOKEN_SECRET)# Set green light on indicating that everything is readyGPIO.setup(green,GPIO.OUT,initial=GPIO.HIGH)GPIO.setup(blue,GPIO.OUT,initial=GPIO.LOW)GPIO.setup(red,GPIO.OUT,initial=GPIO.LOW)GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_UP)print('Tellafunny Running.')try: while True: if GPIO.input(button)==0: GPIO.setup(green,GPIO.OUT,initial=GPIO.LOW) print('Getting joke.') tell_joke() print('Har har!') GPIO.setup(green,GPIO.OUT,initial=GPIO.HIGH) sleep(.2)finally: GPIO.cleanup()This is my first foray into Python. So far, I love it. Learning and playing with the code has been very natural. There were a few quirks to work through, but eventually I made them all work in an acceptable answer.
Some of the tools I am using:
The code below starts a green light to let you know that the program is running. Then when you press a button on the breadboard, a light turns red to let you know that it the program is getting a joke and putting it to voice. The light turns blue while it tells you joke, and then when it is done, back to green the light goes.
I still need to get a camera, and post the video to Twitter, but I think the code for those functions will likely be pretty straight forward.
import requestsfrom time import sleepfrom subprocess import callimport RPi.GPIO as GPIOdef tell_joke(): # Toggle red light GPIO.setup(red,GPIO.OUT,initial=GPIO.HIGH) # Setup variables voice_api_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX' # Your key from Voice RSS would go here. joke_api_url = 'https://icanhazdadjoke.com/' headers = {'user-agent': 'twitter.com/tellafunny','Accept': 'application/json'} # Get joke from API and assign it to a variable r = requests.get(joke_api_url,headers=headers) joke = r.json()['joke'] # Send joke to Voice RSS payload = {'key': voice_api_key, 'f': '44khz_16bit_mono', 'hl': 'en-us', 'c': 'WAV', 'src': joke } v = requests.get('https://api.voicerss.org',params=payload) # Creating wav file. f = open('/home/pi/Documents/response.wav','wb') f.write(v.content) f.close # Turn off red light GPIO.setup(red,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(blue,GPIO.OUT,initial=GPIO.HIGH) # Play joke call(['aplay','/home/pi/Documents/response.wav']) # Toggle Lights GPIO.setup(red,GPIO.OUT,initial=GPIO.LOW) GPIO.setup(blue,GPIO.OUT,initial=GPIO.LOW)# Setup GPIO ModeGPIO.setmode(GPIO.BCM)# Assign color names and variablesgreen=17blue=27red=22button=25# Set green light on indicating that everything is readyGPIO.setup(green,GPIO.OUT,initial=GPIO.HIGH)GPIO.setup(blue,GPIO.OUT,initial=GPIO.LOW)GPIO.setup(red,GPIO.OUT,initial=GPIO.LOW)GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_UP)print('Tellafunny Running.')try: while True: if GPIO.input(button)==0: GPIO.setup(green,GPIO.OUT,initial=GPIO.LOW) print('Getting joke.') tell_joke() print('Har har!') GPIO.setup(green,GPIO.OUT,initial=GPIO.HIGH) sleep(.2)finally: GPIO.cleanup()