Skip to content

Commit

Permalink
Merge pull request #23 from lukecyca/fix-close-time-override
Browse files Browse the repository at this point in the history
Fix --close_time_override
  • Loading branch information
lukecyca authored Jan 18, 2025
2 parents 3b9f202 + 8d32743 commit e6e58c2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
23 changes: 21 additions & 2 deletions libs/discourse_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import logging
import pickle
from datetime import datetime
import zoneinfo

vancouver_timezone = zoneinfo.ZoneInfo("America/Vancouver")

from pydiscourse import DiscourseClient

Expand All @@ -23,7 +26,22 @@ def generate_post_winners(all_items: list) -> str:

post += "<h1>Raffle Results</h1>\n\n"

last_close_time = None

for item in all_items:

# Technically each poll could have a different close time.
# This handles that by printing it once at the beginning, and
# again each time we get to a new poll where it differs from the last.
if last_close_time != item["close_time"]:
ts = datetime.fromtimestamp(
item["close_time"], tz=vancouver_timezone)
post += "Raffle was closed at: {} ({})\n\n".format(
ts.isoformat(),
item["close_time"],
)
last_close_time = item["close_time"]

post += f"**{item['description']}**\n"

post += "\nWinners:\n"
Expand Down Expand Up @@ -112,7 +130,7 @@ def get_all_voters(self, post_id, poll_name, option_id):

raise Exception("Received too many pages of voters")

def get_all_polls(self, post_id: int, close_time_override=None) -> list:
def get_all_polls(self, post_id: int, close_time_override: datetime = None) -> list:
assert isinstance(post_id, int)

topic = self._discource_client.topic_posts(str(post_id))
Expand All @@ -132,7 +150,8 @@ def get_all_polls(self, post_id: int, close_time_override=None) -> list:
winnable_item["id"] = item["id"]

if close_time_override:
winnable_item["close_time"] = close_time_override
winnable_item["close_time"] = int(
close_time_override.timestamp())
else:
try:
winnable_item["close_time"] = int(
Expand Down
8 changes: 5 additions & 3 deletions raffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from base64 import b64encode
from gzip import compress
import re
from datetime import datetime

from libs.crypto_helper import get_dice_roll, get_hash, hash_xor
from libs.discourse_helper import (
Expand Down Expand Up @@ -67,9 +68,9 @@ def parse_args(parser):
)
parser.add_argument(
"--close_time_override",
type=int,
type=datetime.fromisoformat,
action="store",
help="override the time the poll closes.",
help="override the time the poll closes (e.g.: 2024-08-10 00:00:00-07:00)",
)
parser.add_argument(
"--api-key",
Expand Down Expand Up @@ -150,7 +151,8 @@ def main():
"Hitting discourse topic to find some polls."
+ " It can take a bit if there are a lot of polls."
)
all_items = discourse_connection.get_all_polls(args.topic_id)
all_items = discourse_connection.get_all_polls(
args.topic_id, close_time_override=args.close_time_override)
info("Got %s polls from topic" % len(all_items))

# Add in the crypto parts to the results we got from Discourse
Expand Down

0 comments on commit e6e58c2

Please sign in to comment.