tumblr-stats/build_queue_stats_model.py

48 lines
1.7 KiB
Python

from dataclasses import dataclass, field
from datetime import datetime
from operator import itemgetter
from typing import Any, Dict, List
from stats_model import StatsModel
@dataclass(kw_only=True)
class BuildQueueStatsModel(StatsModel):
"""Stats model built around calculating stats from your currently queued posts"""
operation: str = 'build_queue_stats'
# Queued posts (both original and not original), sorted in publish order.
ordered_queue: List[Dict[str, Any]] = field(init=False)
def __post_init__(self):
super().__post_init__()
self.most_popular_tags = self.determine_most_popular_tags('post_count')
self.ordered_queue = self.determine_ordered_queue()
def determine_ordered_queue(self) -> List[Dict[str, Any]]:
full_post_map = self.original_post_map | self.unoriginal_post_map
post_list: List[Dict[str, Any]] = []
for post_key in full_post_map:
post = full_post_map[post_key]
if 'scheduled_publish_time' not in post or not post['scheduled_publish_time']:
print('WARNING: Queued post found without publish time. Huh?')
queued_date_time: datetime = datetime.fromtimestamp(
post['scheduled_publish_time'])
post_list.append({
'post_url': post['post_url'],
'tags': post['tags'],
'publish_date_time': queued_date_time
})
# https://stackoverflow.com/a/73050
sorted_list = sorted(post_list, key=itemgetter('publish_date_time'))
# https://stackoverflow.com/a/522578
for i, post in enumerate(sorted_list):
post['queue_order'] = i + 1
return sorted_list