from datetime import datetime
from ftplib import FTP_TLS
from time import sleep
from subprocess import call
import time
import subprocess
import picamera
pic = 1
camera = picamera.PiCamera()
camera.resolution = (680, 480)
camera.rotation = 180
while True:
    now = datetime.now()
    now = str(now)
    now =now[0:10] + '_' + now[11:13] + '_' + now[14:16] + '_' + now[17:19]
    record = "arecord --device=hw:1 --format S16_LE --rate 44100 --duration=5 -c1 " + now + ".wav"
    convert = "lame -b 16 " + now + ".wav " + now + ".mp3"
    delete = "rm " + now + ".wav"
    delete2 = "rm " + now + ".mp3"
    print(now)
    now = str(now)
    #camera.start_preview()
    #sleep(2)
    camera.capture( '/home/pi/PHOTO/' + now + '.jpg')
    #camera.stop_preview()
    call([record], shell=True)
    call([convert], shell=True)
    sleep(6)
    call([delete], shell=True)
    print('photo taken')      
    s = FTP_TLS ('ftp.lafavre.us')
    s.login ('gear@lafavre.us', 'Iotisnumber1!')
    s.prot_p()
    filename = '/home/pi/PHOTO/' + now + '.jpg'
    ftppath = '/Aidan'
    s.cwd (ftppath)
    f= open (filename, 'rb')
    s.storbinary('STOR '+ now + '.jpg', f)
    filename = '/home/pi/' + now + '.mp3'
    ftppath = '/Aidan'
    s.cwd (ftppath)
    f= open (filename, 'rb')
    s.storbinary('STOR '+ now + '.mp3', f)
    call([delete2], shell=True)
    print('donemydude')
    sleep(8)
    
