Skip to content

Commit

Permalink
wip: discord
Browse files Browse the repository at this point in the history
  • Loading branch information
suchencjusz committed Sep 18, 2024
1 parent f1b6181 commit 5a15977
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 199 deletions.
10 changes: 3 additions & 7 deletions src/filman_crawler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ def check_connection() -> bool:
def main():
logging.info("Program started")

min_wait = 2 # Minimum wait time in seconds
max_wait = 60 # Maximum wait time in seconds
wait_time = min_wait
wait_time = 2

with ThreadPoolExecutor(max_workers=3) as executor:
while True:
Expand All @@ -164,16 +162,14 @@ def main():

if task is not None:
executor.submit(do_task, task)
wait_time = min_wait
else:
logging.info("No tasks found")
wait_time = min_wait

time.sleep(wait_time)


if __name__ == "__main__":
while not check_connection():
logging.error("Connection not established, retrying in 3 seconds")
time.sleep(3)
logging.error("Connection not established, retrying in 5 seconds")
time.sleep(5)
main()
3 changes: 1 addition & 2 deletions src/filman_crawler/tasks/scrap_movie.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import datetime
import logging
import ujson
from filman_server.database.schemas import FilmWebUserWatchedMovieCreate

from .utils import (
FilmWeb,
FilmWebMovie,
Expand Down
13 changes: 13 additions & 0 deletions src/filman_crawler/tasks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,16 @@ def add_watched_movie(self, info: FilmWebUserWatchedMovieCreate):
return False

return True

def check_if_user_exist_on_filmweb(self, filmweb_id: str):

upd = Updaters(HEADERS, self.endpoint_url)

# https://www.filmweb.pl/api/v1/user/sucheta348/preview

r = upd.fetch(f"https://www.filmweb.pl/api/v1/user/{filmweb_id}/preview")

if r is None:
return False

return True
4 changes: 2 additions & 2 deletions src/filman_discord/endpoints/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@configure_plugin.command
@lightbulb.command("configure", "Skonfiguruj bota")
@lightbulb.command("configure", "Konfiguracja bota")
@lightbulb.implements(lightbulb.SlashCommandGroup)
async def configure_group(_: lightbulb.SlashContext) -> None:
pass
Expand Down Expand Up @@ -67,7 +67,7 @@ async def channel_subcommand(ctx: lightbulb.SlashContext, text_channel: hikari.T
) as resp:
if not resp.ok:
await ctx.respond(
f"API returned a {resp.status} status :c",
f"Coś poszło nie tak! {resp.status} {resp.reason}",
flags=hikari.MessageFlag.EPHEMERAL,
)
return
Expand Down
7 changes: 6 additions & 1 deletion src/filman_discord/endpoints/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@ async def info_command(ctx: lightbulb.SlashContext) -> None:

embed.add_field(
name="Wersja i ostatnia aktualizacja",
value="`1.1.0` - `2024-09-07`",
value="`1.1.1` - `2024-09-18`",
)

embed.add_field(
name="Autor",
value="`@suchencjusz`",
)

embed.add_field(
name="Repozytorium",
value="https://github.com/suchencjusz/filman2",
)

embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
Expand Down
134 changes: 87 additions & 47 deletions src/filman_discord/endpoints/tracker.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from datetime import datetime
from typing import Optional

import logging
import hikari
import lightbulb

tracker_plugin = lightbulb.Plugin("Tracker")
tracker_plugin = lightbulb.Plugin("Filmweb")


@tracker_plugin.command
@lightbulb.command("tracker", "Zarządzaj powiadomieniami na serwerze")
@lightbulb.command("filmweb", "interakcje z filmwebem")
@lightbulb.implements(lightbulb.SlashCommandGroup)
async def tracker_group(_: lightbulb.SlashContext) -> None:
pass
Expand All @@ -20,72 +21,91 @@ async def tracker_group(_: lightbulb.SlashContext) -> None:
@lightbulb.command("me", "monitoruj swoje konto filmweb", pass_options=True)
@lightbulb.implements(lightbulb.SlashSubCommand)
async def me_subcommand(ctx: lightbulb.SlashContext, filmweb_username: str) -> None:

# Step 1: Create a user in the database using the discord_id
async with ctx.bot.d.client_session.post(
"http://filman_server:8000/users/create",
json={"discord_id": ctx.author.id, "filmweb_id": filmweb_username},
) as resp:
if not resp.ok:
if resp.status == 404:
json={"discord_id": ctx.author.id},
) as resp1:
pass

# Step 2: Retrieve the user information using the discord_id
async with ctx.bot.d.client_session.get(
"http://filman_server:8000/users/get",
params={"discord_id": ctx.author.id},
) as resp2:
if not resp2.ok:
await ctx.respond(
f"API zwróciło {resp2.status} status :c (2)",
flags=hikari.MessageFlag.EPHEMERAL,
)
return
else:
resp2_json = await resp2.json()
user_id = resp2_json.get("id")

