-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.py
244 lines (206 loc) · 12.4 KB
/
commands.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
import datetime
import json
import requests
import urllib.parse
import urllib.request
import socket
# APIs do AccuWeather, tem essas 3 pelo fato do limite de requisições p/ dia ser só 50 em cada uma
#API = "5EHm0OouVq2MYsFa91FGKPadu6raFZEF" #API 1
#API = "nnvBpHyP6BUcWPiePvvGcPdtTkUOhrz8" #API 2
API = "vBkVX41ZeQvmP2li5Z0AjQRSHfiVfvdz" #API 3
# APIs do Yandex tradutor
API_T = 'trnsl.1.1.20190927T202249Z.ae7d6ae63ea79bdc.69b6616a32a45b1f3412c33ac2de768277b6515c'
urlt = 'https://translate.yandex.net/api/v1.5/tr.json/translate'
def devs():
"""
Funcao que retorna os nomes dos desenvolvedores
:return answer: uma string com os nomes
"""
answer = "[DEVS] Developers: \n1 - Gustavo Rodrigues \n2 - Igor Capeletti"
return answer
def dataHora():
"""
funcao que solicita o horarioo local da maquina
:return answer: uma string com o horario atual
"""
time = datetime.datetime.now()
answer = "[TIME] Data e hora atual: " + str(time)
return answer
def help(argument):
"""
funcao que retorna a lista de comandos disponiveis
:param argument: string que representa o comando que se deseja mais informacoes
:return: string com os comandos disponiveis ou mais informacoes sobre um comando
"""
if argument != "":
argument = argument.upper()
if argument == "DEVS":
answer = "[HELP] O comando \devs contem informacoes sobre os desenvolvedores do MAX."
elif argument == "DATAHORA":
answer = "[HELP] O comando \datahora informa a data e hora atual."
elif argument == "WEATHER":
answer = "[HELP] O comando \weather espera uma cidade como parametro e retorna a temperatura atual desta localizacao."
elif argument == "WEATHERWEEK":
answer = "[HELP] O comando \weatherweek espera uma cidade como parametro e retorna a respectiva previsao do tempo num periodo de 5 dias."
elif argument == "HELP":
answer = "[???] LOOOOOOOPP"
else:
answer = "[ERROR] Desculpe, nao reconheco o comando \help" + argument
else:
answer = "[HELP] Comandos:\n \devs\n \datahora\n \weather <cidade>\n \weatherweek <cidade>\n \help <comando>"
print(answer)
return answer
def weather(argument):
"""
funcao que recebe como parametro o comando e testa se possui os parametros corretos
caso tenha parametro certo, requisita ao servico de metereologia, via http (POST e GET) e retorna a temperatura
atual da cidade requisitada
:param argument: string que eh a cidade a ser pesquisada
:return: condicao climatica atual da localizacao ou uma mensagem de erro caso a cidade nao tenha sido informada
"""
if argument != "":
print("[LOG] Pesquisando previsão...")
sockOpenWeather = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) # abre conexao com o servidor do Openweather
sockOpenWeather.connect(("api.openweathermap.org", 80)) # conecta com a api
request = (
'GET /data/2.5/weather?q=' + argument +
'&appid=778c214add5c6263ad53043ba7bf546d HTTP/1.1\r\nHost: api.openweathermap.org\r\n\r\n')
sockOpenWeather.sendall(request.encode()) # envia request GET http para a api
requisicao = sockOpenWeather.recv(1024).decode()
sockOpenWeather.close() # fecha conexao
requisicao = requisicao.split('{', 1) # divide a string em 2
js = requisicao[1] # pega apenas a parte que eh o json
js = '{' + js # readiciona uma { no inicio da string q eh o json
tempo = json.loads(js)
status = tempo['weather'][0]['description'] # recebe descrição do tempo em inglês dentro do dict 'tempo'
qChuva = 0
if tempo['weather'][0]['main'] == "Rain" and 'rain' in tempo: # verifica se existe palavra no dict 'tempo' e busca quantidade de chuva
qChuva = list(tempo['rain'].values())[0] #quantidade de chuva previsto
print("[LOG] Traduzindo...")
params = dict(key=API_T, text=status, lang='en-pt') #parametros para requisição com API de tradução
res = requests.get(urlt, params=params) #busca com uma API a tradução da previsão do tempo, retornando um json
jsonT = res.json() # transforma para dict o json recebido da API
status = str(jsonT['text'][0]) #pega string traduzida da API
#variavel com todas as informações atuais do tempo
answer = "[FORECAST] Cidade " + argument + "\nStatus atual: " + status + "\nPrevisão de chuva: " + str(
qChuva) + "mm\nTemperatura atual: " + str(
round((float(tempo['main']['temp'])) - 273.15)) + "°C\nUmidade em " + str(
tempo['main']['humidity']) + "%\nVelocidade do vento em " + str(
round((float(tempo['wind']['speed'])) * 3.6)) + " km/h"
else:
answer = "[ERROR] Comando \weather necessita de uma localizacao. Digite \help para saber mais"
return answer
def weatherWeek(argument):
"""
funcao que recebe uma cidade como parametro e retorna a previsao do tempo para ela, no periodo de uma semana
:param argument: string que representa a cidade a ser pesquisada
:return: temperatura da semana
"""
if argument != "":
print("[LOG] Cidade " + argument + " recebida do cliente e ")
city = urllib.parse.quote(str(argument)) # transforma string da cidade para formato URL p/ API reconhecer
print("transformada para " + str(city) + " no formato URLs!")
print("[LOG] Pesquisando previsão...")
sockAccuWeather = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) # abre conexao TCP com o servidor do AccuWeather
sockAccuWeather.connect(("dataservice.accuweather.com", 80)) # conecta com a API AccuWeather
# string de requisição
request = ('GET /locations/v1/cities/search?apikey=' + API +
'&q=' + city + '&details=true HTTP/1.1\r\nHost: dataservice.accuweather.com\r\n\r\n')
sockAccuWeather.sendall(request.encode('utf-8')) # envia request GET http para a api
requisicao = sockAccuWeather.recv(4096).decode() # recebe string com resposta do servidor
sockAccuWeather.close() # fecha conexao com API AccuWeather
requisicao = requisicao.split('[', 1) # divide a string em 2
requisicao = requisicao[1] # pega segunda parte da string
requisicao = requisicao.split(',"Country"', 1) # separa string novamente
requisicao = requisicao[0] # pega primeira parte da string separada
requisicao = requisicao + '}' # acrescenta um caracter no final da string
tempo = json.loads(requisicao) # transforma toda resposta da API para dict
location_key = tempo['Key']
sockAccuWeather = socket.socket(socket.AF_INET,
socket.SOCK_STREAM) # abre conexao com o servidor do AccuWeather
sockAccuWeather.connect(("dataservice.accuweather.com", 80)) # conecta com a api
# string de requisição
request = ('GET /forecasts/v1/daily/5day/' + location_key + '?apikey=' + API +
'&details=true HTTP/1.1\r\nHost: dataservice.accuweather.com\r\n\r\n')
sockAccuWeather.sendall(request.encode('utf-8')) # envia request GET http para a api
requisicao2 = sockAccuWeather.recv(16384).decode('utf-8') # recebe string com resposta do servidor
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
requisicao2 = requisicao2 + sockAccuWeather.recv(16384).decode(
'utf-8') # acrescenta resposta anterior com esta nova
sockAccuWeather.close() # fecha conexao com API AccuWeather
requisicao2 = requisicao2.split('{', 1) # divide a string em 2
requisicao2 = '{' + requisicao2[1]
tempo = json.loads(requisicao2) # transforma toda resposta da API para dict
resp = ""
print("[LOG] Traduzindo...")
respStatus = []
cont = 0
# este for vai percorrer posições do dict dos dados de cada dia da previsão e colocar
# todos os status em um dict para depois serem traduzidas
# vai colocar todos os status do turno do dia e noite de cada dia em uma list
for key1 in tempo['DailyForecasts']:
status = key1['Day']['LongPhrase']
respStatus.insert(cont, str(status))
cont = cont + 1
status = key1['Night']['LongPhrase']
respStatus.insert(cont, str(status))
cont = cont + 1
params = dict(key=API_T, text=str(respStatus),
lang='en-pt') # envia list anterior em formato string para API traduzir
res = requests.get(urlt, params=params) # recebe json da API com as palavras já traduzidas
jsonT = res.json() # transforma arquivo json recebido em dict
status = str(jsonT['text'][0])
string = status[1:len(status)]
list = string.split("', '")
list[0] = list[0][1:]
list[9] = list[9][:-2]
# este for vai buscar as temperaturas, chuva, vento e status de cada dia
cont = 0
for key1 in tempo['DailyForecasts']:
tempMin = round(((key1['Temperature']['Minimum']['Value']) - 32) / 1.8000)
tempMax = round(((key1['Temperature']['Maximum']['Value']) - 32) / 1.8000)
# parte da temperatura durante o dia
ventoD = round((key1['Day']['Wind']['Speed']['Value']) * 1.609)
qChuvaD = round((key1['Day']['Rain']['Value']) * 25.4)
statusD = list[cont]
cont = cont + 1
# parte da temperatura durante a noite---------------------------------------
ventoN = round((key1['Night']['Wind']['Speed']['Value']) * 1.609)
qChuvaN = round((key1['Night']['Rain']['Value']) * 25.4)
statusN = list[cont]
cont = cont + 1
diaData = key1['Date']
# resposta de cada dia que retorna para resposta prncipal dessa função
resp = resp + ("\n-----------\nData: " + diaData[8:10] + "/" + diaData[5:7] + "/" + diaData[0:4] +
"\nTemperatura mínima: " + str(tempMin) + "\nTemperatura máxima: " + str(tempMax) +
"\n--Durante o dia:" + "\nPrevisão do tempo: " + statusD + "\nChuva " +
str(qChuvaD) + "mm\nVento " + str(ventoD) +
"km/h\n--Durante a noite:" + "\nPrevisão do tempo: " + statusN + "\nChuva " +
str(qChuvaN) + "mm\nVento " + str(ventoN) + "km/h")
# resposta principal que retornada para cliente
answer = "[FORECAST] Previsão do tempo para " + argument + " durante os próximos dias:\n" + resp
else:
# retorna resposta se cliente deixou em branco o parâmetro de cidade
answer = "[ERROR] Comando \weatherweek necessita de uma localizacao. Digite \help para saber mais"
return answer # retorna resposta da função