[+] Encapsulate sendRequest()

This commit is contained in:
Hykilpikonna
2020-11-22 18:10:21 -05:00
parent 4c1afe9d93
commit 108db468d7
+17 -6
View File
@@ -1,11 +1,13 @@
import difflib
import json
import re import re
from io import BytesIO from io import BytesIO
from telegram import Bot, Update from telegram import Bot, Update
from telegram.ext import Updater, CallbackContext from telegram.ext import Updater, CallbackContext, Job
from src.database import Database from src.database import Database
from src.utils import toJson, create from src.utils import toJson, create, dictToString
helpMsg = """ helpMsg = """
Welcome! This bot monitors http changes! Welcome! This bot monitors http changes!
@@ -41,6 +43,15 @@ cache: {str: {str: str}} = {}
updater: Updater = {} updater: Updater = {}
def sendRequest(req: str):
r = create(req)
text = r.text
if r.headers['Content-Type'] == 'application/json':
text = dictToString(json.loads(text))
r.close()
return text
# Initialize bot # Initialize bot
def init(bot: Bot, u: Updater): def init(bot: Bot, u: Updater):
global updater global updater
@@ -133,12 +144,12 @@ def test(update: Update, context: CallbackContext):
return "*Error:* %s doesn't exist." % name return "*Error:* %s doesn't exist." % name
# Run # Run
r = create(database.userRequests[user][name]) text = sendRequest(database.userRequests[user][name])
body = r.text
if len(body) > 60000: if len(text) > 60000:
return "File too large (>60kb)." return "File too large (>60kb)."
context.bot.send_document(chat_id=chat.id, document=BytesIO(bytes(r.text, 'utf-8')), filename=name + '.txt') context.bot.send_document(chat_id=chat.id, document=BytesIO(bytes(text, 'utf-8')), filename=name + '.txt')
return 'Done!' return 'Done!'