Submitted by aabbaabb in gemini

--- post_orig.py	2022-06-28 18:21:16.535706715 +0200
+++ post.py	2022-06-28 18:21:07.575227754 +0200
@@ -2,9 +2,13 @@
 import requests, re, os, sys, pypandoc, time
 from bs4 import BeautifulSoup
 from md2gemini import md2gemini
+from html2text import HTML2Text
 
 os.environ.setdefault('PYPANDOC_PANDOC', '/usr/bin/pandoc')
 
+H2Text = HTML2Text()
+H2Text.body_width = 0
+
 # Getting variables from query
 query = os.environ['QUERY_STRING']
 if not query:
@@ -29,7 +33,8 @@
 # Extracting info
 link = soup.find_all("a", class_="submission__link")[0]
 username = soup.find_all("a", class_="submission__submitter")[0].text.replace("/user/","")
-comment = soup.find_all("a", string=re.compile("comments?$"))[0]
+comments_head = soup.find_all("a", string=re.compile("comments?$"))[0]
+comments = soup.find_all("article", class_ = "comment")
 timestamp = soup.find_all("time", class_="submission__timestamp")[0]
 forum = soup.find_all("a", class_="submission__forum")[0].text.replace("/f/", "")
 score = soup.find_all("span", class_="vote__net-score")[0]
@@ -66,6 +71,29 @@
 
 print(f"""{content_gmi}
 
-## {comment.text}
-This feature isn't implemented yet
+## {comments_head.text}
 """)
+
+comment_number = 1
+for comment in comments:
+	comment.attrs["comment_number"] = comment_number
+	
+	user = comment.h1.a.text
+	time = comment.h1.time.text
+	comment_body = comment.find(attrs={"class":"comment__body"})
+	
+	comment_md = H2Text.handle(str(comment_body))
+	comment_gmi = md2gemini(comment_md, links='paragraph')
+	
+	print(f"{user} {time} #{comment_number}")
+	
+	if comment.attrs["data-level"] != '1':
+		parent = comment.find_parent(name="article")
+		parent_number = parent.attrs["comment_number"]
+		parent_user = parent.a.text
+		
+		print(f"Replying to {parent_user} #{parent_number}")
+	
+	print(f"\n{comment_gmi}\n\n")
+	
+	comment_number += 1

4

Comments

You must log in or register to comment.

SnowCode wrote

Wow. It been a while since I last was active on here. Thanks a lot I'll test and try to implement those changes. Or at least I will try to not forget I don't feel that good at the moment and I'll be gone for a month. Thank you so much for doing this though ❤️

3