-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from jrafaelrn:ifood_alive_beta
start ifood keep alive test
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import base64 | ||
import requests | ||
import sender | ||
|
||
|
||
def check_if_alive(): | ||
|
||
# URL que será acessada | ||
urls = [ | ||
{'loja': 'Vianelo', | ||
'url': 'https://www.ifood.com.br/delivery/jundiai-sp/palhadini-palha-italiana-doce-e-salgado-vila-argos-nova/6a608776-d22b-4784-ba02-587446fc3a01' | ||
}, | ||
{'loja': 'Medeiros', | ||
'url': 'https://www.ifood.com.br/delivery/jundiai-sp/palhadini-palha-italiana---doces-medeiros/ff1f48c4-3f94-448a-bdcf-c9ba07333fdd' | ||
} | ||
] | ||
|
||
|
||
for loja in urls: | ||
|
||
nome_loja = loja['loja'] | ||
url = loja['url'] | ||
msg = None | ||
|
||
try: | ||
# Faz a requisição GET para a URL | ||
response = requests.get(url) | ||
|
||
# Verifica se a requisição foi bem-sucedida (código 200) | ||
if response.status_code == 200: | ||
# Verifica se o texto "Loja Fechada" está presente no conteúdo da página | ||
if "Loja Fechada" in response.text: | ||
msg = f"'Loja Fechada': {loja['loja']}." | ||
else: | ||
msg = f"'Loja Aberta': {loja['loja']}." | ||
else: | ||
msg = f"Erro ao acessar a página da loja {loja['loja']}. Código de status: {response.status_code}" | ||
|
||
except requests.exceptions.RequestException as e: | ||
msg = f"Ocorreu um erro na requisição para a loja {loja['loja']}: {e}" | ||
|
||
# Envia a mensagem para o Telegram | ||
sender.send_message(msg, '572312369') | ||
|
||
|
||
################################# | ||
# Call from Cloud Functions # | ||
################################# | ||
|
||
def check(event, context): | ||
|
||
if type(event) != str: | ||
payload = base64.b64decode(event['data']).decode('utf-8') | ||
else: | ||
payload = event | ||
|
||
response = None | ||
|
||
print(f'Payload: {payload}') | ||
|
||
try: | ||
check_if_alive() | ||
response = {"return": "ok"} | ||
except: | ||
response = {"return": "error"} | ||
|
||
print(f'Response: {response}') | ||
return response | ||
|
||
|
||
if __name__ == '__main__': | ||
check_if_alive() | ||
print('Check Ifood Alive sucecesfully!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os, json, requests | ||
|
||
############################################## | ||
# CLOUD FUNCTION # | ||
############################################## | ||
|
||
def send_message(message, chat_id): | ||
|
||
FUNCTION_NAME = 'function-telegram' | ||
|
||
# DATA | ||
data = { | ||
'message_type': 'text', | ||
'content' : message, | ||
'chat_id': chat_id | ||
} | ||
|
||
# CLOUD FUNCTION | ||
cloud_function(FUNCTION_NAME, data) | ||
|
||
|
||
|
||
def cloud_function(FUNCTION_NAME, DATA, ARGS=None): | ||
|
||
REGION = os.environ.get("REGION") | ||
GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID") | ||
|
||
data = json.dumps(DATA) | ||
headers = {'Content-Type': 'application/json'} | ||
link = f'https://{REGION}-{GCP_PROJECT_ID}.cloudfunctions.net/{FUNCTION_NAME}' | ||
|
||
if ARGS: | ||
link += f'?{ARGS}' | ||
|
||
resp = requests.post(link, data = data, headers = headers) | ||
|
||
if resp.status_code == 200: | ||
#print(f'Function {FUNCTION_NAME} executed!') | ||
#print(f'Response: {resp.status_code} - Text: {resp.text} - Content: {resp.content}') | ||
|
||
try: | ||
try: | ||
response = json.loads(resp.content) | ||
except: | ||
response = resp.content | ||
except: | ||
response = resp.text | ||
|
||
return response | ||
|
||
else: | ||
print(f'!!! Function {FUNCTION_NAME} not executed! - Data: {data} - Status Code: {resp.status_code} - Text: {resp.text}') | ||
return None | ||
|