Commit c819da92 authored by sylvie fiat's avatar sylvie fiat
Browse files

completed with sending file

parent 96b8c277
argos.py.bak
.DS_Store
.DS_Store?
._*
......
import httplib
import http.client
import re
import csv
import math
import datetime
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from subprocess import Popen, PIPE
import xml.dom.minidom
import xml.etree.ElementTree as ET
......@@ -10,12 +21,12 @@ import xml.etree.ElementTree as ET
ARGOS_HOST = "ws-argos.cls.fr"
def argosRequest(request):
conn = httplib.HTTPConnection(ARGOS_HOST)
conn = http.client.HTTPConnection(ARGOS_HOST)
conn.request("POST", "/argosDws/services/DixService", request)
response = conn.getresponse()
#print response.status, response.reason, response.msg
data = response.read()
print data
print(data)
conn.close()
return data
......@@ -200,10 +211,11 @@ def calcul_speed(latitude1,longitude1, date1, latitude2, longitude2, date2):
return distance,temps,vitesse
def convertCSV_for_DTSI():
with open('/media/sfiat/data2/workspace_python/where_argos/examples/ArgosData_2016_09_23-25.csv', 'rb') as f:
fieldnames = ['N\xc2\xb0 ID','Date de loc.','Latitude (degr\xc3\xa9 d\xc3\xa9cimal)','Longitude (degr\xc3\xa9 d\xc3\xa9cimal)','Chronologie']
writer = csv.DictWriter(open('/media/sfiat/data2/workspace_python/where_argos/examples/argos_where_"+datetime.datetime.today().strftime('%Y-%m-%d')+".csv', 'w'), fieldnames=fieldnames,delimiter=';')
def convertCSV_for_DTSI(file):
with open(file, 'r', encoding="utf8") as f:
fieldnames = ['N° ID','Date de loc.','Latitude (degré décimal)','Longitude (degré décimal)','Chronologie']
filename= "argos_where_"+datetime.datetime.today().strftime('%Y-%m-%d')+".csv"
writer = csv.DictWriter(open("./exemples/"+filename, 'w'), fieldnames=fieldnames,delimiter=';')
writer.writeheader()
reader = csv.DictReader(f,delimiter=';')
......@@ -221,16 +233,82 @@ def convertCSV_for_DTSI():
else:
distance,temps,vitesse=calcul_speed(latitude1,longitude1, date1, row[fieldnames[3]], row[fieldnames[2]], row[fieldnames[1]])
if(vitesse<12):
writer.writerow({fieldnames[0]: row[fieldnames[0]], fieldnames[1]: row[fieldnames[1]], fieldnames[2]: row[fieldnames[2]], fieldnames[3]: row[fieldnames[3]],fieldnames[7]: n})
writer.writerow({fieldnames[0]: row[fieldnames[0]], fieldnames[1]: row[fieldnames[1]], fieldnames[2]: row[fieldnames[2]], fieldnames[3]: row[fieldnames[3]],fieldnames[4]: n})
nid1=row[fieldnames[0]]
latitude1=row[fieldnames[3]]
longitude1=row[fieldnames[2]]
date1=row[fieldnames[1]]
n = n+1
return ""
return "./exemples/"+filename
def sendcsv_mail_with_google(expediteur,destinataire,file,username,password):
emailfrom = expediteur
emailto = destinataire
fileToSend = file
username = "user"
password = "password"
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "New argos data"
msg.preamble = "Please find attached new argos data for WHERE project"
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
if maintype == "text":
fp = open(fileToSend)
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "image":
fp = open(fileToSend, "rb")
attachment = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "audio":
fp = open(fileToSend, "rb")
attachment = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
fp = open(fileToSend, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
msg.attach(attachment)
server = smtplib.SMTP("smtp.gmail.com:587")
server.starttls()
server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
def sendcsv_mail_with_sendmail(expediteur,destinataire,fileToSend):
html = MIMEText("<html><head><title>New argos data</title></head><body>Please find attached new argos data for WHERE project</body>", "html")
msg = MIMEMultipart("alternative")
msg["From"] = expediteur
msg["To"] = destinataire
msg["Subject"] = "New argos data"
msg.add_header('Content-Disposition', 'attachment', filename=('utf8', '', fileToSend))
msg.attach(html)
p = Popen(["/usr/sbin/sendmail", "-t"], stdin=PIPE)
p.communicate(msg.as_bytes())
if __name__ == '__main__':
argos_csv=getCsv('GARRIGUE','BOSSE_2016',IDPROGRAM,'program',10, 20, "true", "false")
convertCSV_for_DTSI();
#argos_csv=getCsv('GARRIGUE','BOSSE_2016',IDPROGRAM,'program',10, 20, "true", "false")
argos_csv='./exemples/ArgosData_2016_09_23-25.csv'
file = convertCSV_for_DTSI(argos_csv)
#sendcsv_mail_with_google("devtest.ird@gmail.com","sylvie.fiat@ird.fr",file,"devtest.ird@gmail.com","DEV2016test")
sendcsv_mail_with_sendmail("sylvie.fiat@ird.fr","sylvie.fiat@ird.fr",file)
\ No newline at end of file
N° ID;Date de loc.;Latitude (degré décimal);Longitude (degré décimal);Chronologie
154182;2016/09/23 03:33:55;22,3780555556;169,0361111111;1
154182;2016/09/23 05:15:33;22,3725;169,0361111111;2
154182;2016/09/23 05:31:34;22,3613888889;169,0433333333;3
154182;2016/09/23 07:08:44;22,3547222222;169,0411111111;4
154182;2016/09/23 07:43:05;22,3183333333;169,0919444444;5
154182;2016/09/23 10:28:36;22,2969444444;169,1972222222;6
154182;2016/09/24 04:58:21;21,8452777778;169,5397222222;7
154182;2016/09/24 14:38:03;22,2958333333;169,0586111111;8
154182;2016/09/24 16:16:27;22,2822222222;169,0463888889;9
154182;2016/09/24 18:34:33;22,2747222222;169,0438888889;10
154182;2016/09/24 18:59:10;22,2763888889;169,0394444444;11
154182;2016/09/25 06:08:38;22,3816666667;169,0680555556;12
154182;2016/09/25 06:58:57;22,3891666667;169,0758333333;13
154182;2016/09/25 10:36:40;22,3941666667;169,0702777778;14
154182;2016/09/25 14:26:13;22,3713888889;168,9777777778;15
154182;2016/09/25 16:03:52;22,3477777778;168,9747222222;16
154182;2016/09/25 18:32:40;22,34;168,9661111111;17
154183;2016/09/24 03:22:54;22,3691666667;169,0225;1
154183;2016/09/24 05:05:30;22,3738888889;169,0213888889;2
154183;2016/09/24 05:46:39;22,3855555556;168,9836111111;3
154183;2016/09/24 06:36:11;22,4044444444;169,0233333333;4
154183;2016/09/24 14:39:26;22,4580555556;169,0930555556;5
154183;2016/09/24 16:14:21;22,4116666667;169,0688888889;6
154183;2016/09/24 18:34:59;22,4455555556;169,025;7
154183;2016/09/24 22:09:23;22,4961111111;168,9705555556;8
154183;2016/09/24 22:59:02;22,4969444444;168,9602777778;9
154183;2016/09/25 04:51:16;22,5661111111;168,9125;10
154183;2016/09/25 06:04:41;22,5933333333;168,9127777778;11
154183;2016/09/25 06:58:22;22,5933333333;168,8972222222;12
154183;2016/09/25 10:39:57;22,6113888889;168,8580555556;13
154183;2016/09/25 16:10:19;22,5433333333;168,9780555556;14
154183;2016/09/25 18:11:09;22,5377777778;168,9469444444;15
154183;2016/09/25 18:28:26;22,5461111111;168,95;16
154183;2016/09/25 18:36:27;22,5466666667;168,9419444444;17
154183;2016/09/25 20:06:30;22,6061111111;168,9330555556;18
154184;2016/09/25 18:08:37;23,5941666667;170,0480555556;1
154187;2016/09/24 18:58:25;22,2866666667;168,8002777778;1
154187;2016/09/24 22:11:07;22,1858333333;168,8605555556;2
154187;2016/09/24 22:54:57;22,1811111111;168,8516666667;3
154187;2016/09/25 04:49:58;21,9502777778;168,9297222222;4
154187;2016/09/25 14:27:20;21,9405555556;168,4736111111;5
154187;2016/09/25 18:08:07;22,1283333333;168,5008333333;6
154187;2016/09/25 18:36:47;22,1252777778;168,4811111111;7
154187;2016/09/25 18:29:18;22,0894444444;168,5344444444;8
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment