From 8badc33377e4e359dfb99cdde8408a839f407866 Mon Sep 17 00:00:00 2001 From: ThioJoe <12518330+ThioJoe@users.noreply.github.com> Date: Fri, 31 Dec 2021 15:49:43 -0700 Subject: [PATCH] Update Script - Improve spam list version checking Made it so before using GitHub api to check latest version, just use see if current date lines up with version in file. To avoid unnecessarily having to hit the api --- YouTubeSpammerPurge.py | 60 +++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/YouTubeSpammerPurge.py b/YouTubeSpammerPurge.py index e246c3dd..5fde89ce 100644 --- a/YouTubeSpammerPurge.py +++ b/YouTubeSpammerPurge.py @@ -49,7 +49,7 @@ import re import sys import time -from datetime import datetime +from datetime import datetime, date, timedelta import traceback import platform import requests @@ -1565,6 +1565,17 @@ def check_lists_update(currentListVersion, silentCheck = False): #otherlink = 'https://api.github.com/repos/ThioJoe/YT-Spam-Domains-List/contents/SpamDomainsList.txt' #spamDomainHostedLocation = "https://cdn.jsdelivr.net/gh/thiojoe/YT-Spam-Domains-List/SpamDomainsList.txt" + if os.path.isdir("spam_lists"): + pass + else: + try: + os.mkdir("spam_lists") + except: + print("Error: Could not create folder. Try creating a folder called 'spam_lists' to update the spam lists.") + + if os.path.exists("spam_lists/SpamDomainsList.txt"): + currentListVersion = get_list_file_version("spam_lists/SpamDomainsList.txt") + response = getRemoteFile(spamDomainListLatestCommit, silentCheck) if response != None: pass @@ -1573,18 +1584,13 @@ def check_lists_update(currentListVersion, silentCheck = False): else: input("Press enter to Exit...") sys.exit() - - if os.path.isdir("spam_lists"): - pass - else: - try: - os.mkdir("spam_lists") - except: - print("Error: Could not create folder. Try creating a folder called 'spam_lists' to update the spam lists.") + + + # Get latest update time on github as proxy for latest file version listUpdateDateTime = response.json()[0]['commit']['committer']['date'] - date = datetime.strptime(listUpdateDateTime, '%Y-%m-%dT%H:%M:%SZ') - latestVersion = date.strftime('%Y.%m.%d') + listUpdateDateTime = datetime.strptime(listUpdateDateTime, '%Y-%m-%dT%H:%M:%SZ') + latestVersion = listUpdateDateTime.strftime('%Y.%m.%d') if currentListVersion == None or (parse_version(latestVersion) > parse_version(currentListVersion)): downloadFilePath = "spam_lists/SpamDomainsList.txt" @@ -1628,7 +1634,9 @@ def ingest_list_file(relativeFilePath): if not line.startswith('#'): line = line.strip() processedList.append(line.lower()) - return processedList + return processedList + else: + return None def get_list_file_version(relativeFilePath): if os.path.exists(relativeFilePath): @@ -2390,15 +2398,29 @@ def main(): except: print(f"{F.LIGHTRED_EX}Error Code U-3 occurred while checking for updates. (Checking can be disabled using the config file setting) Continuing...{S.R}\n") updateAvailable = False - try: + + # Check if spam list file exists, see if out of date based on today's date + # Therefore should only need to use GitHub api once a day + if os.path.exists(spamDomainListPath): spamDomainListVersion = get_list_file_version(spamDomainListPath) - check_lists_update(spamDomainListVersion) - spamDomainList = ingest_list_file(spamDomainListPath) - except Exception as e: - print(e) - spamDomainList = ingest_asset_file(spamDomainListFileName) + else: + spamDomainListVersion = None + + # Check if today or tomorrow's date is later than the last update date (add day to account for time zones) + if spamDomainListVersion and (datetime.today()+timedelta(days=1) >= datetime.strptime(spamDomainListVersion, '%Y.%m.%d')): + spamDomainList = ingest_list_file(spamDomainListPath) + else: + try: + check_lists_update(spamDomainListVersion) + spamDomainList = ingest_list_file(spamDomainListPath) + except Exception as e: + # Get backup from asset folder + spamDomainList = ingest_asset_file(spamDomainListFileName) + else: - spamDomainList = ingest_asset_file(spamDomainListFileName) + spamDomainList = ingest_list_file(spamDomainListPath) + if spamDomainList == None: + spamDomainList = ingest_asset_file(spamDomainListFileName) os.system(clear_command) # Load any other data