valcanobacon / BoostBots

Bots which posts Booots
BSD 3-Clause "New" or "Revised" License
18 stars 7 forks source link

Support cross app comments. #19

Open dhk2 opened 1 year ago

dhk2 commented 1 year ago

Big fan. Though of a possible enhancement today while working on PeerTube Boostagram support.

If the item being boosted has podcast:socialinteract configured with the activitypub protocol, The bot could post as a reply to the base post instead of to its own timeline. Thus making the boosts show up in cross-app comments for the episode.

valcanobacon commented 1 year ago

brilliant

valcanobacon commented 1 year ago

Made a little Proof of Concept (PoC) and it works fine. When the Boost comes in it needs to pull the RSS feed and find related item. I did not except the ID of the status on the local server to be different then that on the remote server; It needs to find the local ID via the V2 search API before posting a reply.

import asyncio
import atoot
import logging
import requests
from urllib.parse import urlparse
from bs4 import BeautifulSoup
from uuid import uuid4

mastodon_instance='noagendasocial.com'
mastodon_access_token='REDACTED'

feed_url='https://feeds.podcastindex.org/pc20.xml'
episode_title='Episode 134: ChapterGPT'
episode_guid='PC20134'

logging.getLogger().setLevel(logging.DEBUG)

async def main():
    logging.info("Start")
    mastodon = await atoot.MastodonAPI.create(
        mastodon_instance, access_token=mastodon_access_token
    )
    resp = await mastodon.verify_account_credentials()
    logging.debug(resp)
    logging.info(f"Connected to {mastodon_instance}")

    reply_to_id = None

    response = requests.get(feed_url)
    if response.status_code in [requests.status_codes.codes.ok]:
        soup = BeautifulSoup(response.text, "lxml")
        items = soup.find_all(["podcast:liveitem", "item"])
        item = next(filter(lambda x: x.title.get_text() == episode_title or x.guid.get_text() == episode_guid, items), None)
        if item is not None:
            social_interact = item.find("podcast:socialinteract", protocol="activitypub")
            if social_interact is not None and social_interact['uri'] is not None:
                status = await mastodon.search(social_interact['uri'], resolve=True)
                if status is not None and status.get('statuses'):
                    reply_to_id = status.get('statuses', [{}])[0].get('id')

    print(reply_to_id)

    resp = await mastodon.create_status(
       status=str(uuid4()),
       in_reply_to_id=reply_to_id,
    )
    logging.debug(resp)

    await mastodon.close()
    logging.info(f"Closed")

asyncio.run(main())