-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgofileFFASTRANS.py
201 lines (157 loc) · 5.85 KB
/
gofileFFASTRANS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# This code retrieves output data from FFASTRANS.
# It collects the output data and uploads the file to GOFILE.
# It sends the link to the email address specified.
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import json
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import time
from datetime import datetime
from datetime import timedelta
try:
# Create a log and add the starting time to it
now = datetime.now()
formatTimeINICIO = now.strftime('%d/%m/%Y, %H:%M:%S')
# Create a log and add the starting time to it
log = "INICIO" + formatTimeINICIO
# Get the command line arguments from FFASTRANS
nameficheroData= sys.argv[1]
workdata= sys.argv[2]
name= sys.argv[3]
duracion= sys.argv[4]
# Retrieve the server data for uploading
data = requests.get('https://apiv2.gofile.io/getServer').json()
#{'status': 'ok', 'data': {'server': 'srv-store3'}}
print(data)
# Check if the server status is ok. If not, try again.
if data['status']== "ok":
servidorfre = (data['data']['server'])
servidorup = "https://" + servidorfre + ".gofile.io/uploadFile"
# Launch the curl from Python
# curl -F [email protected] -F [email protected] https://srv-file6.gofile.io/uploadFile
# Response example
# {"status":"ok","data":{"code":"123Abc","adminCode":"3ZcBq12nTgb4cbSwJVYY","fileName":"file.txt"}}
# Add the server data to the log
log += "</br> ok: "+ json.dumps(data)
else:
log = "</br> ERROR:"+json.dumps(data)
# Create a MultipartEncoder to upload the file
mp_encoder = MultipartEncoder(
fields={
'file': (nameficheroData, open(nameficheroData, 'rb'))
}
)
# Send a POST request to upload the file
r = requests.post(servidorup,data=mp_encoder,
headers={'Content-Type': mp_encoder.content_type}
).json()
# Get the link for the uploaded file and add the response data to the log
link = "https://gofile.io/?c="+ r['data']['code']
log += "</br> respuesta Gofile: "+ json.dumps(r)
# Set the email addresses for sending the email
# me == my email address
# you == recipient's email address
me = "[email protected]"
# you = "[email protected]"
# Add the file information and link to the email body
you = ['[email protected]' ,'[email protected]']
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = f"Envio:{workdata} | {name}"
msg['From'] = me
msg['To'] = you
#Log create text
now = datetime.now()
formatTimeINICIO = now.strftime('%d/%m/%Y, %H:%M:%S')
log += "<br>nameficheroData: "+nameficheroData +"<br>"+"workdata"+workdata +"<br>"+"name"+name +"<br>"+"duracion"+duracion+"<br>"+"<br> FIN" + formatTimeINICIO
# Create the body of the message (a plain-text and an HTML version).
text = f"Link: {link}, from file: {name} from conversor {workdata}"
html = f"""\
<html>
<head>CONVERSOR</head>
<body>
<p>
File: {name}<br>
Dur: {duracion}<br>
Workflow: {workdata}<br>
Link: <a href="{link}">{name}</a>
<br>
<br>
<br>
</p>
<p>LOG <br>
<small> {log}</small></p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP('smtp.xxx.xx')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
sys.exit()
except Exception as e:
#If error send mail to admin
print (e)
# me == my email address
# you == recipient's email address
me = "[email protected]"
##Aqui definimos a quien va el error.
you = "[email protected]"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = f"ERROR!!! Conversion:{workdata} | {name}"
msg['From'] = me
msg['To'] = "[email protected]"
#Parte para el log
log ="LOG DE ERRORES:"
now = datetime.now()
log += "<br>nameficheroData:"+nameficheroData +"<br>"+"workdata"+workdata +"<br>"+"name"+name +"<br>"+"duration"+duracion+"<br>"+"<br>"
# Create the body of the message (a plain-text and an HTML version).
text = f"This is the link {link}, from file{name} in workdata: {workdata}"
html = f"""\
<html>
<head>CONVERSOR</head>
<body>
<p>
{e}
File: {name}<br>
Dur: {duracion}<br>
Workflow: {workdata}<br>
Link: <a href="{link}">{name}</a>
<br><br><br>
</p>
<p>LOG <br>
<small> {log}</small></p>
</body>
</html>
"""
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
s = smtplib.SMTP('smtp.xxx.xx')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, you, msg.as_string())
s.quit()
sys.exit()