-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* bluesky wip * Add bluesky download video support * Update readme * lint * pylint
- Loading branch information
1 parent
7246aa9
commit a19d18b
Showing
12 changed files
with
481 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import io | ||
import logging | ||
import sys | ||
import time | ||
import typing | ||
|
||
from api_24ur import downloader | ||
from wells import utils as wells_utils | ||
|
||
get_url_content = downloader.m3u8.get_url_content | ||
wells_utils.logger.addHandler(logging.NullHandler()) | ||
wells_utils.logger.propagate = False | ||
|
||
|
||
async def download_stream( # pylint: disable=too-many-positional-arguments | ||
stream_url: str, | ||
prefix: typing.Optional[str] = None, | ||
tmp_dir: str = '/tmp', | ||
pool_size: int = 5, | ||
max_bitrate: int = sys.maxsize, | ||
sleep: float = 0.1, | ||
) -> io.BytesIO: | ||
if prefix: | ||
# monkeypatch | ||
def _get_url_content(url): | ||
if not url.startswith('http'): | ||
url = prefix + url | ||
time.sleep(sleep) | ||
return get_url_content(url) | ||
|
||
downloader.m3u8.get_url_content = _get_url_content | ||
|
||
result = await downloader.Downloader( | ||
url=stream_url, | ||
download_path=tmp_dir, | ||
tmp_dir=tmp_dir, | ||
pool_size=pool_size, | ||
max_bitrate=max_bitrate, | ||
).download_bytes() | ||
|
||
downloader.m3u8.get_url_content = get_url_content | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import datetime | ||
import typing | ||
from urllib.parse import urlparse | ||
|
||
import atproto | ||
from atproto import models as atproto_models | ||
from django.conf import settings | ||
|
||
from bot import constants | ||
from bot import domain | ||
from bot import exceptions | ||
from bot import logger | ||
from bot.common import m3u8 | ||
from bot.common import utils | ||
from bot.integrations import base | ||
from bot.integrations.bluesky import config | ||
|
||
|
||
class BlueskyClientSingleton(base.BaseClientSingleton): | ||
DOMAINS = ['bsky.app'] | ||
_CONFIG_CLASS = config.BlueskyConfig | ||
|
||
@classmethod | ||
def should_handle(cls, url: str) -> bool: | ||
return super().should_handle(url) and '/post/' in url | ||
|
||
@classmethod | ||
def _create_instance(cls) -> None: | ||
conf: config.BlueskyConfig = cls._load_config(conf=settings.INTEGRATION_CONFIGURATION.get('bluesky', {})) | ||
|
||
if not conf.enabled: | ||
logger.info('Bluesky integration not enabled') | ||
cls._INSTANCE = base.MISSING | ||
return | ||
|
||
if not conf.username or not conf.password: | ||
logger.warning('Missing bluesky username or password') | ||
cls._INSTANCE = base.MISSING | ||
return | ||
|
||
if conf.base_url: | ||
cls.DOMAINS = [conf.base_url] | ||
|
||
cls._INSTANCE = BlueskyClient( | ||
username=conf.username, | ||
password=conf.password, | ||
base_url=conf.base_url, | ||
) | ||
|
||
|
||
class BlueskyClient(base.BaseClient): | ||
INTEGRATION = constants.Integration.BLUESKY | ||
|
||
def __init__(self, username: str, password: str, base_url: typing.Optional[str] = None): | ||
super().__init__() | ||
self.client = atproto.AsyncClient(base_url=base_url) | ||
self.logged_in = False | ||
self.username = username | ||
self.password = password | ||
|
||
async def _login(self): | ||
if self.logged_in: | ||
return | ||
|
||
profile = await self.client.login(self.username, self.password) | ||
self.logged_in = True | ||
logger.info('Logged in', integration=self.INTEGRATION.value, display_name=profile.display_name) | ||
|
||
async def get_integration_data(self, url: str) -> typing.Tuple[constants.Integration, str, typing.Optional[int]]: | ||
return self.INTEGRATION, url.strip('/').split('?')[0].split('/')[-1], None | ||
|
||
@classmethod | ||
def _url_to_uri(cls, url: str) -> atproto.AtUri: | ||
parsed_url = urlparse(url) | ||
|
||
parts = parsed_url.path.strip('/').split('/') | ||
if len(parts) != 4 or parts[0] != 'profile' or parts[2] != 'post': | ||
logger.error('Failed to parse bluesky url', url=url) | ||
return ValueError('Invalid Bluesky post URL format') | ||
|
||
did, rkey = parts[1], parts[3] | ||
return atproto.AtUri.from_str(f'at://{did}/app.bsky.feed.post/{rkey}') | ||
|
||
async def get_post(self, url: str) -> domain.Post: | ||
uri = self._url_to_uri(url) | ||
|
||
await self._login() | ||
|
||
thread = (await self.client.get_post_thread(uri.http)).thread | ||
if getattr(thread, 'not_found', False) or getattr(thread, 'blocked', False) or not thread.post: | ||
logger.error( | ||
'Post not found or blocked', | ||
url=url, | ||
uri=uri.http, | ||
not_found=getattr(thread, 'not_found', False), | ||
blocked=getattr(thread, 'blocked', False), | ||
) | ||
raise exceptions.IntegrationClientError('Failed to get post') | ||
|
||
post = domain.Post( | ||
url=url, | ||
author=thread.post.author.display_name, | ||
description=thread.post.record.text, | ||
created=datetime.datetime.fromisoformat(thread.post.record.created_at), | ||
likes=thread.post.like_count, | ||
) | ||
|
||
if thread.post.embed: | ||
logger.debug('Got bluesky media post', py_type=thread.post.embed.py_type) | ||
if atproto_models.ids.AppBskyEmbedImages in thread.post.embed.py_type: | ||
post.buffer = utils.combine_images( | ||
[await self._download(img.fullsize or img.thumb) for img in thread.post.embed.images[:3]] | ||
) | ||
elif atproto_models.ids.AppBskyEmbedVideo in thread.post.embed.py_type: | ||
stream_url = thread.post.embed.playlist or thread.post.embed.alt | ||
post.buffer = await m3u8.download_stream( | ||
stream_url=stream_url, | ||
prefix=stream_url[: stream_url.find('playlist.m3u8')], | ||
) | ||
|
||
return post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import typing | ||
|
||
from bot.integrations import base | ||
|
||
|
||
class BlueskyConfig(base.BaseClientConfig): | ||
username: typing.Optional[str] = None | ||
password: typing.Optional[str] = None | ||
base_url: typing.Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.