diff --git a/.gitignore b/.gitignore index f8ec04c..d48e085 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.conf *.txt -log/ \ No newline at end of file +log/ +*.bak \ No newline at end of file diff --git a/discorss.py b/discorss.py index bd1f182..3cd9ce6 100755 --- a/discorss.py +++ b/discorss.py @@ -27,6 +27,7 @@ log_file_name = r"/app.log" # Yes, I know you "can't parse HTML with regex", but # just watch me. html_filter = re.compile(r"\<\/?([A-Za-z \"\=])*\>") +success_codes = ['200', '201', '202', '203', '204', '205', '206'] def get_description(feed): @@ -59,14 +60,18 @@ def main(): with open(config_file_path, "r") as config_file: app_config = json.load(config_file) now = time.mktime(time.localtime()) - last_check = app_config["lastupdate"] + try: + last_check = app_config["lastupdate"] + except KeyError: + last_check = now - 21600 # first run, no lastupdate, check up to 6 hours ago for i, hook in enumerate(app_config["feeds"]): # Get the feed feed = feedparser.parse(hook["url"]) published_time = time.mktime(feed.entries[0]["published_parsed"]) published_time = published_time + hook["offset"] print("Parsing feed {}...".format(hook["name"])) - new_hash = hashlib.sha3_512(feed.entries[0]["title"]) + # Hash the title of the latest post and use that to determine if it's been posted + new_hash = hashlib.sha3_512(bytes(feed.entries[0]["title"], 'utf-8')).hexdigest() try: if hook["lasthash"] != new_hash: app_config["feeds"][i]["lasthash"] = new_hash @@ -101,12 +106,12 @@ def main(): "content-type": "application/json", } webhook_string = json.dumps(webhook) - # print(webhook_string) - if published_time > last_check and published_time < now: + + if published_time > last_check: r = requests.post( hook["webhook"], data=webhook_string, headers=custom_header ) - if r.status_code != "200": + if r.status_code not in success_codes: print( "Error {} while trying to post {}".format( r.status_code, hook["webhook"]