Commit 1d2a93d4 authored by marius david's avatar marius david
Browse files

do not spam failure

parent 8eda6ba7
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
from entry_manager import EntryManager
#from lib.github_forge import GithubForge
from lib.gitlab_forge import GitlabForge
import hashlib

class LemmyFetcher:
	def __init__(self, entry_manager):
@@ -14,9 +15,14 @@ def __init__(self, entry_manager):
		self.forge.from_env()
		self.read_ids = set()
		os.makedirs("processed_posts", exist_ok = True)
		os.makedirs("failed_posts", exist_ok = True)
		for filename in os.listdir("processed_posts"):
			self.read_ids.add(filename)
		
		self.failed_ids_hashes = set()
		for filename in os.listdir("failed_posts"):
			self.failed_ids_hashes.add(filename)
		
		login_req = requests.post("https://toast.ooo/api/v3/user/login", json={
			"username_or_email": os.environ.get("LEMMY_USERNAME"),
			"password": os.environ.get("LEMMY_PASSWORD"),
@@ -27,6 +33,19 @@ def __init__(self, entry_manager):
		self.lemmy_jwt = login_req.json()["jwt"]
	
	
	def is_post_failed(self, post_id, body):
		entry_key = str(post_id) + "-" + self.hash_body(body)
		return entry_key in self.failed_ids_hashes
	
	def mark_post_as_failed(self, post_id, body):
		entry_key = str(post_id) + "-" + self.hash_body(body)
		self.failed_ids_hashes.add(entry_key)
		with open("failed_posts/" + entry_key, "w") as f:
			f.write(body)
	
	def hash_body(self, body):
		return hashlib.md5(body.encode()).hexdigest()
	
	def process_post(self, post_body, make_pr = False):
		post_id = post_body["id"]

@@ -44,11 +63,16 @@ def process_post(self, post_body, make_pr = False):
		
		post_body_main = post_body["body"]

		if self.is_post_failed(post_id, post_body_main):
			print("Post already failed with same body: " + str(post_id))
			return

		post_json = None
		try:
			post_json = json.loads(post_body_main)
		except:
			print("Post does not contain valid JSON: " + str(post_id))
			self.mark_post_as_failed(post_id, post_body_main)
			self.send_comment(post_id, "An error occurred while parsing this post. Maybe it’s just not a submission, but if it should, then it is not well formatted.")
			return