from twython import Twython #imports the twitter library
from time import sleep #lets us set delays in our code
import os #allow us to do command line commands
import RPi.GPIO as GPIO #RaspberryPi IO pins
import picamera #camera library
from datetime import datetime #import the library that allows you do get date

GPIO.setmode(GPIO.BCM) # set the mode for pin numbering
camera = picamera.PiCamera() #create camera object
camera.brightness = 53 #set brightness (change based on your light conditions)
camera.annotate_foreground = picamera.Color(y=0.1, u=0, v=0) #color for annotations
camera.annotate_text_size = 70 #annotation size
button = 4 #take photo button pin
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)#sets button as input
#Twitter API tokens that we will retrieve in the next step
ck=''
cs=''
at=''
ats=''
t = Twython(ck, cs, at, ats)#creates object based on the tokens
try:
        while True:#loop
            input_state = GPIO.input(button)#get state of  button
            if input_state == False:#if button is pressed
                c = GPIO.wait_for_edge(button, GPIO.RISING, timeout=3000, bouncetime=300)#waits for release
                if channel is None:#if not released, break (which shuts down pi)
                   break
                else:#take a picture and tweet
                    camera.start_preview()#begin preview (if RPi just turned on). Allows user to change settings on Pi before covering up the whole screen.
                    camera.annotate_text = 'Get Ready! 5'#countdown
                    sleep(1)
                    camera.annotate_text = 'Get Ready! 4'
                    sleep(1)
                    camera.annotate_text = 'Get Ready! 3'
                    sleep(1)
                    camera.annotate_text = 'Get Ready! 2'
                    sleep(1)
                    camera.annotate_text = 'Get Ready! 1'
                    sleep(1)
                    camera.annotate_text = ''#clear annotated text
                    camera.capture("photo.jpg")#take photo
                    d = datetime.now()#get current date
                    camera.annotate_text = 'Uploading...'#let user know that it is uploading
                    file = open('photo.jpg', 'rb')#save file to variable
                    response = t.upload_media(media=file)#upload image to twitter
                    if len(str(d.minute)) == 1:#add 0 to time if minutes is less than 10
                        minute = '0'+str(d.minute)
                    else:
                        minute = str(d.minute)#if the minute is more than 10, do not add 0
                    s = "TWITTER MESSAGE OF YOUR CHOICE #ALMOSTDONE"
                    t.update_status(status=s, media_ids=[response['media_id']])#posts the tweet
                    camera.annotate_text = ''#clear annotations
                    os.system('sudo rm /home/pi/Desktop/photo.jpg')#delete the photo from Raspberry Pi
        camera.stop_preview()#if stop button is pressed, stop camera preview
        GPIO.cleanup()#cleanup pins
        os.system("sudo shutdown -h now")#shutdown
except KeyboardInterrupt:#interrupt Ctrl-C. Only works if run from terminal. Otherwise, just hold the button for shutdown.
    print('stopped')


