68 lines
2.4 KiB
Python
68 lines
2.4 KiB
Python
from dataclasses import dataclass, field
|
|
from datetime import datetime
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
@dataclass
|
|
class StatsModel:
|
|
"""Class that models the output of the Tumblr stats script."""
|
|
# The operation that was used to output stats.
|
|
operation: str
|
|
|
|
# The blog in question.
|
|
blog_name: str
|
|
|
|
# Contains original posts, indexed by post ID.
|
|
original_post_map: Dict[str, Any]
|
|
|
|
# Contains posts that are not original, indexed by post ID.
|
|
unoriginal_post_map: Dict[str, Any]
|
|
|
|
# Any tags used.
|
|
tags: List[str] = field(default_factory=list)
|
|
|
|
# Total count of posts processed.
|
|
total_posts: int = field(init=False)
|
|
|
|
# Total original posts (for blog_name) processed.
|
|
total_original_posts: int = field(init=False)
|
|
|
|
# Total original post (for blog_name) notes processed.
|
|
total_original_post_notes: int = field(init=False)
|
|
|
|
# Total notes for original posts within each month and year.
|
|
total_original_post_notes_by_month_and_year: Dict[str, int] = field(
|
|
init=False)
|
|
|
|
def __post_init__(self):
|
|
self.total_posts = self.calculate_total_posts()
|
|
self.total_original_posts = self.calculate_total_original_posts()
|
|
self.total_original_post_notes = self.calculate_total_original_post_notes()
|
|
self.total_original_post_notes_by_month_and_year = self.calculate_total_original_post_notes_by_month_and_year()
|
|
|
|
def calculate_total_posts(self) -> int:
|
|
return len(self.original_post_map) + len(self.unoriginal_post_map)
|
|
|
|
def calculate_total_original_posts(self) -> int:
|
|
return len(self.original_post_map)
|
|
|
|
def calculate_total_original_post_notes(self) -> int:
|
|
total = 0
|
|
for post_key in self.original_post_map:
|
|
total += self.original_post_map[post_key]['note_count']
|
|
return total
|
|
|
|
def calculate_total_original_post_notes_by_month_and_year(self) -> Dict[str, int]:
|
|
date_map: Dict[str, int] = {}
|
|
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']
|
|
return date_map
|