Skip to content

Commit

Permalink
Update Script - Fixed exception handling crash
Browse files Browse the repository at this point in the history
Added Try Except block around main(), which should catch exceptions that weren't being caught before. Such as the quota exceeded exception.
  • Loading branch information
ThioJoe committed Dec 23, 2021
1 parent 03d2abb commit a859563
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions YouTubeSpammerPurge.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ def get_authenticated_service():
return build(API_SERVICE_NAME, API_VERSION, credentials=creds, discoveryServiceUrl=DISCOVERY_SERVICE_URL)


############################ EXCEPTION MESSAGES ##########################################
def print_exception_reason(reason):
print(" Reason: " + str(reason))
if reason == "processingFailure":
print(f"\n !! {F.RED}Processing Error{S.R} - Sometimes this error fixes itself. Try just running the program again. !!")
print("This issue is often on YouTube's side, so if it keeps happening try again later.")
print("(This also occurs if you try deleting comments on someone elses video, which is not possible.)")
elif reason == "commentsDisabled":
print(f"\n{F.LIGHTRED_EX}Error:{S.R} Comments are disabled on this video. This error can also occur if scanning a live stream.")
elif reason == "quotaExceeded":
print(f"\n{F.LIGHTRED_EX}Error:{S.R} You have exceeded the YouTube API quota. To do more scanning you must wait until the quota resets.")
print(" There is a daily limit of 10,000 units/day, which works out to around reporting 10,000 comments/day.")
print(" You can check your quota by searching 'quota' in the google cloud console.")
print(f"{F.YELLOW}Solutions: Either wait until tomorrow, or create additional projects in the cloud console.{S.R}")
input("\n Press Enter to Exit...")

##########################################################################################
############################### PRINT SPECIFIC COMMENTS ##################################
##########################################################################################
Expand Down Expand Up @@ -919,7 +935,7 @@ def fetch():
fields="items/id,items/snippet/title"
).execute()
return results
results = fetch()
results = fetch()

# Fetch the channel ID and title from the API response
# Catch exceptions if problems getting info
Expand Down Expand Up @@ -2732,19 +2748,7 @@ def scan_video(youtube, miscData, currentUser, filterMode, filterSubMode, videoI
print(" Status Code: "+ str(e.status_code))
if e.error_details[0]["reason"]: # If error reason is available, print it
reason = str(e.error_details[0]["reason"])
print(" Reason: " + reason)
if reason == "processingFailure":
print(f"\n !! {F.RED}Processing Error{S.R} - Sometimes this error fixes itself. Try just running the program again. !!")
print("This issue is often on YouTube's side, so if it keeps happening try again later.")
print("(This also occurs if you try deleting comments on someone elses video, which is not possible.)")
elif reason == "commentsDisabled":
print("\n Error: Comments are disabled on this video. This error can also occur if scanning a live stream.")
elif reason == "quotaExceeded":
print("\n Error: You have exceeded the YouTube API quota. To do more scanning you must wait until the quota resets.")
print("There is a daily limit of 10,000 units/day, which works out to around reporting 10,000 comments/day.")
print("You can check your quota by searching 'quota' in the google cloud console.")
print(f"{F.YELLOW}Solutions: Either wait until tomorrow, or create additional projects in the cloud console.{S.R}")
input("\n Press Enter to Exit...")
print_exception_reason(reason)
input("\n Press Enter to Exit...")
else:
print(f"{F.RED}Unknown Error - Code: X-2{S.R} occurred. If this keeps happening, consider posting a bug report on the GitHub issues page, and include the above error info.")
Expand All @@ -2768,7 +2772,17 @@ def scan_video(youtube, miscData, currentUser, filterMode, filterSubMode, videoI
# with open("output_calls.txt", "w") as f:
# p = pstats.Stats("output.dat", stream=f)
# p.sort_stats("calls").print_stats()

main()
try:
main()
except HttpError as e:
traceback.print_exc()
print("------------------------------------------------")
print("Error Message: " + str(e))
if e.status_code:
print("Status Code: " + str(e.status_code))
if e.error_details[0]["reason"]: # If error reason is available, print it
reason = str(e.error_details[0]["reason"])
print_exception_reason(reason)
input("\nPress Enter to Exit...")


0 comments on commit a859563

Please sign in to comment.