Skip to content

Commit

Permalink
fix history bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Morris committed May 12, 2024
1 parent b6cf8df commit af3670c
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
poems
-----

All of the poems in here are good, or interesting. There are currently 7,790 poems by 531 poets.
All of the poems in here are good, or interesting. There are currently 7,793 poems by 532 poets.
4 changes: 4 additions & 0 deletions poems/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def month(self):
def day(self):
return self.datetime.day

@property
def isoformat(self):
return self.datetime.isoformat()

@property
def pretty_date(self):
return timestamp_to_pretty_date(self.timestamp)
Expand Down
19 changes: 17 additions & 2 deletions poems/curator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import numpy as np
from .errors import AuthorNotFoundError, PoemNotFoundError
from .catalog import Catalog
from .poem import Author, Poem

here, this_file = os.path.split(__file__)

Expand All @@ -13,14 +14,28 @@ class Curator():
def __init__(self, filepath=f"{here}/poems.json"):

self.catalog = Catalog(filepath=filepath)



def get_author(self, author=None) -> Author:

all_authors = list(self.catalog.data.keys())

if author is None:
author = np.random.choice(all_authors)

if author in all_authors:
return Author(tag=author, **self.catalog.data[author]["metadata"])

raise ValueError(f"No author '{author}'.")


def get_poem(
self,
author=None,
title=None,
verbose=False,
very_verbose=False,
):
) -> Poem:

verbose = verbose or very_verbose
mask = self.catalog.likelihood > 0
Expand Down
13 changes: 9 additions & 4 deletions poems/poem.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ def __post_init__(self):
def __repr__(self):
return f"{self.__class__.__name__}({self.title_by_author})"

@property
def translator(self) -> str:
return self.metadata["translator"] if "translator" in self.metadata else ""

@property
def keywords(self) -> dict:
return self.metadata["keywords"] if "keywords" in self.metadata else {}
Expand Down Expand Up @@ -109,6 +105,15 @@ def spacetime(self):
parts.append(self.metadata["location"])
return ". ".join(parts) or ""

@property
def translator(self) -> str:
return self.metadata.get("translator")

@property
def translation(self) -> str:
return f"Translated by {self.translator}" if self.translator is not None else ""


@property
def test_email_subject(self):
return f"TEST ({self.context.pretty_date}): {self.title_by_author} {self.keywords}"
Expand Down
85 changes: 73 additions & 12 deletions poems/poems.json

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions poems/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,24 @@ def make_author_stats(history, catalog=None):
if author not in entries:

author_mask = history.author.values == author
days_since_last_sent = (timestamp - history.loc[author_mask, "timestamp"].max()) / 86400
timestamp_last_sent = history.loc[author_mask, "timestamp"].max()
isoformat_last_sent = datetime.fromtimestamp(timestamp_last_sent).astimezone(pytz.utc).isoformat()
days_since_last_sent = (timestamp - timestamp_last_sent) / 86400

entries[author] = {
"n_times_sent": sum(author_mask),
"date_last_sent": isoformat_last_sent[:10],
"days_since_last_sent": int(days_since_last_sent),
}

stats = pd.DataFrame(entries, dtype=int).T
stats = pd.DataFrame(entries).T

if catalog:

sort_kwargs["by"].append("n_poems")
sort_kwargs["ascending"].append(False)

stats.insert(0, "n_poems", 0)
stats.insert(1, "n_poems", 0)

for author in stats.index:

Expand Down
18 changes: 9 additions & 9 deletions scripts/send-poem.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
if not test:

index = len(history) + 1
now = datetime.now(tz=pytz.utc)
date, time = now.isoformat()[:19].split("T")
now = Context.now()
date, time = now.isoformat[:19].split("T")

history.loc[index, "date"] = date,
history.loc[index, "time"] = time,
history.loc[index, "timestamp"] = int(now.timestamp()),
history.loc[index, "title"] = p.tag,
history.loc[index, "author"] = p.author.tag,
history.loc[index, "date"] = date
history.loc[index, "time"] = time
history.loc[index, "timestamp"] = int(now.timestamp)
history.loc[index, "title"] = p.tag
history.loc[index, "author"] = p.author.tag

daily_poems = {}
for index, entry in history.iterrows():
Expand All @@ -71,7 +71,7 @@
"description": p.html_description,
"body": p.html_body,
"translation": f"Translated by {p.translator}" if p.translator else "",
"spacetime": p.spacetime
"spacetime": p.spacetime,
}

daily_poems[str(index)] = packet
Expand All @@ -82,7 +82,7 @@
utils.write_to_repo(repo,
items={
"data/poems/history-daily.csv": history.to_csv(),
"data/poems/author-stats.csv": utils.make_author_stats(history).to_csv(),
"data/poems/author-stats.csv": utils.make_author_stats(history, catalog=curator.catalog).to_csv(),
"docs/assets/scripts/data/daily-poems.js": f"var dailyPoems = {json.dumps(daily_poems, indent=4, ensure_ascii=False)}",
},
verbose=True)

0 comments on commit af3670c

Please sign in to comment.