looks like i finally have this shit working
This commit is contained in:
parent
62f474f3e9
commit
b1a3309cc7
1 changed files with 18 additions and 43 deletions
61
listen.py
61
listen.py
|
|
@ -5,19 +5,16 @@ from requests import get, post
|
|||
from random import randint
|
||||
from os import getenv
|
||||
from munch import munchify
|
||||
from lxml import html
|
||||
from markdownify import markdownify as md
|
||||
from pprint import pp
|
||||
import csv, io
|
||||
import sys
|
||||
import apprise
|
||||
|
||||
def strip_html(s):
|
||||
return str(html.fromstring(s).text_content())
|
||||
|
||||
apprise_object = apprise.Apprise()
|
||||
apprise_object.add('pover://dP9LCGHZRMozXRn6K5PctGg6uZhaYc@af7xxr6ho94isdzc3uj92m3f82hdkb', tag='pover')
|
||||
apprise_object.add('zulip://sandbox@chat.fyrfli.org/KiXbeBR7poxmUuuW3S6b9igX2ibpyfNC/sandbox', tag='zulip')
|
||||
# apprise_object.notify(body='is this even working from inside this program?', title='fyrfli at mastodon.social timeline', tag='zulip')
|
||||
|
||||
|
||||
api_token = getenv('MASTODON_SOCIAL_FYRFLI_TOKEN')
|
||||
|
|
@ -29,10 +26,6 @@ quotes_list = loads(dumps(list(csv.DictReader(io.StringIO(get(quotes_src).text))
|
|||
instance_info = munchify(loads(get(f"https://{endpoint}/api/v1/instance").text))
|
||||
streaming_api = f"{instance_info.urls.streaming_api}/api/v1/streaming?access_token={api_token}&stream=user"
|
||||
|
||||
# print(streaming_api)
|
||||
# sys.exit(0)
|
||||
|
||||
# streaming_api=f"wss://{endpoint}/api/v1/streaming?access_token={api_token}&stream=user"
|
||||
post_uri = f"https://{endpoint}/api/v1/statuses"
|
||||
headers = { "Content-Type" : "application/json",
|
||||
"Authorization" : f"Bearer {api_token}",
|
||||
|
|
@ -62,7 +55,6 @@ def get_random_quote():
|
|||
|
||||
def process_request(the_post):
|
||||
print('processing ', the_post.url)
|
||||
# pp(dumps(the_post), compact=True, indent=2)
|
||||
hellos = ["hey" in the_post.content.lower(),"hey you" in the_post.content.lower(), "hi" in the_post.content.lower(), "hello" in the_post.content.lower()]
|
||||
request_quote = ["random quote" in the_post.content.lower(), "quote please" in the_post.content.lower()]
|
||||
if any(hellos):
|
||||
|
|
@ -82,59 +74,41 @@ def process_request(the_post):
|
|||
|
||||
|
||||
async def hello(uri):
|
||||
# async with websockets.connect(uri) as ws:
|
||||
async for ws in websockets.connect(uri):
|
||||
async for ws in websockets.connect(streaming_api):
|
||||
try:
|
||||
message = await ws.recv()
|
||||
decoded = munchify(loads(message))
|
||||
# print(decoded.event, decoded.payload[1:150])
|
||||
# print(decoded.event, decoded.payload)
|
||||
if decoded.event != "delete":
|
||||
the_post = munchify(loads(decoded.payload))
|
||||
# print(decoded.payload[1:250])
|
||||
match decoded.event:
|
||||
case "delete":
|
||||
# message = f"a delete event for id: {decoded.payload}\\n===>\\n"
|
||||
# print("a delete event for id ", decoded.payload, "\n===>\n")
|
||||
message = 'a delete event for id: ', decoded.payload, '===>'
|
||||
message = f"a delete event for id: {decoded.payload}\n===>\n"
|
||||
|
||||
case "status.update":
|
||||
# print("an update to an existing post: ", the_post.url, "\n===>\n")
|
||||
# message = f"an update to an existing post: {the_post.url}\\n===>\\n"
|
||||
message = 'an update to an existing post: ', the_post.url, '===>'
|
||||
message = f"an update to an existing post: {the_post.url}\n===>\n"
|
||||
|
||||
case "update":
|
||||
if the_post.reblog:
|
||||
# print( "a reblog of ", the_post.reblog.url)
|
||||
reblog_post_id = the_post.reblog.url.split('/')[4]
|
||||
reblogged_post = munchify(loads(get(f'{post_uri}/{reblog_post_id}').text))
|
||||
# pp(md(reblogged_post.content))
|
||||
# print("\n===>\n")
|
||||
# message = f"a reblog of {the_post.reblog.url}\\n{pp(md(reblogged_post.content))}\\n===>\\n"
|
||||
message = 'a reblog of ', the_post.reblog.url, pp(md(reblogged_post.content)),'===>'
|
||||
message = f"a reblog of {the_post.reblog.url}"
|
||||
if reblogged_post.content:
|
||||
message += "\n{(pp(md(reblogged_post.content)) or \"unknown content\")}\n===>\n"
|
||||
else:
|
||||
# plain_text_content = strip_html(the_post.content)
|
||||
post_content = md(the_post.content)
|
||||
# print( "new status: ", the_post.url, "\n", post_content, "\n===>\n" )
|
||||
# message = f"new status: {the_post.url}\\n{post_content}\\n===>\\n"
|
||||
message = 'new status: ', the_post.url, post_content, '===>'
|
||||
message = f"new status: {the_post.url}\n{post_content}\n===>\n"
|
||||
if the_post.mentions:
|
||||
for mention in the_post.mentions:
|
||||
if mention.username == "botsy":
|
||||
process_request(the_post)
|
||||
# break
|
||||
|
||||
case "notification":
|
||||
process_request(the_post)
|
||||
|
||||
case _:
|
||||
# print('unknown event: ', decoded.event, "\n",
|
||||
# " with payload beginning ",
|
||||
# decoded.payload[1:150])
|
||||
# message = f"unknown event: {decoded.event}\\n{decoded.payload[1:200]}"
|
||||
message = 'unknown event: ', decoded.event, decoded.payload[1:120]
|
||||
pp(message)
|
||||
apprise_object.notify(body=message, title='@fyrfli.mastodon.social timeline')
|
||||
message = f"unknown event: {decoded.event}\n{decoded.payload[1:200]}"
|
||||
print(message)
|
||||
apprise_object.notify(body=message, title='fyrfli at mastodon.social timeline', tag='zulip')
|
||||
|
||||
except asyncio.exceptions.CancelledError:
|
||||
print('keyboard interrupt received ... exiting')
|
||||
|
|
@ -145,15 +119,16 @@ async def hello(uri):
|
|||
break
|
||||
|
||||
except websockets.ConnectionClosed:
|
||||
break
|
||||
print("connection closed ... reconnecting ...")
|
||||
continue
|
||||
|
||||
except websockets.exceptions.ConnectionClosed:
|
||||
print("connection closed ... reconnecting ...")
|
||||
continue
|
||||
|
||||
async def main():
|
||||
async with websockets.serve(hello, streaming_api):
|
||||
await asyncio.Future()
|
||||
|
||||
# print(__name__)
|
||||
# sys.exit(0)
|
||||
async with websockets.connect(hello, streaming_api):
|
||||
await asyncio.Future()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(hello(streaming_api))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue