Skip to content

Commit

Permalink
Cálculo de próximo directo con horario local
Browse files Browse the repository at this point in the history
  • Loading branch information
mouredev committed Feb 24, 2024
1 parent 707bf77 commit a2ed280
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
18 changes: 15 additions & 3 deletions link_bio/link_bio/state/PageState.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,26 @@

class PageState(rx.State):

timezone = ""
live_status = Live(live=False, title="")
next_live = ""
featured_info: list[Featured]

async def check_live(self, timezone: str):
async def check_live(self):
self.live_status = await live(USER)
if not self.live_status.live:
self.next_live = utils.next_date(await schedule(), timezone)

def check_schedule(self):
if self.timezone == "":
return rx.call_script(
utils.LOCAL_TIMEZONE_SCRIPT,
PageState.update_timezone
)
else:
self.update_timezone(self.timezone)

async def update_timezone(self, timezone: str):
self.timezone = timezone
self.next_live = utils.next_date(await schedule(), self.timezone)

async def featured_links(self):
self.featured_info = await featured()
28 changes: 14 additions & 14 deletions link_bio/link_bio/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from datetime import datetime, timedelta, timezone
import pytz
import locale
import reflex as rx

# Común
Expand Down Expand Up @@ -43,22 +42,11 @@ def lang() -> rx.Component:

# Date


def local_timezone() -> str:
return datetime.now().astimezone().tzname()
LOCAL_TIMEZONE_SCRIPT = "Intl.DateTimeFormat().resolvedOptions().timeZone"


def next_date(dates: dict, timezone: str) -> str:

# Se intenta forzar el locale para traducir el formateo de fecha a español
try:
locale.setlocale(locale.LC_TIME, "es_ES")
except:
try:
locale.setlocale(locale.LC_TIME, "es_ES.utf8")
except:
pass

if len(dates) == 0:
return ""

Expand Down Expand Up @@ -87,6 +75,18 @@ def next_date(dates: dict, timezone: str) -> str:
next_date.year, next_date.month, next_date.day,
time_utc.hour, time_utc.minute, tzinfo=pytz.UTC).astimezone(tz)

return local_date.strftime("%A, %d de %B a las %H:%M").capitalize() + f" ({timezone} {tz} {locale.getlocale()})"
weekdays = {
0: "Lunes",
1: "Martes",
2: "Miércoles",
3: "Jueves",
4: "Viernes",
5: "Sábado",
6: "Domingo",
}

day = "Hoy" if weekday == 0 else weekdays[local_date.weekday()]

return local_date.strftime(f"{day.capitalize()}, %d/%m a las %H:%M") + f" ({timezone} {tz})"

return ""
21 changes: 12 additions & 9 deletions link_bio/link_bio/views/header.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import reflex as rx
import datetime
import link_bio.constants as const
import link_bio.utils as utils
from link_bio.styles.styles import Size, Spacing
from link_bio.styles.colors import Color, TextColor
from link_bio.components.link_icon import link_icon
Expand Down Expand Up @@ -122,15 +121,19 @@ def header(details=True) -> rx.Component:
const.TWITCH_URL,
highlight_color=Color.PURPLE.value
),
rx.cond(
PageState.next_live,
link_button(
"Próximo directo",
rx.box(
rx.cond(
PageState.next_live,
"/icons/twitch.svg",
const.TWITCH_URL,
highlight_color=Color.PURPLE.value
link_button(
"Próximo directo",
PageState.next_live,
"/icons/twitch.svg",
const.TWITCH_URL,
highlight_color=Color.PURPLE.value
),
),
width="100%",
on_mount=PageState.check_schedule
)
),
rx.text(
Expand All @@ -150,7 +153,7 @@ def header(details=True) -> rx.Component:
width="100%",
spacing=Spacing.BIG.value,
align_items="start",
on_mount=PageState.check_live(utils.local_timezone())
on_mount=PageState.check_live
)


Expand Down

0 comments on commit a2ed280

Please sign in to comment.