Skip to content

Commit

Permalink
Update Script - Improve spam list version checking
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ThioJoe committed Dec 31, 2021
1 parent abe52a1 commit 8badc33
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions YouTubeSpammerPurge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8badc33

Please sign in to comment.