This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (69 loc) · 2.27 KB
/
main.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
import os
import sys
import time
from pathlib import Path
from random import choice
from codecs import decode
from base64 import b64decode
import tweepy
max_errors_count = 5
CONSUMER_KEY = os.environ["CONSUMER_KEY"]
CONSUMER_SECRET = os.environ["CONSUMER_SECRET"]
ACCESS_TOKEN = os.environ["ACCESS_TOKEN"]
ACCESS_TOKEN_SECRET = os.environ["ACCESS_TOKEN_SECRET"]
LANG = os.environ["LANG"]
if LANG == "es":
p_data = Path("crypt/")
file_sufix = ".png"
elif LANG == "pt":
p_data = Path("crypt_pt")
file_sufix = ".jpeg"
def remove_zero_from_start(s):
while s.startswith("0"):
s = s[1:]
return s
if __name__ == "__main__":
print("Starting ...")
valid_comics = list(p.name for p in p_data.iterdir())
comic = choice(valid_comics)
filename = comic + file_sufix
super_secret = (p_data / comic).read_text()
im_b = b64decode(decode(super_secret, "rot-13"))
p_im = Path(filename)
p_im.write_bytes(im_b)
if LANG == "es":
_, volumen, pagina, parte = comic.split("-")
if parte == "a":
parte = "1ra"
elif parte == "b":
parte = "2da"
status = f"Vol. {remove_zero_from_start(volumen)}, pag. {remove_zero_from_start(pagina)}, {parte} parte. #Mafalda #Quino"
elif LANG == "pt":
_, _, pagina, parte = comic.split("_")
if parte == "a":
parte = "um"
elif parte == "b":
parte = "dois"
elif parte == "c":
parte = "três"
elif parte == "d":
parte = "quatro"
status = f"Toda Mafalda, página {pagina}, parte {parte}. #Mafalda #Quino"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
media = api.media_upload(filename)
errors_count = 0
while True:
try:
tweet = api.update_status(status=status, media_ids=[media.media_id])
except Exception as e:
print(f"Error found: {e}")
errors_count += 1
if errors_count == max_errors_count:
print("Max number of errors reached with twitter API!")
sys.exit(1)
time.sleep(errors_count + 1)
else:
break
print("Tweet done!")