-
Notifications
You must be signed in to change notification settings - Fork 0
/
pautabot.py
415 lines (330 loc) · 11.8 KB
/
pautabot.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import dataclasses
from datetime import datetime
import logging
import os
import pickle
import tempfile
from typing import Dict, List, Optional, Set, Tuple
import sys
import click
import dotenv
from dataclasses_json import dataclass_json
import requests
import shutil
import tweepy
STATE_FILE = "pautabot.state"
BIGNUM = 9e15
CURRENT_YEAR = datetime.now().year
KEYWORD_TO_CHECK = "publicidad"
AD_PURCHASES_URL = f"https://gobiernoabierto.bahia.gob.ar/WS/2328/{CURRENT_YEAR}"
ALL_PURCHASES_URL = f"https://gobiernoabierto.bahia.gob.ar/WS/2307/{CURRENT_YEAR}"
DETAIL_PURCHASE_URL_TEMPLATE = (
"https://gobiernoabierto.bahia.gob.ar/WS/2312/{year}/{ordencompra}"
)
DETAIL_PURCHASE_PAGE_URL_TEMPLATE = (
"https://www.bahia.gob.ar/compras/data/oc/{year}/{ordencompra}"
)
TWEET_TEMPLATE = """
💸 Nuevo gasto de pauta oficial:
📰 Proveedor: {proveedor}
🏛 Dependencia: {dependencia}
🗓 Fecha: {fecha}
💵 Importe: $ {importe}
{url}
"""
BOLD_TRANS = str.maketrans(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?.,\"'",
"𝐚𝐛𝐜𝐝𝐞𝐟𝐠𝐡𝐢𝐣𝐤𝐥𝐦𝐧𝐨𝐩𝐪𝐫𝐬𝐭𝐮𝐯𝐰𝐱𝐲𝐳𝐀𝐁𝐂𝐃𝐄𝐅𝐆𝐇𝐈𝐉𝐊𝐋𝐌𝐍𝐎𝐏𝐐𝐑𝐒𝐓𝐔𝐕𝐖𝐗𝐘𝐙𝟎𝟏𝟐𝟑𝟒𝟓𝟔𝟕𝟖𝟗❗❓.,\"'",
)
DIGIT_TRANS = str.maketrans("1234567890", "𝟭𝟮𝟯𝟰𝟱𝟲𝟳𝟴𝟵𝟬")
def boldify(s: str) -> str:
"""
>>> boldify("Gran inversion, 65 palos para adornar periodistas")
'𝐆𝐫𝐚𝐧 𝐢𝐧𝐯𝐞𝐫𝐬𝐢𝐨𝐧, 𝟔𝟓 𝐩𝐚𝐥𝐨𝐬 𝐩𝐚𝐫𝐚 𝐚𝐝𝐨𝐫𝐧𝐚𝐫 𝐩𝐞𝐫𝐢𝐨𝐝𝐢𝐬𝐭𝐚𝐬'
"""
return s.translate(BOLD_TRANS)
def monodigits(s: str) -> str:
return s.translate(DIGIT_TRANS)
@dataclass_json
@dataclasses.dataclass
class Purchase:
ejercicio: int
ordencompra: int
fecha: str
importe: float
proveedor: str
dependencia: str
expediente: str
@dataclasses.dataclass
class ProcessedPurchase(Purchase):
processed_at: datetime
status: str # one of: "processed", "dropped"
tweet_id: Optional[str]
@dataclasses.dataclass
class RunState:
last_run: datetime
totals_by_seller: Dict[str, float]
all_purchases: List[Purchase]
processed_purchases: List[ProcessedPurchase]
logger = logging.getLogger("pautabot")
log_handler = logging.StreamHandler(sys.stdout)
log_handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
)
if logger.hasHandlers():
logger.handlers.clear()
logger.addHandler(log_handler)
logger.setLevel(logging.DEBUG)
def init_twitter_client() -> tweepy.API:
auth = tweepy.OAuthHandler(
os.environ["TWITTER_API_KEY"], os.environ["TWITTER_SECRET_KEY"]
)
auth.set_access_token(
os.environ["TWITTER_ACCESS_TOKEN"], os.environ["TWITTER_TOKEN_SECRET"]
)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
api.verify_credentials()
return api
def get_microlink_screenshot(url: str) -> str:
resp = requests.get(
"https://api.microlink.io/",
params={
"url": url,
"screenshot": "",
"element": "table",
"viewport.width": 1024,
"styles": "table { margin: 10px !important }"
},
)
j = resp.json()
if j.get("status") == "fail" and j.get("code") == "ECNRCY":
# concurrency error, try again
# TODO: set max number of retries
logger.info("Microlink throttled. Retrying in 2 secs...")
time.sleep(2)
return get_microlink_screenshot(url)
else:
url = j["data"]["screenshot"]["url"]
resp = requests.get(url, stream=True)
fp, fname = tempfile.mkstemp(".png")
with open(fname, "wb") as f:
resp.raw.decode_content = True
shutil.copyfileobj(resp.raw, f)
return fname
def purchase_detail_url(purchase: Purchase) -> str:
return DETAIL_PURCHASE_URL_TEMPLATE.format(
year=purchase.ejercicio, ordencompra=purchase.ordencompra
)
def get_advertisement_totals_by_seller() -> Dict[str, float]:
resp = requests.get(AD_PURCHASES_URL)
logger.info("Got totals by seller: %s", resp.text)
json_resp = resp.json()
return {row["proveedor"]: float(row["monto"]) for row in json_resp}
def get_all_purchases() -> List[Purchase]:
resp = requests.get(ALL_PURCHASES_URL)
all_purchases = Purchase.schema().load(resp.json(), many=True)
return sorted(
all_purchases,
key=lambda p: datetime.strptime(p.fecha, "%d-%m-%Y"),
reverse=True,
)
def diff_totals(old: Dict[str, float], new: Dict[str, float]) -> List[str]:
"""
>>> diff_totals({"foo": 10, "bar": 20}, {"foo": 20, "bar": 20})
['foo']
>>> diff_totals({"quux": 10}, {"foo": 20, "bar": 20})
['foo', 'bar']
>>> diff_totals({"foo": 10, "bar": 10}, {"foo": 20, "bar": 20})
['foo', 'bar']
>>> diff_totals({"foo": 10, "bar": 20}, {"quux": 20})
['quux']
"""
sellers_to_process = [
new_seller
for new_seller, new_amount in new.items()
if old.get(new_seller, BIGNUM) < new_amount or new_seller not in old
]
return sellers_to_process
def get_purchase_detail(url: str) -> List[dict]:
resp = requests.get(url)
return resp.json()
def get_unprocessed_purchases_for_seller(
all_purchases: List, processed_purchases: List[ProcessedPurchase], seller: str
) -> List[Purchase]:
processed_purchases_set: Set[Tuple[int, int]] = set()
for pp in processed_purchases:
processed_purchases_set.add((pp.ejercicio, pp.ordencompra))
unprocessed_purchases = [
purchase
for purchase in all_purchases
if purchase.proveedor == seller
and (purchase.ejercicio, purchase.ordencompra) not in processed_purchases_set
]
return unprocessed_purchases
def tweet_body_for_purchase(purchase: Purchase) -> str:
tweet_body = TWEET_TEMPLATE.format(
fecha=purchase.fecha,
proveedor=purchase.proveedor,
dependencia=purchase.dependencia,
importe="{:,}".format(int(purchase.importe)).replace(",", "."),
url=DETAIL_PURCHASE_PAGE_URL_TEMPLATE.format(
year=purchase.ejercicio, ordencompra=purchase.ordencompra
),
)
return tweet_body
def tweet_new_purchase(
purchase: Purchase, twitter_client: tweepy.API, with_image: bool = True
) -> tweepy.Status:
status: tweepy.Status
tweet_body = tweet_body_for_purchase(purchase)
if with_image:
screenshot_path = get_microlink_screenshot(
DETAIL_PURCHASE_PAGE_URL_TEMPLATE.format(
year=purchase.ejercicio, ordencompra=purchase.ordencompra
)
)
status = twitter_client.update_with_media(screenshot_path, status=tweet_body)
else:
status = twitter_client.update_status(tweet_body)
return status
def load_state() -> RunState:
with open(STATE_FILE, "rb") as sf:
state: RunState = pickle.load(sf)
return state
def save_state(state: RunState):
logger.info("Saving new state")
with open(STATE_FILE, "wb") as sf:
pickle.dump(state, sf)
def main():
logger.info(f"Running {__name__}")
state = load_state()
logger.info(
(
"Read state. last_run: %s - "
"len(totals_by_seller): %d - len(all_purchases): %d - "
"len(processed_purchases): %d"
),
state.last_run,
len(state.totals_by_seller),
len(state.all_purchases),
len(state.processed_purchases),
)
prev_run = state.last_run
state.last_run = datetime.now()
logger.info("Getting all purchases")
state.all_purchases = get_all_purchases()
logger.info("Getting ad purchases")
ad_purchases_totals = get_advertisement_totals_by_seller()
# Get sellers to find new purchases
sellers_to_process = diff_totals(state.totals_by_seller, ad_purchases_totals)
state.totals_by_seller = ad_purchases_totals
if len(sellers_to_process) == 0:
logger.info("No changes since %s - Bye.", prev_run)
save_state(state)
sys.exit(0)
twitter_client = init_twitter_client()
tweet_queue: List[Purchase] = []
for seller in sellers_to_process:
purchases_to_process = get_unprocessed_purchases_for_seller(
state.all_purchases, state.processed_purchases, seller
)
for p in purchases_to_process:
logger.info("Processing purchase: %s", p)
logger.info("Getting detail for %s/%s", p.ejercicio, p.ordencompra)
try:
purchase_detail = get_purchase_detail(purchase_detail_url(p))
except Exception as e:
logger.warn("Error when getting detail of %s. Exception: %s", p, e)
state.processed_purchases.append(
ProcessedPurchase(
**p.to_dict(),
processed_at=datetime.now(),
status="error",
tweet_id=None,
)
)
continue
if not any(
map(
lambda line: KEYWORD_TO_CHECK in line["detalle"].lower(),
purchase_detail,
)
):
logger.info("Dropping %s - keyword not found")
state.processed_purchases.append(
ProcessedPurchase(
**p.to_dict(),
processed_at=datetime.now(),
status="dropped",
tweet_id=None,
)
)
continue
logger.info("Will tweet: %s", p)
tweet_queue.append(p)
for i, p in enumerate(
sorted(tweet_queue, key=lambda p: datetime.strptime(p.fecha, "%d-%m-%Y"))
):
tweet_id: str = ""
status: str = ""
logger.info("Tweeting: %s", p)
try:
status = tweet_new_purchase(p, twitter_client)
tweet_id = status.id
status = "processed"
except Exception as e:
logger.error("Could not tweet: %s", e)
tweet_id = ""
status = "error"
state.processed_purchases.append(
ProcessedPurchase(
**p.to_dict(),
processed_at=datetime.now(),
status=status,
tweet_id=tweet_id,
)
)
save_state(state)
@click.group()
def commands():
...
@commands.command()
def run_bot():
main()
@commands.command()
@click.argument("po_number", type=int)
@click.argument("po_year", type=int)
def tweet_purchase(po_number: int, po_year: int):
all_purchases = get_all_purchases()
ps = [
p
for p in all_purchases
if p.ordencompra == po_number and p.ejercicio == po_year
]
if len(ps) != 1:
click.echo(f"PO {po_number}/{po_year} not found", err=True)
sys.exit(1)
purchase = ps[0]
tweet_body = tweet_body_for_purchase(purchase)
api = init_twitter_client()
screenshot_path = get_microlink_screenshot(
DETAIL_PURCHASE_PAGE_URL_TEMPLATE.format(
year=purchase.ejercicio, ordencompra=purchase.ordencompra
)
)
api.update_with_media(screenshot_path, status=tweet_body)
@commands.command()
def check_if_new():
state = load_state()
ad_purchases_totals = get_advertisement_totals_by_seller()
sellers_to_process = diff_totals(state.totals_by_seller, ad_purchases_totals)
if len(sellers_to_process) > 0:
click.echo(f"New POs for: {sellers_to_process}")
else:
click.echo("No new POs")
commands.add_command(run_bot)
commands.add_command(tweet_purchase)
commands.add_command(check_if_new)
if __name__ == "__main__":
dotenv.load_dotenv()
commands()