Skip to content

Commit

Permalink
remove the arrow lib and don't use deprecated utcnow()
Browse files Browse the repository at this point in the history
  • Loading branch information
honzajavorek committed Oct 3, 2024
1 parent 9767681 commit e864422
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 89 deletions.
4 changes: 2 additions & 2 deletions jg/coop/cli/cancel_previous_builds.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import UTC, datetime, timedelta

import click
from pycircleci.api import Api
Expand All @@ -19,7 +19,7 @@ def main(circleci_branch, circleci_workflow_id, production, circleci_api_key):
pipelines = circleci.get_project_pipelines(
"juniorguru", "junior.guru", branch=circleci_branch, paginate=True
)
recently = datetime.utcnow() - timedelta(days=3)
recently = datetime.now(UTC) - timedelta(days=3)
pipelines = [
pipeline
for pipeline in pipelines
Expand Down
10 changes: 4 additions & 6 deletions jg/coop/lib/google_coerce.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from datetime import datetime
from urllib.parse import urlparse

import arrow


def coerce(mapping, record):
data = {}
Expand Down Expand Up @@ -42,19 +40,19 @@ def parse_datetime(value):
if value:
value = value.strip()
try:
return arrow.get(value, "M/D/YYYY H:m:s").naive
return datetime.strptime(value, "%m/%d/%Y %H:%M:%S")
except ValueError:
return arrow.get(datetime.fromisoformat(value)).naive
return datetime.fromisoformat(value).replace(tzinfo=None)


def parse_date(value):
if value:
value = value.strip()
try:
return arrow.get(value, "M/D/YYYY H:m:s").date()
return datetime.strptime(value, "%m/%d/%Y %H:%M:%S").date()
except ValueError:
try:
return arrow.get(value, "M/D/YYYY").date()
return datetime.strptime(value, "%m/%d/%Y").date()
except ValueError:
return datetime.fromisoformat(value).date()

Expand Down
7 changes: 5 additions & 2 deletions jg/coop/lib/template_filters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import math
import random
import re
from datetime import UTC
from numbers import Number
from operator import itemgetter
from typing import Generator, Iterable, Literal
from urllib.parse import unquote, urljoin
from zoneinfo import ZoneInfo

import arrow
from markupsafe import Markup
from mkdocs.structure import StructureItem
from mkdocs.structure.nav import Navigation
Expand Down Expand Up @@ -41,7 +42,9 @@ def remove_p(html):


def local_time(dt):
return arrow.get(dt).to("Europe/Prague").format("H:mm")
dt_utc = dt.replace(tzinfo=UTC)
dt_prg = dt_utc.astimezone(ZoneInfo("Europe/Prague"))
return dt_prg.strftime("%-H:%M")


def weekday(dt):
Expand Down
19 changes: 11 additions & 8 deletions jg/coop/models/club.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import math
from datetime import date, timedelta
from datetime import date, datetime, timedelta
from enum import StrEnum, auto, unique
from itertools import groupby
from operator import attrgetter
Expand Down Expand Up @@ -351,10 +350,14 @@ def channel_listing(

# TODO squash with channel_listing
@classmethod
def channel_listing_since(cls, channel_id: int, since: datetime) -> Iterable[Self]:
def channel_listing_since(
cls, channel_id: int, since_at: datetime
) -> Iterable[Self]:
if since_at.tzinfo:
raise ValueError("Naive UTC datetime expected, got timezone-aware")
return (
cls.select()
.where((cls.channel_id == channel_id) & (cls.created_at >= since))
.where((cls.channel_id == channel_id) & (cls.created_at >= since_at))
.order_by(cls.created_at)
)

Expand All @@ -369,20 +372,20 @@ def forum_listing(cls, channel_id: int) -> Iterable[Self]:
)

@classmethod
def digest_listing(cls, since: datetime, limit: int = 5) -> Iterable[Self]:
def digest_listing(cls, since_on: date, limit: int = 5) -> Iterable[Self]:
return (
cls.select()
.where(
cls.is_private == False, # noqa: E712
ClubMessage.parent_channel_id.not_in(UPVOTES_EXCLUDE_CHANNELS),
cls.created_at >= since,
cls.created_at >= datetime.combine(since_on, datetime.min.time()),
)
.order_by(cls.upvotes_count.desc())
.limit(limit)
)

@classmethod
def digest_channels(cls, since: datetime, limit: int = 5) -> Iterable[dict]:
def digest_channels(cls, since_on: date, limit: int = 5) -> Iterable[dict]:
size = fn.sum(cls.content_size).alias("size")
return (
cls.select(
Expand All @@ -395,7 +398,7 @@ def digest_channels(cls, since: datetime, limit: int = 5) -> Iterable[dict]:
.where(
cls.is_private == False, # noqa: E712
ClubMessage.parent_channel_id.not_in(UPVOTES_EXCLUDE_CHANNELS),
cls.created_at >= since,
cls.created_at >= datetime.combine(since_on, datetime.min.time()),
)
.group_by(cls.channel_id)
.order_by(size.desc())
Expand Down
13 changes: 7 additions & 6 deletions jg/coop/models/event.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import math
from datetime import datetime, timedelta
from datetime import UTC, datetime, timedelta
from zoneinfo import ZoneInfo

import arrow
from peewee import (
CharField,
DateTimeField,
Expand Down Expand Up @@ -41,7 +41,8 @@ def full_title(self) -> str:

@property
def start_at_prg(self):
return arrow.get(self.start_at).to("Europe/Prague").naive
start_at_utc = self.start_at.replace(tzinfo=UTC)
return start_at_utc.astimezone(ZoneInfo("Europe/Prague")).replace(tzinfo=None)

@property
def end_at(self):
Expand Down Expand Up @@ -77,7 +78,7 @@ def to_card(self) -> dict:

@classmethod
def next(cls, now=None):
now = now or datetime.utcnow()
now = now or datetime.now(UTC).replace(tzinfo=None)
return cls.select().where(cls.start_at >= now).order_by(cls.start_at).first()

@classmethod
Expand All @@ -98,12 +99,12 @@ def api_listing(cls):

@classmethod
def archive_listing(cls, now=None):
now = now or datetime.utcnow()
now = now or datetime.now(UTC).replace(tzinfo=None)
return cls.select().where(cls.start_at < now).order_by(cls.start_at.desc())

@classmethod
def planned_listing(cls, now=None):
now = now or datetime.utcnow()
now = now or datetime.now(UTC).replace(tzinfo=None)
return cls.select().where(cls.start_at >= now).order_by(cls.start_at)

@classmethod
Expand Down
11 changes: 6 additions & 5 deletions jg/coop/sync/club_content/store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import arrow
import peewee
from discord import DMChannel, Member, Message, User

Expand Down Expand Up @@ -35,7 +34,7 @@ def store_member(member: Member) -> ClubUser:
has_avatar=bool(member.avatar),
display_name=member.display_name,
mention=member.mention,
joined_at=arrow.get(member.joined_at).naive,
joined_at=member.joined_at.replace(tzinfo=None),
initial_roles=get_user_roles(member),
)

Expand All @@ -60,7 +59,9 @@ def _store_user(user: User) -> ClubUser:
display_name=user.display_name,
mention=user.mention,
joined_at=(
arrow.get(user.joined_at).naive if hasattr(user, "joined_at") else None
user.joined_at.replace(tzinfo=None)
if hasattr(user, "joined_at")
else None
),
initial_roles=get_user_roles(user),
)
Expand Down Expand Up @@ -96,10 +97,10 @@ def store_message(message: Message) -> ClubMessage:
},
upvotes_count=count_upvotes(message.reactions),
downvotes_count=count_downvotes(message.reactions),
created_at=arrow.get(message.created_at).naive,
created_at=message.created_at.replace(tzinfo=None),
created_month=f"{message.created_at:%Y-%m}",
edited_at=(
arrow.get(message.edited_at).naive if message.edited_at else None
message.edited_at.replace(tzinfo=None) if message.edited_at else None
),
author=_store_user(message.author),
author_is_bot=message.author.id == ClubMemberID.BOT,
Expand Down
16 changes: 9 additions & 7 deletions jg/coop/sync/digest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,25 @@ def main(force_since):

@db.connection_context()
async def sync_digest(client: ClubClient, force_since: date):
since = force_since or date.today() - timedelta(weeks=1)
since_on = force_since or date.today() - timedelta(weeks=1)
message = ClubMessage.last_bot_message(ClubChannelID.ANNOUNCEMENTS, DIGEST_EMOJI)

if not is_message_older_than(message, since):
if not is_message_older_than(message, since_on):
if not force_since:
logger.info("Digest not needed")
return
logger.warning("Digest forced!")

if not force_since and message:
since = message.created_at.date()
logger.info(f"Analyzing since {since}")
since_on = message.created_at.date()
logger.info(f"Analyzing since {since_on}")

content = f"{DIGEST_EMOJI} Co se tu dělo za poslední týden? (od {since:%-d.%-m.})"
content = (
f"{DIGEST_EMOJI} Co se tu dělo za poslední týden? (od {since_on:%-d.%-m.})"
)

logger.info(f"Listing {TOP_MESSAGES_LIMIT} top messages")
messages = ClubMessage.digest_listing(since, limit=TOP_MESSAGES_LIMIT)
messages = ClubMessage.digest_listing(since_on, limit=TOP_MESSAGES_LIMIT)
for n, message in enumerate(messages, start=1):
logger.info(
f"Message #{n}: {message.upvotes_count} votes for {message.author.display_name} in #{message.channel_name}, {message.url}"
Expand All @@ -70,7 +72,7 @@ async def sync_digest(client: ClubClient, force_since: date):
)

logger.info(f"Listing {TOP_CHANNELS_LIMIT} top channels")
channels_digest = ClubMessage.digest_channels(since, limit=TOP_CHANNELS_LIMIT)
channels_digest = ClubMessage.digest_channels(since_on, limit=TOP_CHANNELS_LIMIT)
for n, channel_digest in enumerate(channels_digest, start=1):
logger.info(
f"Channel #{n}: {channel_digest['size']} characters in {channel_digest['channel_name']!r}, parent channel #{channel_digest['parent_channel_name']}"
Expand Down
14 changes: 8 additions & 6 deletions jg/coop/sync/events.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import date, timedelta
from datetime import UTC, date, datetime, timedelta
from pathlib import Path
from zoneinfo import ZoneInfo

import arrow
import click
from discord import ScheduledEvent
from strictyaml import CommaSeparated, Int, Map, Optional, Seq, Str, Url, load
Expand Down Expand Up @@ -151,7 +151,7 @@ def main(clear_posters):
@db.connection_context()
async def sync_scheduled_events(client: ClubClient):
discord_events = {
arrow.get(scheduled_event.start_time).naive: scheduled_event
scheduled_event.start_time.replace(tzinfo=None): scheduled_event
for scheduled_event in client.club_guild.scheduled_events
if is_event_scheduled_event(scheduled_event)
}
Expand Down Expand Up @@ -294,10 +294,12 @@ async def post_next_event_messages(client: ClubClient):


def load_record(record):
start_at = arrow.get(
start_at_prg = datetime(
*map(int, str(record.pop("date")).split("-")),
*map(int, record.pop("time").split(":")),
tzinfo="Europe/Prague",
tzinfo=ZoneInfo("Europe/Prague"),
)
record["start_at"] = start_at.to("UTC").naive
start_at_utc = start_at_prg.astimezone(UTC)
start_at = start_at_utc.replace(tzinfo=None)
record["start_at"] = start_at
return record
4 changes: 2 additions & 2 deletions jg/coop/sync/intro.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from datetime import datetime, timedelta
from datetime import UTC, datetime, timedelta

import discord
from discord import MessageType, TextChannel
Expand Down Expand Up @@ -49,7 +49,7 @@ async def sync_intro(client: ClubClient):
discord_channel = await client.club_guild.fetch_channel(ClubChannelID.INTRO)

logger.info("Processing messages")
since_at = datetime.utcnow() - PROCESS_HISTORY_SINCE
since_at = datetime.now(UTC).replace(tzinfo=None) - PROCESS_HISTORY_SINCE
tasks = []
for message in ClubMessage.channel_listing_since(ClubChannelID.INTRO, since_at):
tasks.append(asyncio.create_task(process_message(discord_channel, message)))
Expand Down
6 changes: 2 additions & 4 deletions jg/coop/web/context.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
from datetime import date, timedelta
from datetime import UTC, date, datetime, timedelta
from operator import attrgetter
from urllib.parse import urljoin

import arrow

from jg.coop.lib import loggers
from jg.coop.lib.discord_club import CLUB_GUILD
from jg.coop.models.base import db
Expand Down Expand Up @@ -41,7 +39,7 @@

@db.connection_context()
def on_shared_context(context):
now = arrow.utcnow()
now = datetime.now(UTC)
today = now.date()
context["now"] = now
context["today"] = today
Expand Down
4 changes: 2 additions & 2 deletions jg/coop/web_legacy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import arrow
from datetime import UTC, datetime
from flask import Flask, render_template, url_for
from flask_frozen import Freezer

Expand Down Expand Up @@ -166,7 +166,7 @@ def not_found():

@app.context_processor
def inject_defaults():
now = arrow.utcnow()
now = datetime.now(UTC)
return dict(nav_tabs=NAV_TABS, now=now, today=now.date())


Expand Down
32 changes: 1 addition & 31 deletions poetry.lock

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

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ python = "~3.12"
"jg.chick" = { git = "https://github.com/juniorguru/chick.git" }
apify-client = "1.7.1"
apify-shared = "1.1.2"
arrow = "1.3.0"
beautifulsoup4 = "4.12.3"
click = "8.1.7"
cssselect = "1.2.0"
Expand Down
Loading

0 comments on commit e864422

Please sign in to comment.