summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Content/posts/2021-06-25-Blog2Twitter-P1.md98
-rw-r--r--docs/posts/2021-06-25-Blog2Twitter-P1.html119
2 files changed, 217 insertions, 0 deletions
diff --git a/Content/posts/2021-06-25-Blog2Twitter-P1.md b/Content/posts/2021-06-25-Blog2Twitter-P1.md
new file mode 100644
index 0000000..3c9bb83
--- /dev/null
+++ b/Content/posts/2021-06-25-Blog2Twitter-P1.md
@@ -0,0 +1,98 @@
+---
+date: 2021-06-25 00:08
+description: Converting Posts to Twitter Threads
+tags: Python, Twitter, Eh
+---
+
+# Posting Blog Posts as Twitter Threads Part 1/n
+
+Why? Eh, no good reason, but should be fun.
+
+## Plan of Action
+
+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.
+
+## Time to Code
+
+First, the dependency: tweepy for tweeting.
+
+`pip install tweepy`
+
+```python
+import os
+import tweepy
+
+consumer_key = os.environ["consumer_key"]
+consumer_secret = os.environ["consumer_secret"]
+
+access_token = os.environ["access_token"]
+access_token_secret = os.environ["access_token_secret"]
+
+auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
+auth.set_access_token(access_token, access_token_secret)
+
+api = tweepy.API(auth)
+```
+
+The program need to convert the blog post into text fragments.
+
+It reads the markdown file, removes the top YAML content, checks for headers and splits the content.
+
+```python
+tweets = []
+
+first___n = 0
+
+with open(sample_markdown_file) as f:
+ for line in f.readlines():
+ if first___n <= 1:
+ if line == "---\n":
+ first___n += 1
+ continue
+ line = line.strip()
+ line += " "
+ if "#" in line:
+ line = line.replace("#","")
+ line.strip()
+ line = "\n" + line
+ line += "\n\n"
+ try:
+ if len(tweets[-1]) < 260 and (len(tweets[-1]) + len(line)) <= 260:
+ tweets[-1] += line
+ else:
+ tweets.append(line)
+ except IndexError:
+ if len(line) > 260:
+ print("ERROR")
+ else:
+ tweets.append(line)
+```
+
+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.
+
+For every tweet fragment, it also append 1/n.
+
+```python
+for idx, tweet in enumerate(tweets):
+ tweet += " {}/{}".format(idx+1,len(tweets))
+ if idx == 0:
+ a = None
+ a = api.update_status(tweet)
+ else:
+ a = api.update_status(tweet,in_reply_to_status_id=a.id)
+ print(len(tweet),end=" ")
+ print("{}/{}\n".format(idx+1,len(tweets)))
+```
+
+Finally, it replies to the last tweet in the thread with the link of the post.
+
+```python
+api.update_status("Web Version: {}".format(post_link))
+```
+
+## What's Next?
+
+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. \ No newline at end of file
diff --git a/docs/posts/2021-06-25-Blog2Twitter-P1.html b/docs/posts/2021-06-25-Blog2Twitter-P1.html
new file mode 100644
index 0000000..7393f4b
--- /dev/null
+++ b/docs/posts/2021-06-25-Blog2Twitter-P1.html
@@ -0,0 +1,119 @@
+<!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</title>
+
+</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 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>
+
+</main>
+
+
+<script src="assets/manup.min.js"></script>
+<script src="/pwabuilder-sw-register.js"></script>
+</body>
+</html> \ No newline at end of file