Skip to content

Commit

Permalink
24ur support
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic committed Aug 25, 2024
1 parent 8b258d2 commit 09100b2
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 6 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pymemcache = "==4.0.0"
"discord-oauth2.py" = "==1.2.1"
twitch-dl = "==2.3.1"
pydantic = "==2.8.2"
"24ur-api[download]" = "==0.1.4"

[dev-packages]
black = "==24.8.0"
Expand Down
35 changes: 30 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Integration(enum.Enum):
TIKTOK = 'tiktok'
REDDIT = 'reddit'
THREADS = 'threads'
TWENTY4_UR = '24ur'
TWITCH = 'twitch'
TWITTER = 'twitter'
YOUTUBE = 'youtube'
2 changes: 2 additions & 0 deletions bot/downloader/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from bot.downloader.reddit import client as reddit_client
from bot.downloader.threads import client as threads_client
from bot.downloader.tiktok import client as tiktok_client
from bot.downloader.twenty4ur import client as twenty4ur_client
from bot.downloader.twitch import client as twitch_client
from bot.downloader.twitter import client as twitter_client
from bot.downloader.youtube import client as youtube_client
Expand All @@ -17,6 +18,7 @@
reddit_client.RedditClientSingleton,
threads_client.ThreadsClientSingleton,
tiktok_client.TiktokClientSingleton,
twenty4ur_client.Twenty4UrClientSingleton,
twitch_client.TwitchClientSingleton,
twitter_client.TwitterClientSingleton,
youtube_client.YoutubeClientSingleton,
Expand Down
Empty file.
56 changes: 56 additions & 0 deletions bot/downloader/twenty4ur/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import hashlib
import typing

from api_24ur import client
from django.conf import settings

from bot import constants
from bot import domain
from bot import logger
from bot.downloader import base
from bot.downloader.twenty4ur import config


class Twenty4UrClientSingleton(base.BaseClientSingleton):
DOMAINS = ['24ur.com']
_CONFIG_SCHEMA = config.Twenty4UrConfig

@classmethod
def _create_instance(cls) -> None:
conf: config.Twenty4UrConfig = cls._load_config(conf=settings.INTEGRATION_CONFIGURATION.get('24ur', {}))

if not conf.enabled:
logger.info('24ur integration not enabled')
cls._INSTANCE = base.MISSING
return

cls._INSTANCE = Twenty4UrClient()


class Twenty4UrClient(base.BaseClient):
INTEGRATION = constants.Integration.TWENTY4_UR

def __init__(self) -> None:
super().__init__()
self.client = client.Client()

async def get_integration_data(self, url: str) -> typing.Tuple[constants.Integration, str, typing.Optional[int]]:
return self.INTEGRATION, hashlib.md5(self.client._path_from_url(url).encode()).hexdigest(), None

async def get_post(self, url: str) -> domain.Post:
article = await self.client.get_article_by_url(url=url)

buffer = None
if len(article.videos) > 0:
buffer = await self.client.download_video_bytes(stream_url=article.videos[0].url, max_bitrate=2000000)
elif len(article.images) > 0:
buffer = await self._download(url=article.images[0].url)

return domain.Post(
url=url,
author=article.author,
description=article.summary,
created=article.posted_at,
views=article.num_views,
buffer=buffer,
)
7 changes: 7 additions & 0 deletions bot/downloader/twenty4ur/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from bot.downloader import base


class Twenty4UrConfig(base.BaseClientConfig):
"""
No additional settings for 24ur integration
"""
6 changes: 6 additions & 0 deletions conf/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@
'youtube': {
'enabled': False,
},
'threads': {
'enable': False,
},
'24ur': {
'enable': False,
},
'twitter': {
'enabled': False,
'username': None,
Expand Down
18 changes: 18 additions & 0 deletions examples/settings_dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,21 @@
},
}
)

BOT_CONFIGURATION = {
'discord': {
'enabled': False,
'api_token': '',
},
}

OAUTH2_CONFIGURATION = {
'discord': {
'client_id': None,
'client_secret': None,
'redirect_uri': None,
'api_token': BOT_CONFIGURATION['discord']['api_token'],
},
}

LOGGING['loggers']['bot']['level'] = 'DEBUG'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dev = ["black", "flake8"]

[tool.black]
line-length = 120
target-version = ['py311']
target-version = ['py312']
include = '\.pyi?$'
skip-string-normalization = true

Expand Down
1 change: 1 addition & 0 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pkgs.mkShell {
curl
jq
file
ffmpeg
];

shellHook = ''
Expand Down

0 comments on commit 09100b2

Please sign in to comment.