Compare commits
1
Commits
5c4b1a7dbd
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c750a80206 |
+17
-6
@@ -6,7 +6,7 @@ import re
|
|||||||
import settings
|
import settings
|
||||||
from telethon import TelegramClient, events
|
from telethon import TelegramClient, events
|
||||||
|
|
||||||
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH).start(bot_token=settings.API_TOKEN)
|
bot = TelegramClient('data/bot', settings.API_ID, settings.API_HASH)
|
||||||
web = None
|
web = None
|
||||||
api = lambda route: settings.DYNMAP_ADDRESS + route
|
api = lambda route: settings.DYNMAP_ADDRESS + route
|
||||||
md5 = lambda name: int(hashlib.md5(name.encode()).hexdigest(), 16)
|
md5 = lambda name: int(hashlib.md5(name.encode()).hexdigest(), 16)
|
||||||
@@ -58,9 +58,7 @@ async def new_message(event):
|
|||||||
logging.exception('Problem sending message to dynmap:')
|
logging.exception('Problem sending message to dynmap:')
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def bridge_loop():
|
||||||
global web
|
|
||||||
web = aiohttp.ClientSession()
|
|
||||||
last_time = 0
|
last_time = 0
|
||||||
last_msg = ''
|
last_msg = ''
|
||||||
logging.info('Bridge loaded')
|
logging.info('Bridge loaded')
|
||||||
@@ -93,5 +91,18 @@ async def main():
|
|||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
with bot:
|
|
||||||
bot.loop.run_until_complete(main())
|
async def main():
|
||||||
|
global web
|
||||||
|
await bot.start(bot_token=settings.API_TOKEN)
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
web = session
|
||||||
|
await bridge_loop()
|
||||||
|
finally:
|
||||||
|
web = None
|
||||||
|
await bot.disconnect()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
asyncio.run(main())
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
import asyncio
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
import unittest
|
import unittest
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
@@ -10,26 +12,19 @@ import aiohttp
|
|||||||
ROOT = Path(__file__).parent
|
ROOT = Path(__file__).parent
|
||||||
|
|
||||||
|
|
||||||
class _FakeLoop:
|
|
||||||
def run_until_complete(self, coroutine):
|
|
||||||
coroutine.close()
|
|
||||||
|
|
||||||
|
|
||||||
class _FakeBot:
|
class _FakeBot:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.loop = _FakeLoop()
|
self.started_loop = None
|
||||||
|
self.disconnected_loop = None
|
||||||
|
|
||||||
def on(self, event):
|
def on(self, event):
|
||||||
return lambda callback: callback
|
return lambda callback: callback
|
||||||
|
|
||||||
def start(self, **kwargs):
|
async def start(self, **kwargs):
|
||||||
return self
|
self.started_loop = asyncio.get_running_loop()
|
||||||
|
|
||||||
def __enter__(self):
|
async def disconnect(self):
|
||||||
return self
|
self.disconnected_loop = asyncio.get_running_loop()
|
||||||
|
|
||||||
def __exit__(self, *args):
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def load_service():
|
def load_service():
|
||||||
@@ -91,6 +86,21 @@ class TeleDynmapUpgradeTests(unittest.TestCase):
|
|||||||
self.assertGreaterEqual(expected % len(service.emojis), 0)
|
self.assertGreaterEqual(expected % len(service.emojis), 0)
|
||||||
self.assertLess(expected % len(service.emojis), len(service.emojis))
|
self.assertLess(expected % len(service.emojis), len(service.emojis))
|
||||||
|
|
||||||
|
def test_telethon_and_http_session_share_loop_and_are_closed(self):
|
||||||
|
class StopBridge(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
async def stop_bridge():
|
||||||
|
raise StopBridge
|
||||||
|
|
||||||
|
with patch.object(service, 'bridge_loop', stop_bridge):
|
||||||
|
with self.assertRaises(StopBridge):
|
||||||
|
asyncio.run(service.main())
|
||||||
|
|
||||||
|
self.assertIsNotNone(service.bot.started_loop)
|
||||||
|
self.assertIs(service.bot.disconnected_loop, service.bot.started_loop)
|
||||||
|
self.assertIsNone(service.web)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
Reference in New Issue
Block a user