# Step 3: Set the filmweb_id for the user in the database
async with ctx.bot.d.client_session.post(
"http://filman_server:8000/filmweb/user/mapping/set",
json={"user_id": user_id, "filmweb_id": filmweb_username},
) as resp3:
if not resp3.ok:
if resp3.status == 404:
embed = hikari.Embed(
title=f"Nie znaleziono użytkownika '{filmweb_username}' na filmwebie!",
colour=0xFF4400,
timestamp=datetime.now().astimezone(),
)

embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
)

await ctx.respond(embed, flags=hikari.MessageFlag.EPHEMERAL)

if resp.status == 409:
elif resp3.status == 409:
embed = hikari.Embed(
title=f"Użytkownik '{filmweb_username}' jest już monitorowany!",
colour=0xFF4400,
timestamp=datetime.now().astimezone(),
)

embed.add_field(
name="Zmiana konta filmweb",
value="""Jeśli chcesz zmienić monitorowane konto filmweb, musisz najpierw usunąć obecne!
W tym celu użyj komendy `/tracker cancel`""",
inline=True,
)

embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
)

await ctx.respond(embed, flags=hikari.MessageFlag.EPHEMERAL)

else:
await ctx.respond(
f"API zwróciło {resp3.status} status :c (3)",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

embed = hikari.Embed(
title=f"Konto {filmweb_username} zostało dodane!",
colour=0xFFC200,
timestamp=datetime.now().astimezone(),
)

embed.add_field(
name="Inicializacja",
value="Inicjalizacja twoich danych może chwilę potrwać, więc nie martw się, jeśli nie dostaniesz od razu powiadomienia!",
inline=True,
)

embed.add_field(
name="Powiadomienia na tym serwerze",
value="Aby włączyć powiadomienia na tym serwerze, użyj komendy `/tracker here`",
inline=True,
)

embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
)

await ctx.respond(embed)
# Step 4: Provide feedback to the user
embed = hikari.Embed(
title=f"Konto {filmweb_username} zostało dodane!",
colour=0xFFC200,
timestamp=datetime.now().astimezone(),
)
embed.add_field(
name="Inicializacja",
value="Inicjalizacja twoich danych może chwilę potrwać, więc nie martw się, jeśli nie dostaniesz od razu powiadomienia!",
inline=True,
)
embed.add_field(
name="Powiadomienia na tym serwerze",
value="Aby włączyć powiadomienia na tym serwerze, użyj komendy `/tracker here`",
inline=True,
)
embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
)
await ctx.respond(embed)


@tracker_group.child
Expand Down Expand Up @@ -182,22 +202,41 @@ async def stop_subcommand(ctx: lightbulb.SlashContext) -> None:


@tracker_group.child
@lightbulb.command("cancel", "przestań powiadamiać na WSZYSTKICH serwerach", pass_options=True)
@lightbulb.command("cancel", "przestań monitorować konto filmweb", pass_options=True)
@lightbulb.implements(lightbulb.SlashSubCommand)
async def cancel_subcommand(ctx: lightbulb.SlashContext) -> None:
async with ctx.bot.d.client_session.post(
"http://filman_server:8000/users/remove_from_all_guilds", # SPRAWDZ ENDPONT
json={"id_discord": ctx.author.id},
async with ctx.bot.d.client_session.delete(
"http://filman_server:8000/filmweb/user/mapping/delete",
params={"discord_id": ctx.author.id},
) as resp:
if resp.status == 404:
embed = hikari.Embed(
title=f"Nie monitorujesz żadnego konta filmweb!",
colour=0xFF4400,
timestamp=datetime.now().astimezone(),
)
embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
)
await ctx.respond(embed, flags=hikari.MessageFlag.EPHEMERAL)
return

if not resp.ok:
await ctx.respond(
f"API zwróciło {resp.status} status :c",
flags=hikari.MessageFlag.EPHEMERAL,
embed = hikari.Embed(
title=f"API zwróciło {resp.status} status :c",
colour=0xFF4400,
timestamp=datetime.now().astimezone(),
)
embed.set_footer(
text=f"Requested by {ctx.author}",
icon=ctx.author.display_avatar_url,
)
await ctx.respond(embed, flags=hikari.MessageFlag.EPHEMERAL)
return

embed = hikari.Embed(
title=f"Powiadomienia na WSZYSTKICH serwerach zostały wyłączone!",
title=f"To koniec monitorowania twojego konta filmweb!",
colour=0xFFC200,
timestamp=datetime.now().astimezone(),
)
Expand All @@ -207,7 +246,8 @@ async def cancel_subcommand(ctx: lightbulb.SlashContext) -> None:
icon=ctx.author.display_avatar_url,
)

await ctx.respond(embed)
await ctx.respond(embed, flags=hikari.MessageFlag.EPHEMERAL)
return


def load(bot: lightbulb.BotApp) -> None:
Expand Down
Loading

0 comments on commit 5a15977

Please sign in to comment.