[F] Move sendRequest to utils to avoid circular import

This commit is contained in:
Hykilpikonna
2020-11-27 09:31:48 -05:00
parent ea95450333
commit 65cb1a8842
3 changed files with 13 additions and 13 deletions
+2 -11
View File
@@ -9,7 +9,7 @@ from telegram.ext import Updater, CallbackContext, Job
from src.database import Database from src.database import Database
from src.scheduler import Scheduler from src.scheduler import Scheduler
from src.utils import toJson, create, dictToString, render, wrap from src.utils import toJson, create, dictToString, render, wrap, sendRequest
helpMsg = """ helpMsg = """
Welcome! This bot monitors http changes! Welcome! This bot monitors http changes!
@@ -44,15 +44,6 @@ scheduler: Scheduler
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
@@ -63,7 +54,7 @@ def init(bot: Bot, u: Updater):
for user in database.users: for user in database.users:
for request in database.reqs[user]: for request in database.reqs[user]:
if request['enabled']: if request['enabled']:
scheduler.startTask(user, request) scheduler.start(user, request)
def start(update: Update, context: CallbackContext): def start(update: Update, context: CallbackContext):
+1 -2
View File
@@ -4,9 +4,8 @@ from io import BytesIO
from telegram.ext import Job, CallbackContext, Updater from telegram.ext import Job, CallbackContext, Updater
from src.commands import sendRequest
from src.database import Database from src.database import Database
from src.utils import wrap, render from src.utils import wrap, render, sendRequest
class Scheduler: class Scheduler:
+10
View File
@@ -1,3 +1,4 @@
import difflib
import json import json
import textwrap import textwrap
from urllib.parse import unquote from urllib.parse import unquote
@@ -96,3 +97,12 @@ def render(message):
def wrap(string: str): def wrap(string: str):
return '\n'.join(['\n '.join(textwrap.wrap(line, 120)) for line in string.splitlines()]) return '\n'.join(['\n '.join(textwrap.wrap(line, 120)) for line in string.splitlines()])
def sendRequest(req):
response = create(req)
text = response.text
if response.headers['Content-Type'] == 'application/json':
text = dictToString(json.loads(text))
response.close()
return text