-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorreos.py
87 lines (64 loc) · 2.17 KB
/
correos.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
from typing import Mapping, Dict, List
from bs4 import BeautifulSoup
from requests import Session
URL = 'https://www.correosdemexico.gob.mx/SSLServicios/SeguimientoEnvio/Seguimiento.aspx'
def get_validation_info(s: Session) -> Dict[str, str]:
r = s.get(URL)
soup = BeautifulSoup(r.content, 'lxml')
return {
tag['name']: tag['value']
for tag in soup.select('input[name^=__]')
}
def get_tracking_data(
s: Session,
validation: Mapping[str, str],
tracking_id: str,
period: str,
) -> List[Dict[str, str]]:
request_data = {
**validation,
'__EVENTTARGET': 'Busqueda',
'Guia': tracking_id,
'Periodo': period,
}
r = s.post(URL, data=request_data)
soup = BeautifulSoup(r.content, 'lxml')
data = []
# Header
header = [th.text for th in soup.select('#GDDatos th')]
# Rows
for tr in soup.select('#GDDatos tr'):
rows = tr.select('td')
if rows:
data.append(dict(zip(header, [td.text for td in rows])))
return data
if __name__ == '__main__':
import json
import os
import requests
from jinja2 import Environment, FileSystemLoader
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from templates import render_tracking
SENDGRID_API_KEY = os.getenv('SENDGRID_API_KEY')
GUIA = os.getenv('GUIA')
PERIODO = os.getenv('PERIODO', 2020)
env = Environment(loader=FileSystemLoader('./templates'))
sg = SendGridAPIClient(SENDGRID_API_KEY)
with requests.session() as s:
s.headers['user-agent'] = 'Mozilla/5.0'
validation = get_validation_info(s)
data = get_tracking_data(s, validation, GUIA, PERIODO)
message = Mail(
from_email=os.getenv('EMAIL_FROM'),
to_emails=os.getenv('EMAIL_TO'),
subject='Seguimiento de envío',
html_content=render_tracking(data, env),
)
response = sg.send(message)
status_code = response.status_code
body = response.body.decode()
headers = dict(response.headers)
print(status_code, type(status_code))
print(body, type(body))
print(headers, type(headers))