-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSIBOM.py
350 lines (288 loc) · 11.6 KB
/
SIBOM.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import requests
import sys
import re
import os
import textwrap
import string
from random import choices
from HTMLtoImg import TableToIMG
from bs4 import BeautifulSoup
art_regex = re.compile(r"^\s*?art.culo\s*?(\d+).*?(?=\w)", flags=re.I)
spaces_regex = re.compile(r" {2,}")
class Tweet:
"""Una clase para hacer más legible el código"""
def __init__(self, content="", media_filenames=[]) -> None:
"""
Parámetros
----------
content : str
El texto del tweet
media_filenames : list
Una lista conteniendo los paths hacia las imágenes a incluir
en el tweet
"""
self.content = content
self.media_filenames = media_filenames
class Publicacion:
def __init__(self) -> None:
self.articulos = []
self.boid = 0
self.ciudad_fecha = ""
self.imagenes = []
self.titulo = ""
self.url = ""
self.tablas = []
self.cuits = []
self.anexos = []
def GetTweets(self) -> list:
"""Devuelvo una lista de objetos Tweet basados en el contenido
de la publicación
"""
if not os.path.exists("temp"):
os.mkdir("temp")
fill = "..."
max_chars = 280 - len(fill)
tweets = []
first_tweet = self.ciudad_fecha + "\n" + self.titulo + \
"\nFuente: %s" % (self.url) + \
("", " (ver anexos)")[len(self.anexos) > 0]
first_tweet += "\nRecordá que esta cuenta no está afiliada al Municipio!"
tweets.append(Tweet(first_tweet))
for text in self.articulos:
text = self._FormatText(text)
text_sub = textwrap.wrap(text, max_chars)
n = len(text_sub)
for i in range(0, n):
if i != n - 1:
text_sub[i] += fill
tweets.append(Tweet(text_sub[i], []))
media_filenames = []
for i in range(0, len(self.imagenes)):
filename = "temp/%s.png" % (self._GetRandomString())
with open(filename, "wb") as fp:
fp.write(self.imagenes[i])
# Separo cada 4 imágenes, que es el máximo que se puede
# subir por cada Tweet.
media_filenames.append(filename)
if (i+1) % 4 == 0:
tweets.append(Tweet(media_filenames=media_filenames))
media_filenames = []
if len(media_filenames) != 0 and len(media_filenames) % 4 != 0:
# El último Tweet tiene menos de 4 imágenes, pero no está
# vacío.
tweets.append(Tweet(media_filenames=media_filenames))
# os.rmdir("temp")
return tweets
def _FormatText(self, text: str) -> str:
"""Quito espacios innecesarios y hago un formateo básico del
texto
"""
text = text.replace(u"\xa0", " ")
# La diferencia es extremadamente sutil, pero rompía art_regex
text = text.replace(u"º", u"°")
text = spaces_regex.sub(" ", text)
# "Artículo 3˚.- Se dispone..." -> "3: Se dispone..."
text = art_regex.sub(r"[\1]: ", text)
return text
def _GetRandomString(self) -> str:
"""Devuelvo una secuencia aleatoria de 10 caracteres para
utilizar en los nombres de archivo.
"""
return "".join(choices(string.ascii_letters + string.digits, k=10))
class SIBOM:
sibom_url = "https://sibom.slyt.gba.gov.ar/bulletins/"
img_gen = TableToIMG()
muni_display = ""
muni = ""
tw_handle = ""
muni_regex = None
cuit_regex = re.compile(
r"([23]\d *?- *?\d{7,8} *?- *?\d)", flags=re.I | re.M | re.S)
def __init__(self, tw_handle: str, muni_display: str, muni_regex: str, font_name: str, logo: str) -> None:
"""
Parámetros
----------
tw_handle : str
El nombre de usuario de Twitter que se utilizará. Lo uso
para los pies de las imágenes que genero a partir de tablas.
muni_display : str
El nombre del municipio. También lo uso en las imágenes.
muni_regex : str
Una expresión regular para matchear el municipio en SIBOM.
Hago de esta forma para contemplar casos en que el municipio
contenga (o no) acentos o mayúsculas.
font_name : str
Path a una fuente TrueType a utilizar para las imágenes.
logo : str
Path al logo del municipio a utilizar (PNG, 100x100).
"""
self.tw_handle = tw_handle
self.muni_display = muni_display
self.muni_regex = re.compile(muni_regex, re.IGNORECASE)
self.img_gen.footer_line_1 = "Twitter: %s (cuenta no afiliada al municipio)" % (
self.tw_handle)
self.img_gen.footer_line_2 = "Municipalidad de %s" % (
self.muni_display)
self.img_gen.font_name = font_name
self.img_gen.logo = logo
return
def _GetURL(self, url: str, **kwargs) -> BeautifulSoup:
"""Hago el request y devuelvo el objeto parseado por BS
Parámetros
----------
url : str
URL a la cual acceder
**kwargs
Cualquier otro parámetro que se desee pasar a requests.get
"""
# TODO: Reintentar un par de veces si falla
parsed = None
resp = requests.get(url, kwargs)
if resp.status_code != 200:
print("ERROR: No pude acceder a %s" % (url))
else:
parsed = BeautifulSoup(resp.text, features="html.parser")
return parsed
def GetLatestID(self) -> int:
"""Devuelvo el ID del último boletín oficial dado un municipio.
Si no lo encuentro en las primeras cinco páginas, o no puedo
acceder por algún motivo, devuelvo 0.
"""
id = 0
for i in range(1, 6):
url = self.sibom_url + ("", "?page=%s" % (i))[i > 1]
parsed = self._GetURL(url)
if parsed:
divs = parsed.find_all(class_="row bulletin-index")
if divs is not None:
for div in divs:
if self.muni_regex.search(div.text):
# Encontré el div, obtengo el id
id = div.find("form").attrs["action"]
# "/bulletins/(id)" --> (id)
id = int(id.split("/")[2])
break
if id != 0:
break
return id
def GetAllURLs(self, id: int) -> list:
"""Devuelvo las URL de decretos, resoluciones, etc. de un BO
Parámetros
----------
id : int
El ID del boletín oficial al que se desea acceder.
"""
url = self.sibom_url + "%s?" % (id)
urls = []
parsed = self._GetURL(url)
if parsed:
objs = parsed.find_all("a", class_="content-link")
for obj in objs:
# "/bulletins/4047/contents/1477570" --> "1477570"
bulletin_id = obj.attrs["href"].split("contents/")[1]
url = self.sibom_url + "%s/contents/%s" % (id, bulletin_id)
urls.append(url)
return urls
def ParsePublicacion(self, url: str) -> Publicacion:
"""Accedo a una URL y devuelvo los datos parseados
Parámetros
----------
url : str
URL del decreto o resolución requerido.
"""
pub = Publicacion()
pub.tablas = []
parsed = self._GetURL(url)
if parsed:
pub.titulo = parsed.find(class_="title").text
pub.url = url
pub.ciudad_fecha = parsed.find(class_="city-and-date").text
contenido = parsed.find(class_="col-md-9")
pub.cuits = self.cuit_regex.findall(contenido.text)
# pub.tablas = contenido.find_all("table")
pub.tablas = contenido.find_all(self._MatchTables)
pub.anexos = []
for anexo in parsed.find_all(class_="annex-name"):
pub.anexos.append(anexo.text)
for art in contenido.find_all(self._MatchParagraphs, recursive=False):
pub.articulos.append(art.text)
self.img_gen.caption = pub.titulo
self.img_gen.footer_line_3 = "Datos extraídos de SIBOM. Fuente: %s" % (
url)
for tabla in pub.tablas:
pub.imagenes.append(
self.img_gen.GetImage(str(tabla), 1920, 1080))
return pub
def _MatchParagraphs(self, tag: BeautifulSoup) -> bool:
"""Devuelvo True si un tag HTML corresponde a un artículo de una
resolución o decreto. Utilizado con BeautifulSoup.
Hago esto porque acostumbran no poner un atributo _class_, ni
hacer ningún tipo de distinción en el código.
Parámetros
----------
tag : BeautifulSoup
Objeto BeautifulSoup a evaluar.
"""
matches = False
if tag.name != "table":
if art_regex.match(tag.text):
# Comienza con "artículo"
matches = True
return matches
def _MatchTables(self, tag: BeautifulSoup) -> bool:
"""Devuelvo True si un tag HTML corresponde a una tabla.
Tengo que hacer esto porque acostumbran poner tablas, dentro de
tablas, dentro de tablas... Con este método determino si se
trata de la última capa de la cebolla.
De no hacerlo de esta manera, en algunos casos TableToIMG
tardaba muchísimo en procesar todo, y devolvía basura.
Parámetros
----------
tag : BeautifulSoup
Objeto BeautifulSoup a evaluar.
"""
matches = False
if tag.name == "table":
if not tag.find("table"):
# Por si la tabla está vacía...
if not len(tag.text.strip("\n\xa0 ")) == 0:
matches = True
return matches
if __name__ == "__main__":
s = SIBOM("@BoletinMGP", "General Pueyrredón",
r"general pueyrred.n", "assets/Montserrat-Regular.ttf", "assets/logo.png")
id = s.GetLatestID()
if id != 0:
print("Procesando %s..." % id)
urls = s.GetAllURLs(id)
if not os.path.exists(str(id)):
os.mkdir(str(id))
for url in urls:
print("\t" + url)
pub = s.ParsePublicacion(url)
tweets = pub.GetTweets()
filename = pub.titulo.replace("/", "")
with open("%s/%s.txt" % (id, filename), "wt") as fp:
for tw in tweets:
if len(tw.content) > 0:
fp.write(tw.content + "\n" + "-" * 20 + "\n")
if len(tw.media_filenames) > 0:
fp.write(str(tw.media_filenames) +
"\n" + "-" * 20 + "\n")
# for id in [3987]: # [3970,3987,4009,4012,4016,4047]:
# print("Procesando %s..." % id)
# urls = s.GetAllURLs(id)
# if not os.path.exists(str(id)):
# os.mkdir(str(id))
# for url in urls:
# print("\t" + url)
# pub = s.ParsePublicacion(url)
# tweets = pub.GetTweets()
# filename = pub.titulo.replace("/", "")
# with open("%s/%s.txt" % (id, filename), "wt") as fp:
# for tw in tweets:
# if len(tw.content) > 0:
# fp.write(tw.content + "\n" + "-" * 20 + "\n")
# if len(tw.media_filenames) > 0:
# fp.write(str(tw.media_filenames) +
# "\n" + "-" * 20 + "\n")