forked from institutoazmina/elasnocongressobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
89 lines (73 loc) · 2.92 KB
/
utils.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
from PIL import Image
import imgkit
import os
import locale
locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8')
async def text_to_image(texto, descricao):
titulo = texto.split('\n')[0]
url = texto.split('\n')[-1].replace('🔗 ', '')
new_texto = [t.split(': ') for t in texto.split('\n')[1:-1]]
valores = {}
valores['titulo'] = titulo
for tipo, texto in new_texto:
if tipo == '🕙 Última atualização':
valores['data'] = texto
elif tipo == '📕 Nome':
valores['nome'] = texto
elif tipo == '💡 Tema':
valores['tema'] = texto
elif tipo == '🔈 Tramitação':
valores['tramitacao'] = texto
elif tipo == '↪️ Situação':
valores['situacao'] = texto
valores['url'] = url
insert_html = ""
for k, v in valores.items():
if k == 'titulo':
new_titulo = v.split(': ')
insert_html += f" <b> {new_titulo[0]}: <span class='valor'>{new_titulo[1]}</span> </b></br>"
elif k == 'data':
insert_html += f" <small>ATUALIZAÇÃO: {v}</small> "
elif k == 'nome':
if len(v) > 130:
v = v[:127] + '...'
insert_html += f"</br>AUTOR/RELATOR: <span class='nome'>{v}</span>"
elif k == 'tema':
insert_html += f"</br>TEMA:<span class='tema'>{v}</span>"
elif k == 'tramitacao':
insert_html += f"</br>TRAMITAÇÃO: <span class='redeytgoogle'>{v}</span>"
elif k == 'situacao':
insert_html += f"</br>SITUAÇÃO: <span class='redeytgoogle'>{v}</span>"
elif k == 'url':
new_link = v.replace('https://', '')
insert_html += f"</br> Link: <b>{new_link}</b>"
if len(descricao) < 130:
descricao = f'<div class="titulo"> {descricao} </br><small>*Texto resumido por um modelo de aprendizado de máquina</small></div>'
else:
descricao = f'<div class="titulo"> {descricao[:127]}... </br><small>*Texto resumido por um modelo de aprendizado de máquina</small></div>'
html = """
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/styles.css">
<script src="js/twemoji.min.js"></script>
<script>window.onload = function () {}</script>
</head>
<body>
<p>
{}
{}
<img src='landing-nucleo_logo-header.png'><br>
<url>nucleo.jor.br/legislaredes</url>
</body>
</html>
""".format("{twemoji.parse(document.body);}", insert_html, descricao)
with open('html/tmp.html', 'w') as f:
f.write(html)
kitoptions = { "enable-local-file-access": None , "encoding": 'UTF-8'}
imgkit.from_url("file://" + os.getcwd() + "/html/tmp.html", output_path='html/out.png', options=kitoptions)
img = Image.open('html/out.png')
height = img.size[1]
img2 = img.crop((0, 0, 600, height))
img2.save('html/out.png')