Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
139 lines (122 loc) · 4.72 KB
/
Copy pathmain.py
File metadata and controls
139 lines (122 loc) · 4.72 KB
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
from os import getenv
import sys
import time
from pypodio2 import api
from pypodio2.transport import TransportException
from get_time import get_hour
from podio_create_tables import create_tables
from podio_insert_items import insert_items
from podio_tools import handling_podio_error
from logging_tools import logger
if __name__ == '__main__':
# Database update period in seconds
timeOffset = int(getenv('TIMEOFFSET'))
# Podio credentials
client_id = getenv('PODIO_CLIENT_ID')
client_secret = getenv('PODIO_CLIENT_SECRET')
username = getenv('PODIO_USERNAME')
password = getenv('PODIO_PASSWORD')
# Apps IDs
apps_ids = list(map(int, getenv('PODIO_APPS_IDS').split(',')))
MESSAGE = "==== PODIO API PYTHON SCRIPT (PostgreSQL) ===="
logger.debug(MESSAGE)
# Podio authentication
try:
podio = api.OAuthClient(
client_id,
client_secret,
username,
password
)
# Caso haja erro, provavelmente o token de acesso a API expirou.
except TransportException as err:
handled = handling_podio_error(err)
if handled == 'status_400':
logger.info("Terminando o programa.")
exit(1)
else:
CYCLE = 1
while True:
MESSAGE = f"==== Ciclo {CYCLE} ===="
logger.info(MESSAGE)
# send_to_bot(MESSAGE)
CREATION = create_tables(podio, apps_ids)
if CREATION == 0:
INSERTION = insert_items(podio, apps_ids)
if INSERTION == 1:
hour = get_hour(hours=1)
MESSAGE = f"Esperando a hora seguinte. Até às {hour}"
logger.info(MESSAGE)
time.sleep(3600)
try:
podio = api.OAuthClient(
client_id,
client_secret,
username,
password
)
except:
MESSAGE = 'Erro na obtenção do novo cliente Podio! Tentando novamente...'
logger.warning(MESSAGE)
elif INSERTION == 0:
hours = get_hour(seconds=timeOffset)
MESSAGE = f"Esperando as próximas {timeOffset/3600}hs. Até às {hours}"
logger.info(MESSAGE)
time.sleep(timeOffset)
try:
podio = api.OAuthClient(
client_id,
client_secret,
username,
password
)
except:
MESSAGE = 'Erro na obtenção do novo cliente Podio! Tentando novamente...'
logger.warning(MESSAGE)
else:
MESSAGE = "Tentando novamente..."
logger.info(MESSAGE)
try:
podio = api.OAuthClient(
client_id,
client_secret,
username,
password
)
except:
MESSAGE = 'Erro na obtenção do novo cliente Podio! Tentando novamente...'
logger.warning(MESSAGE)
# time.sleep(1)
elif CREATION == 1:
hour = get_hour(hours=1)
MESSAGE = f"Esperando a hora seguinte às {hour}"
logger.info(MESSAGE)
time.sleep(3600)
try:
podio = api.OAuthClient(
client_id,
client_secret,
username,
password
)
except:
MESSAGE = 'Erro na obtenção do novo cliente Podio! Tentando novamente...'
logger.warning(MESSAGE)
elif CREATION == 2:
MESSAGE = "Tentando novamente..."
logger.info(MESSAGE)
try:
podio = api.OAuthClient(
client_id,
client_secret,
username,
password
)
except:
MESSAGE = 'Erro na obtenção do novo cliente Podio! Tentando novamente...'
logger.warning(MESSAGE)
# time.sleep(1)
else:
MESSAGE = "Erro inesperado na criação/atualização do BD. Terminando o programa."
logger.error(MESSAGE)
exit(code=1)