1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
81
82
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://unpkg.com/latex.css/style.min.css" />
<link rel="stylesheet" href="/assets/main.css" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Posting blogs as Mastodon Toots</title>
<meta name="og:site_name" content="Navan Chauhan" />
<link rel="canonical" href="https://web.navan.dev/posts/2022-12-25-blog-to-toot.html" />
<meta name="twitter:url" content="https://web.navan.dev/posts/2022-12-25-blog-to-toot.html />
<meta name="og:url" content="https://web.navan.dev/posts/2022-12-25-blog-to-toot.html" />
<meta name="twitter:title" content="Posting blogs as Mastodon Toots" />
<meta name="og:title" content="Posting blogs as Mastodon Toots" />
<meta name="description" content="Cross posting blog posts to Mastodon" />
<meta name="twitter:description" content="Cross posting blog posts to Mastodon" />
<meta name="og:description" content="Cross posting blog posts to Mastodon" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="/images/favicon.png" type="image/png" />
<link rel="alternate" href="/feed.rss" type="application/rss+xml" title="Subscribe to Navan Chauhan" />
<meta name="twitter:image" content="https://web.navan.dev/images/opengraph/posts/2022-12-25-blog-to-toot.png" />
<meta name="og:image" content="https://web.navan.dev/images/opengraph/posts/2022-12-25-blog-to-toot.png" />
<meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k" />
<script data-goatcounter="https://navanchauhan.goatcounter.com/count"
async src="//gc.zgo.at/count.js"></script>
<script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script>
<link rel="manifest" href="manifest.json" />
</head>
<body>
<center><nav style="display: block;">
|
<a href="/">home</a> |
<a href="/about/">about/links</a> |
<a href="/posts/">posts</a> |
<!--<a href="/publications/">publications</a> |-->
<!--<a href="/repo/">iOS repo</a> |-->
<a href="/feed.rss">RSS Feed</a> |
</nav>
</center>
<main>
<h1>Posting blogs as Mastodon Toots</h1>
<p>What is better than posting a blog post? Posting about your posting pipeline. I did this previously with <a rel="noopener" target="_blank" href="/posts/2021-06-25-Blog2Twitter-P1.html">Twitter</a>. </p>
<h2>the elephant in the room</h2>
<p>mastodon.social does not support any formatting in the status posts.
Yes, there are other instances which have patches to enable features such as markdown formatting, but there is no upstream support.</p>
<h2>time to code</h2>
<p>My website is built using a really simple static site generator I wrote in Python.
Therefore, each post is self-contained in a Markdown file with the necessary metadata.</p>
<p>I am going to specify the path to the blog post, parse it and then publish it.</p>
<p>I initially planned on having a command line parser and some more flags.</p>
<h3>interacting with mastodon</h3>
<p>I ended up using mastodon.py rather than crafting requests by hand. Each status<em>post/toot call returns a status</em>id that can be then used as an in<em>reply</em>to parameter.</p>
<p>For the code snippets, seeing that mastodon does not support native formatting, I am resorting to using ray-so.</p>
<h3>reading markdown</h3>
<p>I am using a bunch of regex hacks, and reading the blog post line by line.
Because there is no markdown support, I append all the links to the end of the toot.
For images, I upload them and attach them to the toot.
The initial toot is generated based off the title and the tags associated with the post.</p>
<div class="codehilite">
<pre><span></span><code><span class="c1"># Regexes I am using</span>
<span class="n">markdown_image</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:!\[(.*?)\]\((.*?)\))'</span>
<span class="n">markdown_links</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:\[(.*?)\]\((.*?)\))'</span>
<span class="n">tags_within_metadata</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"tags: ([\w,\s]+)"</span>
<span class="n">metadata_regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"---\s*\n(.*?)\n---\s*\n"</span>
</code></pre>
</div>
<p>This is useful when I want to get the exact data I want.
In this case, I can extract the tags from the front matter.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">metadata</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">metadata_regex</span><span class="p">,</span> <span class="n">markdown_content</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="k">if</span> <span class="n">metadata</span><span class="p">:</span>
<span class="n">tags_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s2">"tags: ([\w,\s]+)"</span><span class="p">,</span> <span class="n">metadata</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="k">if</span> <span class="n">tags_match</span><span class="p">:</span>
<span class="n">tags</span> <span class="o">=</span> <span class="n">tags_match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">","</span><span class="p">)</span>
</code></pre>
</div>
<h3>code snippet support</h3>
<p>I am running <a rel="noopener" target="_blank" href="https://github.com/akashrchandran/Rayso-API">akashrchandran/Rayso-API</a>.</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
<span class="k">def</span> <span class="nf">get_image</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">language</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"python"</span><span class="p">,</span> <span class="n">title</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"Code Snippet"</span><span class="p">):</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="s1">'code'</span><span class="p">,</span> <span class="n">code</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'language'</span><span class="p">,</span> <span class="n">language</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="n">title</span><span class="p">),</span>
<span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://localhost:3000/api'</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">content</span>
</code></pre>
</div>
<h3>threads! threads! threads!</h3>
<p>Even though mastodon does officially have a higher character limit than Twitter.
I prefer the way threads look.</p>
<h2>result</h2>
<p>Everything does seem to work!
Seeing that you are reading this on Mastodon, and that I have updated this section.</p>
<p><iframe src="https://mastodon.social/@navanchauhan/109577330116812393/embed" class="mastodon-embed" style="max-width: 100%; border: 0" width="400" allowfullscreen="allowfullscreen"></iframe><script src="https://static-cdn.mastodon.social/embed.js" async="async"></script></p>
<h2>what's next?</h2>
<p>Here is the current code:</p>
<div class="codehilite">
<pre><span></span><code><span class="kn">from</span> <span class="nn">mastodon</span> <span class="kn">import</span> <span class="n">Mastodon</span>
<span class="kn">from</span> <span class="nn">mastodon.errors</span> <span class="kn">import</span> <span class="n">MastodonAPIError</span>
<span class="kn">import</span> <span class="nn">requests</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="n">mastodon</span> <span class="o">=</span> <span class="n">Mastodon</span><span class="p">(</span>
<span class="n">access_token</span><span class="o">=</span><span class="s1">'reeeeee'</span><span class="p">,</span>
<span class="n">api_base_url</span><span class="o">=</span><span class="s2">"https://mastodon.social"</span>
<span class="p">)</span>
<span class="n">url_base</span> <span class="o">=</span> <span class="s2">"https://web.navan.dev"</span>
<span class="n">sample_markdown_file</span> <span class="o">=</span> <span class="s2">"Content/posts/2022-12-25-blog-to-toot.md"</span>
<span class="n">tags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">toots</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">image_idx</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">markdown_image</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:!\[(.*?)\]\((.*?)\))'</span>
<span class="n">markdown_links</span> <span class="o">=</span> <span class="sa">r</span><span class="s1">'(?:\[(.*?)\]\((.*?)\))'</span>
<span class="k">def</span> <span class="nf">get_image</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">language</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"python"</span><span class="p">,</span> <span class="n">title</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">"Code Snippet"</span><span class="p">):</span>
<span class="n">params</span> <span class="o">=</span> <span class="p">(</span>
<span class="p">(</span><span class="s1">'code'</span><span class="p">,</span> <span class="n">code</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'language'</span><span class="p">,</span> <span class="n">language</span><span class="p">),</span>
<span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="n">title</span><span class="p">),</span>
<span class="p">)</span>
<span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'http://localhost:3000/api'</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
<span class="k">return</span> <span class="n">response</span><span class="o">.</span><span class="n">content</span>
<span class="k">class</span> <span class="nc">TootContent</span><span class="p">:</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">:</span> <span class="nb">str</span> <span class="o">=</span> <span class="s2">""</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">=</span> <span class="n">text</span>
<span class="bp">self</span><span class="o">.</span><span class="n">images</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">links</span> <span class="o">=</span> <span class="p">[]</span>
<span class="bp">self</span><span class="o">.</span><span class="n">image_count</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">images</span><span class="p">)</span>
<span class="k">def</span> <span class="fm">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">toot_text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">text</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
<span class="n">toot_text</span> <span class="o">+=</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">link</span>
<span class="k">return</span> <span class="n">toot_text</span>
<span class="k">def</span> <span class="nf">get_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">toot_text</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">text</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
<span class="n">toot_text</span> <span class="o">+=</span> <span class="s2">" "</span> <span class="o">+</span> <span class="n">link</span>
<span class="k">return</span> <span class="n">toot_text</span>
<span class="k">def</span> <span class="nf">get_length</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="n">length</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span><span class="p">)</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">:</span>
<span class="n">length</span> <span class="o">+=</span> <span class="mi">23</span>
<span class="k">return</span> <span class="n">length</span>
<span class="k">def</span> <span class="nf">add_link</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">link</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span><span class="p">)</span> <span class="o">+</span> <span class="mi">23</span> <span class="o"><</span> <span class="mi">498</span><span class="p">:</span>
<span class="k">if</span> <span class="n">link</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">'h'</span><span class="p">:</span>
<span class="n">link</span> <span class="o">=</span> <span class="n">url_base</span> <span class="o">+</span> <span class="n">link</span>
<span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">link</span><span class="p">)</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">def</span> <span class="nf">add_image</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">image</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span> <span class="o">==</span> <span class="mi">4</span><span class="p">:</span>
<span class="c1"># will handle in future</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"cannot upload more than 4 images per toot"</span><span class="p">)</span>
<span class="n">exit</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># upload image and get id</span>
<span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">image_count</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">images</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">add_text</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">+</span> <span class="n">text</span><span class="p">)</span> <span class="o">></span> <span class="mi">400</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">else</span><span class="p">:</span>
<span class="bp">self</span><span class="o">.</span><span class="n">text</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">" </span><span class="si">{</span><span class="n">text</span><span class="si">}</span><span class="s2">"</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="k">def</span> <span class="nf">get_links</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">links</span><span class="p">))</span>
<span class="n">in_metadata</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">in_code_block</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">my_toots</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">text</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">images</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">image_links</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">extra_links</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">tags</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">code_block</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">language</span> <span class="o">=</span> <span class="s2">"bash"</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">()</span>
<span class="n">metadata_regex</span> <span class="o">=</span> <span class="sa">r</span><span class="s2">"---\s*\n(.*?)\n---\s*\n"</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">sample_markdown_file</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">markdown_content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">metadata</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">metadata_regex</span><span class="p">,</span> <span class="n">markdown_content</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="k">if</span> <span class="n">metadata</span><span class="p">:</span>
<span class="n">tags_match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="sa">r</span><span class="s2">"tags: ([\w,\s]+)"</span><span class="p">,</span> <span class="n">metadata</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="k">if</span> <span class="n">tags_match</span><span class="p">:</span>
<span class="n">tags</span> <span class="o">=</span> <span class="n">tags_match</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">","</span><span class="p">)</span>
<span class="n">markdown_content</span> <span class="o">=</span> <span class="n">markdown_content</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s2">"---</span><span class="se">\n</span><span class="s2">"</span><span class="p">,</span><span class="mi">1</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">markdown_content</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">):</span>
<span class="k">if</span> <span class="n">current_toot</span><span class="o">.</span><span class="n">get_length</span><span class="p">()</span> <span class="o"><</span> <span class="mi">400</span><span class="p">:</span>
<span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s1">''</span><span class="p">:</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="n">line</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s1">'#'</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"#"</span><span class="p">,</span><span class="s2">""</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">my_toots</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">current_toot</span><span class="o">.</span><span class="n">add_text</span><span class="p">(</span>
<span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">line</span><span class="si">}</span><span class="s2">: a cross-posted blog post </span><span class="se">\n</span><span class="s2">"</span>
<span class="p">)</span>
<span class="n">hashtags</span> <span class="o">=</span> <span class="s2">""</span>
<span class="k">for</span> <span class="n">tag</span> <span class="ow">in</span> <span class="n">tags</span><span class="p">:</span>
<span class="n">hashtags</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">"#</span><span class="si">{</span><span class="n">tag</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="si">}</span><span class="s2">,"</span>
<span class="n">current_toot</span><span class="o">.</span><span class="n">add_text</span><span class="p">(</span><span class="n">hashtags</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">()</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">(</span><span class="n">text</span><span class="o">=</span><span class="sa">f</span><span class="s2">"</span><span class="si">{</span><span class="n">line</span><span class="o">.</span><span class="n">title</span><span class="p">()</span><span class="si">}</span><span class="s2">:"</span><span class="p">)</span>
<span class="k">continue</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">if</span> <span class="s2">"```"</span> <span class="ow">in</span> <span class="n">line</span><span class="p">:</span>
<span class="n">in_code_block</span> <span class="o">=</span> <span class="ow">not</span> <span class="n">in_code_block</span>
<span class="k">if</span> <span class="n">in_code_block</span><span class="p">:</span>
<span class="n">language</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">"```"</span><span class="p">,</span><span class="s1">''</span><span class="p">)</span>
<span class="k">continue</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="sa">f</span><span class="s2">"code-snipped_</span><span class="si">{</span><span class="n">image_idx</span><span class="si">}</span><span class="s2">.png"</span><span class="p">,</span><span class="s2">"wb"</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">get_image</span><span class="p">(</span><span class="n">code_block</span><span class="p">,</span> <span class="n">language</span><span class="p">))</span>
<span class="n">current_toot</span><span class="o">.</span><span class="n">add_image</span><span class="p">(</span><span class="sa">f</span><span class="s2">"code-snipped_</span><span class="si">{</span><span class="n">image_idx</span><span class="si">}</span><span class="s2">.png"</span><span class="p">)</span>
<span class="n">image_idx</span> <span class="o">+=</span> <span class="mi">1</span>
<span class="n">code_block</span> <span class="o">=</span> <span class="s2">""</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="n">in_code_block</span><span class="p">:</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s2">" "</span><span class="p">,</span><span class="s2">"</span><span class="se">\t</span><span class="s2">"</span><span class="p">)</span>
<span class="n">code_block</span> <span class="o">+=</span> <span class="n">line</span> <span class="o">+</span> <span class="s2">"</span><span class="se">\n</span><span class="s2">"</span>
<span class="k">continue</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_image</span><span class="p">,</span><span class="n">line</span><span class="p">))</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">for</span> <span class="n">image_link</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_links</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="n">image_link</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image_link</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># not handled yet</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">markdown_image</span><span class="p">,</span><span class="s2">""</span><span class="p">,</span><span class="n">line</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_links</span><span class="p">,</span><span class="n">line</span><span class="p">))</span> <span class="o">></span> <span class="mi">0</span><span class="p">:</span>
<span class="k">for</span> <span class="n">link</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="n">markdown_links</span><span class="p">,</span> <span class="n">line</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">current_toot</span><span class="o">.</span><span class="n">add_link</span><span class="p">(</span><span class="n">link</span><span class="p">[</span><span class="mi">1</span><span class="p">])):</span>
<span class="n">extra_links</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">link</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="sa">f</span><span class="s1">'[</span><span class="si">{</span><span class="n">link</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">}</span><span class="s1">](</span><span class="si">{</span><span class="n">link</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="si">}</span><span class="s1">)'</span><span class="p">,</span><span class="n">link</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">current_toot</span><span class="o">.</span><span class="n">add_text</span><span class="p">(</span><span class="n">line</span><span class="p">):</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">current_toot</span> <span class="o">=</span> <span class="n">TootContent</span><span class="p">()</span>
<span class="n">my_toots</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">current_toot</span><span class="p">)</span>
<span class="n">in_reply_to_id</span> <span class="o">=</span> <span class="kc">None</span>
<span class="k">for</span> <span class="n">toot</span> <span class="ow">in</span> <span class="n">my_toots</span><span class="p">:</span>
<span class="n">image_ids</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">image</span> <span class="ow">in</span> <span class="n">toot</span><span class="o">.</span><span class="n">images</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s2">"uploading image, </span><span class="si">{</span><span class="n">image</span><span class="si">}</span><span class="s2">"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">image_id</span> <span class="o">=</span> <span class="n">mastodon</span><span class="o">.</span><span class="n">media_post</span><span class="p">(</span><span class="n">image</span><span class="p">)</span>
<span class="n">image_ids</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">image_id</span><span class="o">.</span><span class="n">id</span><span class="p">)</span>
<span class="k">except</span> <span class="n">MastodonAPIError</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"failed to upload. Continuing..."</span><span class="p">)</span>
<span class="k">if</span> <span class="n">image_ids</span> <span class="o">==</span> <span class="p">[]:</span>
<span class="n">image_ids</span> <span class="o">=</span> <span class="kc">None</span>
<span class="n">in_reply_to_id</span> <span class="o">=</span> <span class="n">mastodon</span><span class="o">.</span><span class="n">status_post</span><span class="p">(</span>
<span class="n">toot</span><span class="o">.</span><span class="n">get_text</span><span class="p">(),</span> <span class="n">in_reply_to_id</span><span class="o">=</span><span class="n">in_reply_to_id</span><span class="p">,</span> <span class="n">media_ids</span><span class="o">=</span><span class="n">image_ids</span>
<span class="p">)</span><span class="o">.</span><span class="n">id</span>
</code></pre>
</div>
<p>Not the best thing I have ever written, but it works!</p>
<blockquote>If you have scrolled this far, consider subscribing to my mailing list <a href="https://listmonk.navan.dev/subscription/form">here.</a> You can subscribe to either a specific type of post you are interested in, or subscribe to everything with the "Everything" list.</blockquote>
<script data-isso="https://comments.navan.dev/"
src="https://comments.navan.dev/js/embed.min.js"></script>
<section id="isso-thread">
<noscript>Javascript needs to be activated to view comments.</noscript>
</section>
</main>
<script src="assets/manup.min.js"></script>
<script src="/pwabuilder-sw-register.js"></script>
</body>
</html>
|