Skip to content

Commit

Permalink
wip: w2s filmweb
Browse files Browse the repository at this point in the history
  • Loading branch information
suchencjusz committed Jan 3, 2025
1 parent f340491 commit 498db5a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 62 deletions.
25 changes: 23 additions & 2 deletions src/filman_discord/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# FROM python:3.11-alpine

# RUN apk update && apk add --no-cache \
# gcc \
# musl-dev \
# libffi-dev \
# openssl-dev \
# mariadb-dev \
# build-base

# WORKDIR /src/filman_discord

# COPY requirements.txt requirements.txt
# RUN pip install --no-cache-dir -r requirements.txt

# COPY /src/filman_discord .

# CMD [ "python3", "-OO", "main.py"]

FROM python:3.11-alpine

RUN apk update && apk add --no-cache \
Expand All @@ -13,6 +32,8 @@ WORKDIR /src/filman_discord
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

COPY /src/filman_discord .
COPY src /src

ENV PYTHONPATH=/src

CMD [ "python3", "-OO", "main.py"]
CMD ["python3", "-OO", "main.py"]
67 changes: 8 additions & 59 deletions src/filman_discord/endpoints/filmweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import logging


tracker_plugin = lightbulb.Plugin("Filmweb")


Expand Down Expand Up @@ -331,62 +332,7 @@ async def cancel_subcommand(ctx: lightbulb.SlashContext) -> None:
return


# https://www.reddit.com/r/Discordjs/comments/10377qg/adding_the_possibility_to_mention_multiple_users/
# https://hikari-lightbulb.readthedocs.io/en/2.3.5/api_references/decorators.html#lightbulb.decorators.option

# @tracker_group.child
# @lightbulb.option("users", "Wymień użytkowników, np. @User1 @User2", type=str, required=True)
# @lightbulb.command("w2s", "wylosuj film z listy chcę obejrzeć dla użytkownika/użytkowników", pass_options=True)
# @lightbulb.implements(lightbulb.SlashSubCommand)
# async def w2s_subcommand(ctx: lightbulb.SlashContext, users: str) -> None:
# mentions = ctx.options.users.split()
# user_ids = []

# for mention in mentions:
# if mention.startswith("<@") and mention.endswith(">"):
# mention = mention.strip("<@!>")
# if mention.isdigit():
# user_ids.append(int(mention))

# if not user_ids:
# await ctx.respond("Nie podano żadnych prawidłowych oznaczeń użytkowników.")
# return

# response = f"Oznaczono {len(user_ids)} użytkowników:\n" + "\n".join(f"<@{uid}>" for uid in user_ids)
# await ctx.respond(response)


# @tracker_group.child
# @lightbulb.option("user5", "Piąty użytkownik (opcjonalny)", type=hikari.User, required=False)
# @lightbulb.option("user4", "Czwarty użytkownik (opcjonalny)", type=hikari.User, required=False)
# @lightbulb.option("user3", "Trzeci użytkownik (opcjonalny)", type=hikari.User, required=False)
# @lightbulb.option("user2", "Drugi użytkownik (opcjonalny)", type=hikari.User, required=False)
# @lightbulb.option("user1", "Pierwszy użytkownik (wymagany)", type=hikari.User, required=True)
# @lightbulb.command("w2s", "wylosuj film z listy chcę obejrzeć dla użytkownika/użytkowników", pass_options=True)
# @lightbulb.implements(lightbulb.SlashCommand)
# async def w2s_subcommand(
# ctx: lightbulb.SlashContext,
# user1: hikari.User,
# user2: hikari.User = None,
# user3: hikari.User = None,
# user4: hikari.User = None,
# user5: hikari.User = None,
# ) -> None:
# response = f"Oznaczono {len([user1, user2, user3, user4, user5])} użytkowników:\n" + "\n".join(
# f"{user.mention}" for user in [user1, user2, user3, user4, user5]
# )
# await ctx.respond(response)

