Making the outputs a bit more organized, adding some more stats to them

This commit is contained in:
Amber McCloughan 2025-12-30 19:52:34 -05:00
parent 9a8144af68
commit 6e32e3ec54
4 changed files with 39 additions and 17 deletions

View File

@ -1,6 +1,6 @@
from dataclasses import dataclass, field
from operator import itemgetter
from typing import List
from typing import Any, Dict, List
from stats_model import StatsModel
@ -11,13 +11,26 @@ class BuildTagStatsModel(StatsModel):
operation: str = 'build_tag_stats'
# Posts ranked from most popular to least popular by notes.
ranked_post_urls: List[str] = field(init=False)
ranked_posts: List[Dict[str, Any]] = field(init=False)
def __post_init__(self):
super().__post_init__()
self.ranked_post_urls = self.determine_ranked_post_urls()
self.ranked_posts = self.determine_ranked_posts()
def determine_ranked_post_urls(self) -> List[str]:
post_list = sorted(list(self.original_post_map.values()),
key=itemgetter('note_count'), reverse=True)
return [post['post_url'] for post in post_list]
def determine_ranked_posts(self) -> List[Dict[str, Any]]:
post_list: List[Dict[str, Any]] = []
for post_key in self.original_post_map:
post = self.original_post_map[post_key]
post_list.append({
'id_string': post['id_string'],
'post_url': post['post_url'],
'tags': post['tags'],
'note_count': post['note_count']
})
sorted_list = sorted(post_list, key=itemgetter('note_count'),
reverse=True)
for i, post in enumerate(sorted_list):
post['rank'] = i + 1
return sorted_list

View File

@ -1,6 +1,5 @@
from dataclasses import dataclass, field
from operator import itemgetter
from typing import Any, Dict, List
from typing import Dict, List
from stats_model import StatsModel
@ -17,4 +16,4 @@ class BuildTotalStatsModel(StatsModel):
top_post_urls_by_month_and_year: Dict[str, List[str]] = field(init=False)
def __post_init__(self):
super().__post_init__()
super().__post_init__()

View File

@ -59,22 +59,32 @@ class StatsModel:
return total
def calculate_total_original_post_notes_by_month_and_year(self) -> Dict[str, int]:
date_map: Dict[str, int] = {}
date_map: Dict[str, Any] = {}
date_map = defaultdict(lambda: {'note_count': 0,
'post_count': 0},
date_map)
for post_key in self.original_post_map:
post = self.original_post_map[post_key]
# Format is like 2025-12-28 20:00:34 GMT
post_date: datetime = datetime.strptime(
post['date'], '%Y-%m-%d %H:%M:%S %Z')
post_date_key = f"{post_date.year}-{post_date.month:02}"
if post_date_key in date_map:
date_map[post_date_key] += post['note_count']
else:
date_map[post_date_key] = post['note_count']
sts = date_map[post_date_key]
sts['year_month'] = post_date_key
sts['post_count'] += 1
sts['note_count'] += post['note_count']
for date in date_map:
sts = date_map[date]
post_count = sts['post_count']
note_count = sts['note_count']
sts['notes_to_posts_ratio'] = note_count / post_count
return date_map
def determine_most_popular_tags(self) -> List[Dict[str, Any]]:
tag_dict: Dict[str, Any] = {}
tag_dict = defaultdict(lambda : {'note_count': 0,
tag_dict = defaultdict(lambda: {'note_count': 0,
'post_count': 0},
tag_dict)
for post_key in self.original_post_map:

View File

@ -182,7 +182,7 @@ def main() -> None:
# Write the chosen model as JSON output.
with open('./tumblr_stats.json', 'w') as f:
json.dump(asdict(stats_model), f, indent=1)
json.dump(asdict(stats_model), f, indent=1, default=str)
# If there were original posts, create a CSV for them.
if post_map: