Skip to content

Commit

Permalink
Add default post formats for each integration (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
amadejkastelic authored Nov 29, 2024
1 parent a346c19 commit 65c8468
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 97 deletions.
10 changes: 3 additions & 7 deletions bot/domain.py → bot/domain/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# TODO: Refactor
import datetime
import io
import typing
from dataclasses import dataclass

from bot import constants
from bot.common import utils
from bot.domain import post_format


DEFAULT_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👀 Views: {views}
👍🏻 Likes: {likes} 👎🏻 Dislikes: {dislikes}
📕 Description: {description}\n
"""
DEFAULT_POST_FORMAT = post_format.DEFAULT_POST_FORMAT

DEFAULT_COMMENT_FORMAT = """🧑🏻‍🎨 Author: {author}
📅 Created: {created}
Expand Down
91 changes: 91 additions & 0 deletions bot/domain/post_format.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import typing

from bot import constants


DEFAULT_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👀 Views: {views}
👍🏻 Likes: {likes} 👎🏻 Dislikes: {dislikes}
📕 Description: {description}\n
"""

FOUR_CHAN_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
📕 Description: {description}\n
"""

INSTAGRAM_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👍🏻 Likes: {likes}\n
"""

REDDIT_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👍🏻 Likes: {likes} 👎🏻 Dislikes: {dislikes}
📕 Description: {description}\n
"""

THREADS_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👍🏻 Likes: {likes}
📕 Description: {description}\n
"""

TIKTOK_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👀 Views: {views}
👍🏻 Likes: {likes}\n
"""

TWENTY4_UR_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
📕 Description: {description}\n
"""

TWITCH_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👀 Views: {views}
📕 Description: {description}\n
"""

TWITTER_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👀 Views: {views}
👍🏻 Likes: {likes}
📕 Description: {description}\n
"""

YOUTUBE_POST_FORMAT = """🔗 URL: {url}
🧑🏻‍🎨 Author: {author}
📅 Created: {created}
👀 Views: {views}
👍🏻 Likes: {likes} 👎🏻 Dislikes: {dislikes}\n
"""


DEFAULT_INTEGRATION_POST_FMT_MAPPING = {
constants.Integration.FACEBOOK: DEFAULT_POST_FORMAT,
constants.Integration.FOUR_CHAN: FOUR_CHAN_POST_FORMAT,
constants.Integration.INSTAGRAM: INSTAGRAM_POST_FORMAT,
constants.Integration.REDDIT: REDDIT_POST_FORMAT,
constants.Integration.THREADS: THREADS_POST_FORMAT,
constants.Integration.TIKTOK: TIKTOK_POST_FORMAT,
constants.Integration.TWENTY4_UR: TWENTY4_UR_POST_FORMAT,
constants.Integration.TWITCH: TWITCH_POST_FORMAT,
constants.Integration.TWITTER: TWITTER_POST_FORMAT,
constants.Integration.YOUTUBE: YOUTUBE_POST_FORMAT,
}


def get_or_default(integration: constants.Integration, default: typing.Optional[str]) -> str:
return DEFAULT_INTEGRATION_POST_FMT_MAPPING.get(integration, default or DEFAULT_POST_FORMAT)
4 changes: 4 additions & 0 deletions bot/integrations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class BaseClientSingleton:
_INSTANCE: typing.Optional[typing.Union[BaseClient, int]] = None
_CONFIG_CLASS = BaseClientConfig

@classmethod
def should_handle(cls, url: str) -> bool:
return any(domain in url for domain in cls.DOMAINS)

@classmethod
def _create_instance(cls) -> None:
raise NotImplementedError()
Expand Down
2 changes: 1 addition & 1 deletion bot/integrations/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

def should_handle(url: str) -> bool:
for klass in CLASSES:
if any(domain in url for domain in klass.DOMAINS):
if klass.should_handle(url):
return klass.get_instance() is not None

return False
Expand Down
4 changes: 4 additions & 0 deletions bot/integrations/twitch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def _create_instance(cls) -> None:

cls._INSTANCE = TwitchClient()

@classmethod
def should_handle(cls, url: str) -> bool:
return super().should_handle(url) and '/clip/' in url


class TwitchClient(base.BaseClient):
INTEGRATION = constants.Integration.TWITCH
Expand Down
8 changes: 7 additions & 1 deletion bot/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from bot import logger
from bot import models
from bot import repository
from bot.domain import post_format
from bot.integrations import registry


Expand Down Expand Up @@ -130,7 +131,12 @@ async def get_post( # noqa: C901
logger.debug('Post already in DB, not downloading again...', url=url)

# Set formatting
post.set_format(server.integrations[integration].post_format)
post.set_format(
post_format.get_or_default(
integration=integration,
default=server.integrations[integration].post_format,
)
)

repository.save_server_post(
server_vendor=server_vendor,
Expand Down
Loading

0 comments on commit 65c8468

Please sign in to comment.