From 50ae0baa49c0b58d13eaeb6618d0920c7315a481 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Fri, 25 Jun 2021 02:25:52 +0530 Subject: updated --- docs/feed.rss | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 105 insertions(+), 2 deletions(-) (limited to 'docs/feed.rss') 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 @@ Navan's Archive Rare Tips, Tricks and Posts https://web.navan.dev/en - Thu, 27 May 2021 00:22:21 -0000 - Thu, 27 May 2021 00:22:21 -0000 + Fri, 25 Jun 2021 02:25:20 -0000 + Fri, 25 Jun 2021 02:25:20 -0000 250 @@ -55,6 +55,109 @@ ]]> + + + https://web.navan.dev/posts/2021-06-25-Blog2Twitter-P1.html + + + Posting Blog Posts as Twitter Threads Part 1/n + + + Converting Posts to Twitter Threads + + https://web.navan.dev/posts/2021-06-25-Blog2Twitter-P1.html + Fri, 25 Jun 2021 00:08:00 -0000 + 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

+ +
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.

+ +
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.

+ +
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.

+ +
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.

+]]>
+
+ https://web.navan.dev/posts/hello-world.html -- cgit v1.2.3