From 8730dcd7f1b9ab758df902d56fc0c580940ac202 Mon Sep 17 00:00:00 2001 From: nihonium Date: Thu, 27 Jan 2022 22:35:15 +0300 Subject: [PATCH] websockets --- bot.py | 43 +++++++++++++++++++++++++++++++++++++++++-- config.json.example | 8 ++++---- msk.py | 3 ++- 3 files changed, 47 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index a395a30..6e017a0 100755 --- a/bot.py +++ b/bot.py @@ -3,6 +3,8 @@ import logging as log import requests import json import msk +import websockets +import asyncio, aiohttp def json_read(file): print("Reading config file: {}".format(file)) @@ -12,6 +14,12 @@ def json_read(file): config = json_read('config.json') +#try: +# config = json_read('config.json') +#except FileNotFoundError: +# print("Config file not found. Maybe we should launch setup.py?") + + if config['verbosity'].upper() == 'DEBUG': log.basicConfig(format="%(levelname)s: %(message)s", level=log.DEBUG) log.info("Verbose output") @@ -19,6 +27,37 @@ if config['verbosity'].upper() == 'DEBUG': msk = msk.Misskey(i=config['token'], url=config['url']) -fid = msk.drive_files_create("yaoshi-jun-Toosaka-Rin-Fate-(series)-Anime-3306019.jpeg", isSensitive=True)['id'] -msk.notes_create(text="Wannya see my picture?", cw="Nya?", fileIds=[fid]) +#fid = msk.drive_files_create("yaoshi-jun-Toosaka-Rin-Fate-(series)-Anime-3306019.jpeg", isSensitive=True)['id'] +#msk.notes_create(text="Wannya see my picture?", cw="Nya?", fileIds=[fid]) +async def main(): + async with websockets.connect(msk.ws_url) as ws: + print('Connecting') + p = { + 'type': 'connect', + 'body': { + 'channel': 'homeTimeline', + 'id': 'Nya1' + } + } + await ws.send(json.dumps(p)) + p = { + 'type': 'connect', + 'body': { + 'channel': 'main', + 'id': 'Nya2' + } + } + await ws.send(json.dumps(p)) + + print('Listening') + while True: + data = await ws.recv() + j = json.loads(data) + print(j) + +while True: + try: + asyncio.get_event_loop().run_until_complete(main()) + except KeyboardInterrupt: + break diff --git a/config.json.example b/config.json.example index a47ce1e..c207b1d 100644 --- a/config.json.example +++ b/config.json.example @@ -1,7 +1,7 @@ { - "nyan.nekoea.red": { "name": "nyan.nekoea.red", - "url" : "https://nyan.nekoea.red", - "token": "nyanyanyanyanyanyanya", - }, + "url" : "nyan.nekoea.red", + "token": "*****", + "verbosity": "debug" } + diff --git a/msk.py b/msk.py index 83caf0e..d7de9f7 100644 --- a/msk.py +++ b/msk.py @@ -8,8 +8,9 @@ class Misskey: ''' def __init__(self, url: str, i: str): - self.url = url + self.url = f"https://{url}" self.i = i + self.ws_url=f"wss://{url}/streaming&i={i}" def notes_create(self, text=None, visibility = "public", cw = None, fileIds = list(), replyId = None, renoteId = None): req_url = self.url + "/api/notes/create"