Skip to main content

Continue tidying up the Twitter thread script

ID
b516a36
date
2023-12-25 23:56:45+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
772618f
message
Continue tidying up the Twitter thread script
changed files
2 files, 58 additions, 1 deletion

Changed files

text/fix_twitter_thread.py (5137) → text/fix_twitter_thread.py (5852)

diff --git a/text/fix_twitter_thread.py b/text/fix_twitter_thread.py
index 05051fa..89604d5 100755
--- a/text/fix_twitter_thread.py
+++ b/text/fix_twitter_thread.py
@@ -154,6 +154,36 @@ def download_images(text: str, /, *, handle: str) -> str:
     return text
 
 
+def remove_view_count_and_reply(text: str) -> str:
+    # Remove the view count and link for me to reply, which are spread
+    # across multiple lines, e.g.:
+    #
+    # 6,260
+    #
+    # Views
+    #
+    # [
+    #
+    # ![Alex Chan](https://pbs.twimg.com/profile_images/1538296879137562624/w3pwqwel_x96.jpg)
+    #
+    # ](https://twitter.com/alexwlchan)
+    text = re.sub(
+        r"\n[0-9,]+\n"
+        r"\n"
+        r"Views\n"
+        r"\n"
+        r"\[\n"
+        r"\n"
+        r"!\[Alex Chan\]\(https://pbs\.twimg\.com/profile_images/[^\)]+\)\n"
+        r"\n"
+        r"\]\(https://twitter\.com/alexwlchan\)\s*\n",
+        "",
+        text,
+    )
+
+    return text
+
+
 if __name__ == "__main__":
     try:
         path = sys.argv[1]
@@ -166,6 +196,8 @@ if __name__ == "__main__":
     with open(path) as in_file:
         text = in_file.read()
 
+    text = remove_view_count_and_reply(text)
+
     handle = re.search(PROFILE_URL_RE, text).group("username")
     print("Detected handle as", termcolor.colored(handle, "blue"))
 

text/test_fix_twitter_thread.py (754) → text/test_fix_twitter_thread.py (1116)

diff --git a/text/test_fix_twitter_thread.py b/text/test_fix_twitter_thread.py
index fa9759e..573dfd5 100644
--- a/text/test_fix_twitter_thread.py
+++ b/text/test_fix_twitter_thread.py
@@ -1,6 +1,10 @@
 import pytest
 
-from fix_twitter_thread import fix_emoji, remove_profile_links
+from fix_twitter_thread import (
+    fix_emoji,
+    remove_profile_links,
+    remove_view_count_and_reply,
+)
 
 
 @pytest.mark.parametrize(
@@ -25,3 +29,24 @@ def test_remove_profile_links() -> None:
         )
         == ""
     )
+
+
+def test_remove_view_count_and_reply() -> None:
+    assert (
+        remove_view_count_and_reply(
+            """Hello
+
+6,260
+
+Views
+
+[
+
+![Alex Chan](https://pbs.twimg.com/profile_images/1538296879137562624/w3pwqwel_x96.jpg)
+
+](https://twitter.com/alexwlchan)
+
+world"""
+        )
+        == "Hello\n\nworld"
+    )