from datetime import datetime
from ftplib import FTP_TLS
from time import sleep
from subprocess import call
import time
import subprocess
import picamera
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
print('welcome to IotChat 3.1.4 Open Alpha')
password = "42"
if password == '42':
    print('yay')
    name = input('what you name')
    name = str(name)
    pic = 1
    camera = picamera.PiCamera()
    camera.resolution = (680, 480)
    camera.rotation = 180
    while True:
        while True:
            note = input('do you want to inform others you are on the webchat(yes or not)')
            if (note == ('yes')):
                who = input('who should be notifided(put email)')
                fromaddr = "42spaceoddity@gmail.com"
                toaddr = who
                msg = MIMEMultipart ()
                sub = 'WebChat'
                sub = str(sub)
                msg['From'] = fromaddr
                msg['To'] = toaddr
                msg['Subject'] = 'webchat'
                body = (name + 'is on the web chat server')
                msg.attach(MIMEText (body, 'plain'))
                p = MIMEBase('application', 'octet-stream')
                encoders.encode_base64(p)
                p.add_header('Content-Disposition', "attachment; filename= %s" % filename)
                msg.attach(p)
                s = smtplib.SMTP('smtp.gmail.com', 587)
                s.starttls()
                s.login(fromaddr, "indy dog")
                text = msg.as_string()
                s.sendmail(fromaddr, toaddr, text)
                print ('message sent')
            else:
                break
        while True:
            pho = input('wanna take a pic(yes or no)')
            if pho == ('yes'):
                wer = input('what do you want to call the picture')
                camera.capture(wer + ".jpg")
                s = FTP_TLS ('ftp.lafavre.us')
                s.login ('gear@lafavre.us', 'Iotisnumber1!')
                s.prot_p()
                filename = wer + '.jpg'
                ftppath = '/webchat'
                s.cwd (ftppath)
                f= open (filename, 'rb')
                s.storbinary('STOR '+ wer + '.jpg', f)
            else:
                break            
        while True:
            audio = input('do you wanna take a audio?(yes or no)')
            if (audio == 'yes'):
                timee = input('time to record')
                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=" + time + " -c1 " + name + '_' + now + ".wav"
                timee = int(timee)
                sleep( timee )
                print('done audio') 
                convert = "lame -b 16 " + name + '_' + now + ".wav " + name + '_' + now + ".mp3"
                delete = "rm " + name + '_' + now + ".wav"
                delete2 = "rm " + name + '_' + now + ".mp3"
                call([record], shell=True)
                call([convert], shell=True)
                sleep(6)
                call([delete], shell=True)      
                s = FTP_TLS ('ftp.lafavre.us')
                s.login ('gear@lafavre.us', 'Iotisnumber1!')
                s.prot_p()
                filename = '/home/pi/' + name + '_' + now + '.mp3'
                ftppath = '/webchat/'
                s.cwd (ftppath)
                f= open (filename, 'rb')
                s.storbinary('STOR '+ name + '_' + now + '.mp3', f)
                call([delete2], shell=True)
            else:
                break
        while True:
                video = input('wanna take a video(yes or no)')
                if video ==('yes'):
                    w = input('what do you want to call the recording')
                    w = str(w)
                    tim = input('time to record')
                    tim = int(tim)
                    camera.start_recording(w + '.h264')
                    sleep( tim )
                    camera.stop_recording()
                    print('video done') 
                    delete3 = w + '.h264'
                    delete4 = w + '.mp4'
                    convert = 'MP4Box -add ' + w + '.h264 ' + w + '.mp4'
                    call([convert], shell=True)
                    sleep(4.5)
                    s = FTP_TLS ('ftp.lafavre.us')
                    s.login ('gear@lafavre.us', 'Iotisnumber1!')
                    s.prot_p()
                    filename = '/home/pi/' + w + '.mp4'
                    ftppath = '/webchat/'
                    s.cwd (ftppath)
                    f= open (filename, 'rb')
                    s.storbinary('STOR ' + w + '.mp4', f)
                    call([delete3], shell=True)
                    call([delete4], shell=True)
                else:
                    break                

                      
