Skip to content

Commit

Permalink
Refactored a util for getting the local time. Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Nov 19, 2022
1 parent 1be7ccb commit fec703b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
15 changes: 3 additions & 12 deletions newshomepages/archive.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import logging
import os
import typing
from datetime import datetime
from pathlib import Path

import click
import internetarchive
import pytz
from rich import print

from . import utils
Expand Down Expand Up @@ -62,7 +60,7 @@ def cli(

# Upload into an "item" keyed to the site's handle and year
handle = data["handle"]
local_now = _get_now_local(data)
local_now = utils.get_local_time(data)
site_identifier = f"{utils.safe_ia_handle(handle)}-{local_now.strftime('%Y')}"
site_metadata = _get_item_metadata(data)
print(
Expand Down Expand Up @@ -106,21 +104,14 @@ def cli(
)


def _get_now_local(data: typing.Dict) -> datetime:
"""Get the current time in the provided site's timezone."""
now = datetime.now()
tz = pytz.timezone(data["timezone"])
return now.astimezone(tz)


def _get_item_metadata(data: typing.Dict) -> typing.Dict:
"""Convert a site's metadata into the format we'll use in its archive.org item."""
# Verify we have an archive.org collection
assert IA_COLLECTION

# Get the current year where the site is based,
# since we segment each site's items by calendar year.
now_year = _get_now_local(data).strftime("%Y")
now_year = utils.get_local_time(data).strftime("%Y")

# Format a metadata dictionary ready to post to the archive.org upload method
return dict(
Expand All @@ -144,7 +135,7 @@ def _get_file_dict(data: typing.Dict, input_dir: Path) -> typing.Dict:
wayback_path = input_dir / f"{handle}.wayback.json"

# Get the local time where the site is based
now_local = _get_now_local(data)
now_local = utils.get_local_time(data)

# Convert it to ISO format for timestamping our files
now_iso = now_local.isoformat()
Expand Down
17 changes: 2 additions & 15 deletions newshomepages/discorder.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import os
import time
import typing
from datetime import datetime
from pathlib import Path

import click
import discord
import pytz
from retry import retry
from rich import print

Expand All @@ -29,12 +27,7 @@ def single(handle: str, input_dir: str):
# Get the metadata
site = utils.get_site(handle)

# Get the timestamp
now = datetime.now()

# Convert it to local time
tz = pytz.timezone(site["timezone"])
now_local = now.astimezone(tz)
now_local = utils.get_local_time(site)

# Create the caption
caption = (
Expand All @@ -52,13 +45,7 @@ def bundle(slug: str, input_dir: str):
"""Post all images for a bundle."""
# Get the metadata
bundle = utils.get_bundle(slug)

# Get the timestamp
now = datetime.now()

# Convert it to local time
tz = pytz.timezone(bundle["timezone"])
now_local = now.astimezone(tz)
now_local = utils.get_local_time(bundle)

# Create the caption
caption = f"{bundle['name']} homepages at {now_local.strftime('%-I:%M %p')} in {bundle['location']}\n"
Expand Down
7 changes: 7 additions & 0 deletions newshomepages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ def get_json_url(url: str):
return r.json()


def get_local_time(data: typing.Dict) -> datetime:
"""Get the current time in the provided site's timezone."""
now = datetime.now()
tz = pytz.timezone(data["timezone"])
return now.astimezone(tz)


def parse_archive_url(url: str):
"""Parse the handle and timestamp from an archive.org URL."""
o = urlparse(url)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ def test_get_url():
utils.get_json_url(
"https://archive.org/download/signalcleveland-2022/signalcleveland-2022-11-17T02%3A50%3A58.280867-05%3A00.wayback.json"
)


def test_get_local_time():
"""Test method to get the local time."""
utils.get_local_time(utils.get_site("latimes"))
utils.get_local_time(utils.get_bundle("us-national"))

0 comments on commit fec703b

Please sign in to comment.