Module for writing out a Hugo-compatible blog post in Markdown. Reads two JSON dumps, one
written by fetch and one written by analyse. Then it writes out the blog post. Since writing
out the fetch JSON was only created in October 2025, the code handles the possibility of it
not existing.
blog(config, copy_files=False, additional_tags=None)
Do what it takes to find the results, convert them to markdown, then
write it out in Hugo-compatible Markdown. Hugo compatibility means
putting some extra TOML variables at the item. It's not just raw markdown.
Source code in mastoscore/blog.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 | def blog(config: ConfigParser, copy_files: bool = False, additional_tags: list = None) -> bool:
"""Do what it takes to find the results, convert them to markdown, then
write it out in Hugo-compatible Markdown. Hugo compatibility means
putting some extra TOML variables at the item. It's not just raw markdown.
"""
logger = logging.getLogger(__name__)
if additional_tags is None:
additional_tags = []
analysis = read_json(config, "analysis")
if not analysis:
logger.error("Problem with analysis file? No blog written.")
return False
# Create directory structure
dir_path = create_journal_directory(config)
if not dir_path:
return False
blog_text = create_blog_text(config, analysis, additional_tags)
# Blog file is index.md
blog_filename = join(dir_path, "index.md")
try:
with open(blog_filename, "+w") as bfile:
print(blog_text, file=bfile)
except (OSError, IOError) as e:
logger.critical(f"Failed to write blog text to {blog_filename}")
logger.critical(e)
return False
except Exception as e:
logger.critical(f"Failed to write blog text to {blog_filename}")
logger.critical(e)
return False
# If copy_files is True, copy files to blog gallery
if copy_files:
try:
hashtag = config.get('mastoscore', 'hashtag')
year = config.get('mastoscore', 'event_year')
month = config.get('mastoscore', 'event_month')
day = config.get('mastoscore', 'event_day')
dest_dir = f"../{hashtag}-gallery/content/{year}/{month}/{day}/{hashtag}/"
os.makedirs(dest_dir, exist_ok=True)
source_dir = f"data/{year}/{month}/{day}/"
for filename in os.listdir(source_dir):
if not filename.endswith('.json'):
src = join(source_dir, filename)
dst = join(dest_dir, filename)
shutil.copy2(src, dst)
logger.info(f"Files copied to {dest_dir}")
except Exception as e:
logger.error(f"Failed to copy files: {e}")
return False
return True
|
create_blog_text(config, analysis, additional_tags=None)
Write out the Markdown for the blog post with all the analysis.
Source code in mastoscore/blog.py
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309 | def create_blog_text(config: ConfigParser, analysis: dict, additional_tags: list = None) -> str:
"""Write out the Markdown for the blog post with all the analysis."""
# pick a language, so that things like currency and numbers come out
# as expected
setlocale(LC_ALL, "en_US.UTF-8")
logger = logging.getLogger(__name__)
event_title = None
if additional_tags is None:
additional_tags = []
year = config.get("mastoscore", "event_year")
month = config.get("mastoscore", "event_month")
day = config.get("mastoscore", "event_day")
hashtag = config.get("mastoscore", "hashtag")
# older data files had event_title, but not episode_title
try:
event_title = config.get("mastoscore", "episode_title")
except Exception:
logger.debug("episode_title not found. Trying event_title")
if not event_title:
try:
event_title = config.get("mastoscore", "event_title")
except Exception:
logger.error("neither episode_title nor event_title found")
event_title = f"{hashtag} {year}-{month}-{day}"
# Find our journal directory so we can see if there are text files
# accompanying the images.
journaldir = create_journal_directory(config)
if not journaldir:
return ""
text_file_name = join(journaldir, f"{hashtag}-{year}{month}{day}.txt")
graph_alt_text = ""
with open(text_file_name, "r", encoding="utf-8") as tfile:
graph_alt_text = tfile.read()
text_file_name = join(
journaldir, f"wordcloud-{hashtag}-{year}{month}{day}-remove.txt"
)
wordcloud_alt_text = ""
with open(text_file_name, "r", encoding="utf-8") as tfile:
wordcloud_alt_text = tfile.read()
# These bits of hugo markdown are a pain to type, so I'm assigning them to variables to make them
# easier to work with.
a = "{{< hilite >}}"
b = "{{</ hilite >}}"
# write out top boosts
top = analysis["max_boosts"]
text = "\n\n"
text = text + f"### The top {analysis['top_n']} boosted toots:\n"
i = 1
for item in top:
text = (
text
+ f"""
{i}. [This toot]({item["url"]}) from [{item["account.display_name"]}]({item["account.url"]})
({item["userid"]}) had {a}{item["reblogs_count"]}{b} boosts.
> {item["content"]}
"""
)
i += 1
boost_text = text
# write out item favourites
top = analysis["max_faves"]
text = "\n\n"
text = text + f"### The top {analysis['top_n']} favourited toots:\n"
i = 1
for item in top:
text = (
text
+ f"""
{i}. [This toot]({item["url"]}) from [{item["account.display_name"]}]({item["account.url"]})
({item["userid"]}) had {a}{item["favourites_count"]}{b} favourites.
> {item["content"]}
"""
)
i += 1
text = text + "\n"
faves_text = text
# write out item replies
top = analysis["max_replies"]
text = "\n\n"
text = (
text
+ f"""### The top {analysis["top_n"]} most-replied-to toots:
(Note: this really should exclude people who reply to themselves, it doesn't do that well, yet)
"""
)
i = 1
for item in top:
text = (
text
+ f"""
{i}. [This toot]({item["url"]}) from [{item["account.display_name"]}]({item["account.url"]})
({item["userid"]}) had {a}{item["replies_count"]}{b} replies.
> {item["content"]}
"""
)
i += 1
replies_text = text
try:
fetch_info = read_json(config, "fetch")
total_servers = len(fetch_info["servers_done"]) + len(
fetch_info["servers_fail"]
)
done_servers = len(fetch_info["servers_done"])
fail_servers = len(fetch_info["servers_fail"])
done_pct = 100 * done_servers / total_servers
fail_pct = 100 * fail_servers / total_servers
intro = "We"
if 'fetch_duration' in fetch_info:
intro = f"{intro} spent {a}{fetch_info['fetch_duration']}{b} downloading"
else:
intro = f"{intro} downloaded"
fetch_text = f"""
{intro} {a}{fetch_info["total_toots"]:n}{b} toots at {a}{fetch_info["fetch_time"]}{b} \
from a total of {done_servers:n} servers. We ended up with a set of \
{a}{analysis["gross_toots"]:n}{b} unique toots posted between \
{a}{analysis["event_start"]}{b} and {a}{analysis["event_end"]}{b} by \
{a}{analysis["unique_ids"]:n}{b} distinct fediverse accounts.
We tried to connect to {total_servers:n} different servers, of which {done_servers} \
({done_pct:0.1f}%) succeeded and {fail_servers} ({fail_pct:0.1f}%) failed.
"""
except Exception:
# we don't care there was an exception. just using this for error handling
fetch_text = f"""
We found {a}{analysis["gross_toots"]:n}{b} unique toots posted between \
{a}{analysis["event_start"]}{b} and {a}{analysis["event_end"]}{b} by \
{a}{analysis["unique_ids"]:n}{b} distinct fediverse accounts across \
{analysis["num_servers"]} unique servers.
"""
summary_text = f"""
## Summary
{{{{< figure\
src="thumb.jpg"\
alt="(Movie poster placeholder)" \
>}}}}
{fetch_text}
The server {a}[{analysis["max_server"]["name"]}](https://{analysis["max_server"]["name"]}/){b}\
contributed the most toots with {a}{analysis["max_server"]["num"]:n}{b}.
'{analysis["most_posts"]["name"]}' ({analysis["most_posts"]["id"]}) tooted the most with \
{a}{analysis["most_posts"]["count"]:n}{b} toots.
"""
# Try to pluck the year of the film from the title. Film titles are usually something like
# "The Swamp Thing (1982)"
match = search(r"\((\d{4})\)", event_title)
blog_tags = [hashtag]
if match:
film_year = match.group(1)
blog_tags.append(film_year)
# Add additional tags from TMDb keywords or user input
blog_tags.extend(additional_tags)
# Format as TOML array
blog_tags_str = '[' + ', '.join(f'"{tag}"' for tag in blog_tags) + ']'
graph_text = f"""
## Activity Graph
{{{{< figure\
src="{hashtag}-{year}{month}{day}.png"\
alt="Activity graph. See main page for actual description." \
>}}}}
{graph_alt_text}
"""
wordcloud_text = f"""
## Wordcloud
{{{{< figure\
src="wordcloud-{hashtag}-{year}{month}{day}-remove.png"\
alt="Word cloud. See main page for actual description." \
>}}}}
{wordcloud_alt_text}
"""
# Pull it all together, with the Hugo stuff at the item
# TOML requires triple double quotes if you want to have newlines in a string.
# So I have to do triple single quotes here.
wordcloud_file = f"{year}/{month}/{day}/{hashtag}/wordcloud-{hashtag}-{year}{month}{day}-remove.png"
blog_text = f'''
+++
title = """{event_title}"""
date = {year}-{month}-{day}
draft = false
image = "{wordcloud_file}"
description = """{hashtag} graphs for {event_title}"""
tags = {blog_tags_str}
archives = ["{year}/{month}"]
author = "Paco Hope"
+++
{summary_text}
{boost_text}
{faves_text}
{replies_text}
{graph_text}
{wordcloud_text}
Analysis of #{hashtag} generated at {analysis["generated"]}
'''
# print out the commands that will copy the data to the blog
print(f"""
mkdir -p ../{hashtag}-gallery/content/{year}/{month}/{day}/{hashtag}/
cp $(ls data/{year}/{month}/{day}/* | grep -v json) ../{hashtag}-gallery/content/{year}/{month}/{day}/{hashtag}
""")
return blog_text
|