diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | docs/feed.rss | 300 | ||||
-rw-r--r-- | docs/index.html | 13 | ||||
-rw-r--r-- | docs/posts/2022-12-25-blog-to-toot.html | 340 | ||||
-rw-r--r-- | docs/posts/index.html | 13 | ||||
-rw-r--r-- | poetry.lock | 147 |
6 files changed, 811 insertions, 3 deletions
@@ -7,3 +7,4 @@ .Trashes ehthumbs.db Thumbs.db +__pycache__/ diff --git a/docs/feed.rss b/docs/feed.rss index 185227c..67e0987 100644 --- a/docs/feed.rss +++ b/docs/feed.rss @@ -4,8 +4,8 @@ <title>Navan's Archive</title> <description>Rare Tips, Tricks and Posts</description> <link>https://web.navan.dev/</link><language>en</language> - <lastBuildDate>Sat, 17 Dec 2022 20:25:31 -0000</lastBuildDate> - <pubDate>Sat, 17 Dec 2022 20:25:31 -0000</pubDate> + <lastBuildDate>Sun, 25 Dec 2022 20:18:51 -0000</lastBuildDate> + <pubDate>Sun, 25 Dec 2022 20:18:51 -0000</pubDate> <ttl>250</ttl> <atom:link href="https://web.navan.dev/feed.rss" rel="self" type="application/rss+xml"/> @@ -3794,6 +3794,302 @@ DescriptionThe bag-of-words model is a simplifying representation used in NLP, i <item> <guid isPermaLink="true"> + https://web.navan.dev/posts/2022-12-25-blog-to-toot.html + </guid> + <title> + Posting blogs as Mastodon Toots + </title> + <description> + Cross posting blog posts to Mastodon + </description> + <link>https://web.navan.dev/posts/2022-12-25-blog-to-toot.html</link> + <pubDate>Sun, 25 Dec 2022 17:32:00 -0000</pubDate> + <content:encoded><![CDATA[<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> +]]></content:encoded> + </item> + + <item> + <guid isPermaLink="true"> https://web.navan.dev/posts/2020-01-14-Converting-between-PIL-NumPy.html </guid> <title> diff --git a/docs/index.html b/docs/index.html index 8a9bb3d..2952918 100644 --- a/docs/index.html +++ b/docs/index.html @@ -47,6 +47,19 @@ <ul> + <li><a href="/posts/2022-12-25-blog-to-toot.html">Posting blogs as Mastodon Toots</a></li> + <ul> + <li>Cross posting blog posts to Mastodon</li> + <li>Published On: 2022-12-25 17:32</li> + <li>Tags: + + Python, + + Mastodon + + </ul> + + <li><a href="/posts/2022-11-07-a-new-method-to-blog.html">A new method to blog</a></li> <ul> <li>Writing posts in markdown using pen and paper</li> diff --git a/docs/posts/2022-12-25-blog-to-toot.html b/docs/posts/2022-12-25-blog-to-toot.html new file mode 100644 index 0000000..297baad --- /dev/null +++ b/docs/posts/2022-12-25-blog-to-toot.html @@ -0,0 +1,340 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + + <link rel="stylesheet" href="/assets/main.css" /> + <link rel="stylesheet" href="/assets/sakura.css" /> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1.0"> + <title>Hey - Post - Posting blogs as Mastodon Toots</title> + <meta name="og:site_name" content="Navan Chauhan" /> + <link rel="canonical" href="https://web.navan.dev/" /> + <meta name="twitter:url" content="https://web.navan.dev/" /> + <meta name="og:url" content="https://web.navan.dev/" /> + <meta name="twitter:title" content="Hey - Post - Posting blogs as Mastodon Toots" /> + <meta name="og:title" content="Hey - Post - 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=" Cross posting blog posts to Mastodon " /> + <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/logo.png" /> + <meta name="og:image" content="https://web.navan.dev/images/logo.png" /> + <link rel="manifest" href="manifest.json" /> + <meta name="google-site-verification" content="LVeSZxz-QskhbEjHxOi7-BM5dDxTg53x2TwrjFxfL0k" /> + <script async src="//gc.zgo.at/count.js" data-goatcounter="https://navanchauhan.goatcounter.com/count"></script> + <script defer data-domain="web.navan.dev" src="https://plausible.io/js/plausible.js"></script> + <script defer data-domain="web.navan.dev" src="https://plausible.navan.dev/js/plausible.js"></script> + +</head> +<body> + <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> + +<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> + + <script data-isso="//comments.navan.dev/" + src="//comments.navan.dev/js/embed.min.js"></script> + <section id="isso-thread"> + <noscript>Javascript needs to be activated to view comments.</noscript> + </section> + <!--<div class="commentbox"></div> + <script src="https://unpkg.com/commentbox.io/dist/commentBox.min.js"></script> + <script>commentBox('5650347917836288-proj')</script>--> +</main> + + +<script src="assets/manup.min.js"></script> +<script src="/pwabuilder-sw-register.js"></script> +</body> +</html>
\ No newline at end of file diff --git a/docs/posts/index.html b/docs/posts/index.html index f4fab83..fd6d5f7 100644 --- a/docs/posts/index.html +++ b/docs/posts/index.html @@ -50,6 +50,19 @@ <ul> + <li><a href="/posts/2022-12-25-blog-to-toot.html">Posting blogs as Mastodon Toots</a></li> + <ul> + <li>Cross posting blog posts to Mastodon</li> + <li>Published On: 2022-12-25 17:32</li> + <li>Tags: + + Python, + + Mastodon, + + </ul> + + <li><a href="/posts/2022-11-07-a-new-method-to-blog.html">A new method to blog</a></li> <ul> <li>Writing posts in markdown using pen and paper</li> diff --git a/poetry.lock b/poetry.lock index 64be20b..3bdd337 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,50 @@ [[package]] +name = "blurhash" +version = "1.1.4" +description = "Pure-Python implementation of the blurhash algorithm." +category = "main" +optional = false +python-versions = "*" + +[package.extras] +test = ["pillow", "numpy", "pytest"] + +[[package]] +name = "certifi" +version = "2022.12.7" +description = "Python package for providing Mozilla's CA Bundle." +category = "main" +optional = false +python-versions = ">=3.6" + +[[package]] +name = "charset-normalizer" +version = "2.1.1" +description = "The Real First Universal Charset Detector. Open, modern and actively maintained alternative to Chardet." +category = "main" +optional = false +python-versions = ">=3.6.0" + +[package.extras] +unicode_backport = ["unicodedata2"] + +[[package]] +name = "decorator" +version = "5.1.1" +description = "Decorators for Humans" +category = "main" +optional = false +python-versions = ">=3.5" + +[[package]] +name = "idna" +version = "3.4" +description = "Internationalized Domain Names in Applications (IDNA)" +category = "main" +optional = false +python-versions = ">=3.5" + +[[package]] name = "jinja2" version = "3.1.2" description = "A very fast and expressive template engine." @@ -34,6 +80,27 @@ optional = false python-versions = ">=3.7" [[package]] +name = "mastodon.py" +version = "1.8.0" +description = "Python wrapper for the Mastodon API" +category = "main" +optional = false +python-versions = "*" + +[package.dependencies] +blurhash = ">=1.1.4" +decorator = ">=4.0.0" +python-dateutil = "*" +python-magic = "*" +requests = ">=2.4.2" +six = "*" + +[package.extras] +blurhash = ["blurhash (>=1.1.4)"] +test = ["pytest", "pytest-runner", "pytest-cov", "vcrpy", "pytest-vcr", "pytest-mock", "requests-mock", "pytz", "http-ece (>=1.0.5)", "cryptography (>=1.6.0)", "blurhash (>=1.1.4)"] +webpush = ["http-ece (>=1.0.5)", "cryptography (>=1.6.0)"] + +[[package]] name = "pygments" version = "2.13.0" description = "Pygments is a syntax highlighting package written in Python." @@ -44,12 +111,78 @@ python-versions = ">=3.6" [package.extras] plugins = ["importlib-metadata"] +[[package]] +name = "python-dateutil" +version = "2.8.2" +description = "Extensions to the standard Python datetime module" +category = "main" +optional = false +python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7" + +[package.dependencies] +six = ">=1.5" + +[[package]] +name = "python-magic" +version = "0.4.27" +description = "File type identification using libmagic" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" + +[[package]] +name = "requests" +version = "2.28.1" +description = "Python HTTP for Humans." +category = "main" +optional = false +python-versions = ">=3.7, <4" + +[package.dependencies] +certifi = ">=2017.4.17" +charset-normalizer = ">=2,<3" +idna = ">=2.5,<4" +urllib3 = ">=1.21.1,<1.27" + +[package.extras] +socks = ["PySocks (>=1.5.6,!=1.5.7)"] +use_chardet_on_py3 = ["chardet (>=3.0.2,<6)"] + +[[package]] +name = "six" +version = "1.16.0" +description = "Python 2 and 3 compatibility utilities" +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*" + +[[package]] +name = "urllib3" +version = "1.26.13" +description = "HTTP library with thread-safe connection pooling, file post, and more." +category = "main" +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*" + +[package.extras] +brotli = ["brotlicffi (>=0.8.0)", "brotli (>=1.0.9)", "brotlipy (>=0.6.0)"] +secure = ["pyOpenSSL (>=0.14)", "cryptography (>=1.3.4)", "idna (>=2.0.0)", "certifi", "urllib3-secure-extra", "ipaddress"] +socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"] + [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "67902615bc43fcd36888f559beb14b25c7b3760ea6b019d4afd5bf06bef1eae2" +content-hash = "e950173022d71e09b7d61b77f1bdcd9034d3da34d8a0fb0092e0b822e64b6096" [metadata.files] +blurhash = [] +certifi = [] +charset-normalizer = [] +decorator = [ + {file = "decorator-5.1.1-py3-none-any.whl", hash = "sha256:b8c3f85900b9dc423225913c5aace94729fe1fa9763b38939a95226f02d37186"}, + {file = "decorator-5.1.1.tar.gz", hash = "sha256:637996211036b6385ef91435e4fae22989472f9d571faba8927ba8253acbc330"}, +] +idna = [] jinja2 = [ {file = "Jinja2-3.1.2-py3-none-any.whl", hash = "sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61"}, {file = "Jinja2-3.1.2.tar.gz", hash = "sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852"}, @@ -97,4 +230,16 @@ markupsafe = [ {file = "MarkupSafe-2.1.1-cp39-cp39-win_amd64.whl", hash = "sha256:46d00d6cfecdde84d40e572d63735ef81423ad31184100411e6e3388d405e247"}, {file = "MarkupSafe-2.1.1.tar.gz", hash = "sha256:7f91197cc9e48f989d12e4e6fbc46495c446636dfc81b9ccf50bb0ec74b91d4b"}, ] +"mastodon.py" = [] pygments = [] +python-dateutil = [ + {file = "python-dateutil-2.8.2.tar.gz", hash = "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86"}, + {file = "python_dateutil-2.8.2-py2.py3-none-any.whl", hash = "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9"}, +] +python-magic = [] +requests = [] +six = [ + {file = "six-1.16.0-py2.py3-none-any.whl", hash = "sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254"}, + {file = "six-1.16.0.tar.gz", hash = "sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926"}, +] +urllib3 = [] |