# TODO: add logic to process users in utils
# from src.filman_discord.utils.w2s_logic import process_users

# users = [user1, user2, user3, user4, user5]
# await ctx.respond("Przetwarzanie...")

# response = await process_users(users)
# await ctx.edit_last_response(response)


# todo: draw within common friends list
@tracker_group.child
@lightbulb.option("user5", "Piąty użytkownik (opcjonalny)", type=hikari.User, required=False)
@lightbulb.option("user4", "Czwarty użytkownik (opcjonalny)", type=hikari.User, required=False)
Expand All @@ -409,9 +355,13 @@ async def w2s_subcommand(
response = f"Oznaczono {len(mentioned_users)} użytkowników:\n" + "\n".join(mentioned_users)
await ctx.respond("Przetwarzanie...")

await asyncio.sleep(5)
logging.debug(f"Processing users: {users}")

from filman_discord.utils.filmweb_w2s_logic import process_users


await ctx.edit_last_response(await process_users(users))

await ctx.edit_last_response(response)

@tracker_group.child
@lightbulb.command("test", "Testowa komenda", pass_options=True)
Expand All @@ -420,6 +370,5 @@ async def test_command(ctx: lightbulb.SlashContext) -> None:
await ctx.respond("Działa!")



def load(bot: lightbulb.BotApp) -> None:
bot.add_plugin(tracker_plugin)
2 changes: 1 addition & 1 deletion src/filman_discord/endpoints/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ async def info_command(ctx: lightbulb.SlashContext) -> None:

embed.add_field(
name="Wersja i ostatnia aktualizacja",
value="`1.1.8t dev` - `2024-12-27`",
value="`1.1.8t dev` - `2025-01-03`",
)

embed.add_field(
Expand Down
52 changes: 52 additions & 0 deletions src/filman_discord/utils/filmweb_w2s_logic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import asyncio
import random
import requests
import logging
import hikari

logger = logging.getLogger(__name__)

async def process_users(users):
mentioned_users = [user.mention for user in users if user is not None]
response = f"Mentioned {len(mentioned_users)} users:\n" + "\n".join(mentioned_users)

logger.debug(f"Users: {mentioned_users}")

filmweb_ids = {}
for user in users:
if user is not None:
logger.debug(f"Fetching Filmweb ID for user: {user.id}")
r = requests.get(f"http://filman_server:8000/filmweb/user/mapping/get", params={"discord_id": user.id})
if r.status_code == 200:
filmweb_id = r.json().get("filmweb_id")
if filmweb_id:
filmweb_ids[filmweb_id] = user
logger.debug(f"Found Filmweb ID: {filmweb_id} for user: {user.id}")
else:
logger.warning(f"No Filmweb ID found for user: {user.id}")
else:
logger.error(f"Error fetching Filmweb ID for user: {user.id}, status: {r.status_code}")

movies_to_watch = []
for filmweb_id, user in filmweb_ids.items():
logger.debug(f"Fetching list of movies to watch for Filmweb ID: {filmweb_id}")
r = requests.get(f"https://www.filmweb.pl/api/v1/user/{filmweb_id}/want2see/film")
if r.status_code == 200:
movie_entities = r.json()
for movie in movie_entities:
movie_id = movie.get("entity")
movie_url = f"https://www.filmweb.pl/film/x-1-{movie_id}"
movies_to_watch.append((movie_url, user))
logger.debug(f"Added movie to list: {movie_url} from user: {user.id}")
else:
logger.error(f"Error fetching list of movies for Filmweb ID: {filmweb_id}, status: {r.status_code}")

if movies_to_watch:
selected_movie, selected_user = random.choice(movies_to_watch)
response += f"\n\nSelected movie: {selected_movie} from {selected_user.mention}'s list"
logger.info(f"Selected movie: {selected_movie} from {selected_user.id}'s list")
else:
response += "\n\nNo movies found to watch."
logger.warning("No movies found to watch.")

return response

0 comments on commit 498db5a

Please sign in to comment.