summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavan Chauhan <navanchauhan@gmail.com>2021-06-25 02:25:52 +0530
committerNavan Chauhan <navanchauhan@gmail.com>2021-06-25 02:25:52 +0530
commit50ae0baa49c0b58d13eaeb6618d0920c7315a481 (patch)
tree274833c0a511ab60e80f8d06b6fad765155d8593
parenteced1cf2687682e9c0216d9fc92c1d05990f8d5a (diff)
updated
-rw-r--r--docs/feed.rss107
-rw-r--r--docs/index.html15
-rw-r--r--docs/posts/index.html15
3 files changed, 135 insertions, 2 deletions
diff --git a/docs/feed.rss b/docs/feed.rss
index 4b21037..ec767e7 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>Thu, 27 May 2021 00:22:21 -0000</lastBuildDate>
- <pubDate>Thu, 27 May 2021 00:22:21 -0000</pubDate>
+ <lastBuildDate>Fri, 25 Jun 2021 02:25:20 -0000</lastBuildDate>
+ <pubDate>Fri, 25 Jun 2021 02:25:20 -0000</pubDate>
<ttl>250</ttl>
<atom:link href="https://web.navan.dev/feed.rss" rel="self" type="application/rss+xml"/>
@@ -57,6 +57,109 @@
<item>
<guid isPermaLink="true">
+ https://web.navan.dev/posts/2021-06-25-Blog2Twitter-P1.html
+ </guid>
+ <title>
+ Posting Blog Posts as Twitter Threads Part 1/n
+ </title>
+ <description>
+ Converting Posts to Twitter Threads
+ </description>
+ <link>https://web.navan.dev/posts/2021-06-25-Blog2Twitter-P1.html</link>
+ <pubDate>Fri, 25 Jun 2021 00:08:00 -0000</pubDate>
+ <content:encoded><![CDATA[<h1>Posting Blog Posts as Twitter Threads Part 1/n</h1>
+
+<p>Why? Eh, no good reason, but should be fun.</p>
+
+<h2>Plan of Action</h2>
+
+<p>I recently shifted my website to a static site generator I wrote specifically for myself.
+Thus, it should be easy to just add a feature into it to check for new posts, split the text into chunks for Twitter threads and post them on Twitter.
+I am not handling lists or images right now.</p>
+
+<h2>Time to Code</h2>
+
+<p>First, the dependency: tweepy for tweeting.</p>
+
+<p><code>pip install tweepy</code></p>
+
+<div class="codehilite"><pre><span></span><code><span class="kn">import</span> <span class="nn">os</span>
+<span class="kn">import</span> <span class="nn">tweepy</span>
+
+<span class="n">consumer_key</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;consumer_key&quot;</span><span class="p">]</span>
+<span class="n">consumer_secret</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;consumer_secret&quot;</span><span class="p">]</span>
+
+<span class="n">access_token</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;access_token&quot;</span><span class="p">]</span>
+<span class="n">access_token_secret</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="p">[</span><span class="s2">&quot;access_token_secret&quot;</span><span class="p">]</span>
+
+<span class="n">auth</span> <span class="o">=</span> <span class="n">tweepy</span><span class="o">.</span><span class="n">OAuthHandler</span><span class="p">(</span><span class="n">consumer_key</span><span class="p">,</span> <span class="n">consumer_secret</span><span class="p">)</span>
+<span class="n">auth</span><span class="o">.</span><span class="n">set_access_token</span><span class="p">(</span><span class="n">access_token</span><span class="p">,</span> <span class="n">access_token_secret</span><span class="p">)</span>
+
+<span class="n">api</span> <span class="o">=</span> <span class="n">tweepy</span><span class="o">.</span><span class="n">API</span><span class="p">(</span><span class="n">auth</span><span class="p">)</span>
+</code></pre></div>
+
+<p>The program need to convert the blog post into text fragments.</p>
+
+<p>It reads the markdown file, removes the top YAML content, checks for headers and splits the content.</p>
+
+<div class="codehilite"><pre><span></span><code><span class="n">tweets</span> <span class="o">=</span> <span class="p">[]</span>
+
+<span class="n">first___n</span> <span class="o">=</span> <span class="mi">0</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="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">():</span>
+ <span class="k">if</span> <span class="n">first___n</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>
+ <span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s2">&quot;---</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">:</span>
+ <span class="n">first___n</span> <span class="o">+=</span> <span class="mi">1</span>
+ <span class="k">continue</span>
+ <span class="n">line</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="n">line</span> <span class="o">+=</span> <span class="s2">&quot; &quot;</span>
+ <span class="k">if</span> <span class="s2">&quot;#&quot;</span> <span class="ow">in</span> <span class="n">line</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">&quot;#&quot;</span><span class="p">,</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
+ <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
+ <span class="n">line</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">line</span>
+ <span class="n">line</span> <span class="o">+=</span> <span class="s2">&quot;</span><span class="se">\n\n</span><span class="s2">&quot;</span>
+ <span class="k">try</span><span class="p">:</span>
+ <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">&lt;</span> <span class="mi">260</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">))</span> <span class="o">&lt;=</span> <span class="mi">260</span><span class="p">:</span>
+ <span class="n">tweets</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">line</span>
+ <span class="k">else</span><span class="p">:</span>
+ <span class="n">tweets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
+ <span class="k">except</span> <span class="ne">IndexError</span><span class="p">:</span>
+ <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">line</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">260</span><span class="p">:</span>
+ <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;ERROR&quot;</span><span class="p">)</span>
+ <span class="k">else</span><span class="p">:</span>
+ <span class="n">tweets</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
+</code></pre></div>
+
+<p>Every status update using tweepy has an id attached to it, for the next tweet in the thread, it add that ID while calling the function.</p>
+
+<p>For every tweet fragment, it also append 1/n.</p>
+
+<div class="codehilite"><pre><span></span><code><span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">tweet</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">tweets</span><span class="p">):</span>
+ <span class="n">tweet</span> <span class="o">+=</span> <span class="s2">&quot; </span><span class="si">{}</span><span class="s2">/</span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">))</span>
+ <span class="k">if</span> <span class="n">idx</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
+ <span class="n">a</span> <span class="o">=</span> <span class="kc">None</span>
+ <span class="n">a</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">update_status</span><span class="p">(</span><span class="n">tweet</span><span class="p">)</span>
+ <span class="k">else</span><span class="p">:</span>
+ <span class="n">a</span> <span class="o">=</span> <span class="n">api</span><span class="o">.</span><span class="n">update_status</span><span class="p">(</span><span class="n">tweet</span><span class="p">,</span><span class="n">in_reply_to_status_id</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">id</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="n">tweet</span><span class="p">),</span><span class="n">end</span><span class="o">=</span><span class="s2">&quot; &quot;</span><span class="p">)</span>
+ <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">{}</span><span class="s2">/</span><span class="si">{}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">idx</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="nb">len</span><span class="p">(</span><span class="n">tweets</span><span class="p">)))</span>
+</code></pre></div>
+
+<p>Finally, it replies to the last tweet in the thread with the link of the post.</p>
+
+<div class="codehilite"><pre><span></span><code><span class="n">api</span><span class="o">.</span><span class="n">update_status</span><span class="p">(</span><span class="s2">&quot;Web Version: </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">post_link</span><span class="p">))</span>
+</code></pre></div>
+
+<h2>What's Next?</h2>
+
+<p>For the next part, I will try to append the code as well.
+I actually added the code to this post after running the program.</p>
+]]></content:encoded>
+ </item>
+
+ <item>
+ <guid isPermaLink="true">
https://web.navan.dev/posts/hello-world.html
</guid>
<title>
diff --git a/docs/index.html b/docs/index.html
index 08209b8..f25234a 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -27,6 +27,21 @@
<ul>
+ <li><a href="/posts/2021-06-25-Blog2Twitter-P1.html">Posting Blog Posts as Twitter Threads Part 1/n</a></li>
+ <ul>
+ <li>Converting Posts to Twitter Threads</li>
+ <li>Published On: 2021-06-25 00:08</li>
+ <li>Tags:
+
+ Python,
+
+ Twitter,
+
+ Eh,
+
+ </ul>
+
+
<li><a href="/posts/2020-12-1-HTML-JS-RSS-Feed.html">RSS Feed written in HTML + JavaScript</a></li>
<ul>
<li>Short code-snippet for an RSS feed, written in HTML and JavaScript</li>
diff --git a/docs/posts/index.html b/docs/posts/index.html
index b94dd4d..4d31a82 100644
--- a/docs/posts/index.html
+++ b/docs/posts/index.html
@@ -30,6 +30,21 @@
<ul>
+ <li><a href="/posts/2021-06-25-Blog2Twitter-P1.html">Posting Blog Posts as Twitter Threads Part 1/n</a></li>
+ <ul>
+ <li>Converting Posts to Twitter Threads</li>
+ <li>Published On: 2021-06-25 00:08</li>
+ <li>Tags:
+
+ Python,
+
+ Twitter,
+
+ Eh,
+
+ </ul>
+
+
<li><a href="/posts/2020-12-1-HTML-JS-RSS-Feed.html">RSS Feed written in HTML + JavaScript</a></li>
<ul>
<li>Short code-snippet for an RSS feed, written in HTML and JavaScript</li>