Update dnet-stats.py

Added Help Menu.
This commit is contained in:
Justin Oros
2021-12-09 07:30:57 -07:00
committed by GitHub
parent 1066f5d531
commit 02938e4f67
+37 -27
View File
@@ -2,10 +2,22 @@
from bs4 import BeautifulSoup
import requests, sys
# Distributed.net participant search form
# Distributed.net Participant Search Form
searchUrl = 'https://stats.distributed.net/participant/psearch.php'
# Output Formatting
# Distributed.net Projects (and associated IDs)
validProjects = {
'RC5-56' : 3,
'RC5-64' : 5,
'RC5-72' : 8,
'OGR-24' : 24,
'OGR-25' : 25,
'OGR-26' : 26,
'OGR-27' : 27,
'OGR-28' : 28,
}
# Format Output
class FORMAT:
PINK = '\033[95m'
BLUE = '\033[94m'
@@ -18,32 +30,30 @@ class FORMAT:
UNDERLINE = '\033[4m'
ITALICS = '\x1B[3m'
# Valid Distributed.net Projects (and associated ID)
validProjects = {
'RC5-56' : 3,
'RC5-64' : 5,
'RC5-72' : 8,
'OGR-24' : 24,
'OGR-25' : 25,
'OGR-26' : 26,
'OGR-27' : 27,
'OGR-28' : 28,
}
# Help Menu (Hint)
def helpHint():
if len(sys.argv) > 1:
if sys.argv[1].lower() == 'help':
helpMenu()
print(FORMAT.GREEN + '\nType ' + FORMAT.BOLD + FORMAT.ITALICS + 'dnet-stats.py help' + FORMAT.REGULAR + FORMAT.GREEN + ' for the help menu.\n')
exit()
# Help menu
def printHelp():
# Help Menu (Main)
def helpMenu():
print(
FORMAT.BOLD + FORMAT.GREEN + '\nExample: ' +
FORMAT.REGULAR + FORMAT.GREEN + 'dnet-stats.py bluecat9@penguinized.net RC5-72\n' +
FORMAT.BOLD + FORMAT.GREEN + '\nHelp Menu:\n' +
FORMAT.BOLD + FORMAT.GREEN + '\nUsage: ' +
FORMAT.REGULAR + FORMAT.GREEN + 'dnet-stats.py <username> <project>\n' +
FORMAT.BOLD + FORMAT.GREEN + '\nProjects: ' +
FORMAT.REGULAR + FORMAT.GREEN + 'RC5-56, RC5-64, RC5-72, OGR-24, OGR-25, OGR-26, OGR-27, OGR-28\n'
FORMAT.REGULAR + FORMAT.GREEN + 'RC5-56, RC5-64, RC5-72, OGR-24, OGR-25, OGR-26, OGR-27, OGR-28\n' +
FORMAT.BOLD + FORMAT.GREEN + '\nExample: ' +
FORMAT.REGULAR + FORMAT.GREEN + 'dnet-stats.py bluecat9@penguinized.net RC5-72\n'
)
exit()
# If no arguments are given, display Help Menu
# If no arguments are given, display Help Menu hint
if len(sys.argv) <= 2:
print( FORMAT.BOLD + FORMAT.RED + '\nError: Please specify username' + FORMAT.ITALICS + ' and ' + FORMAT.REGULAR + FORMAT.BOLD + FORMAT.RED + 'project.')
printHelp()
helpHint()
# get username from user input
myUser = sys.argv[1]
@@ -59,11 +69,11 @@ if len(sys.argv) == 3:
projectId = validProjects[sys.argv[2].upper()]
else:
# otherwise, let the user know their project was not valid
print(FORMAT.BOLD + FORMAT.RED + '\nError: ' + sys.argv[2].upper() + ' is not a valid project.')
printHelp()
print(FORMAT.BOLD + FORMAT.RED + '\nError: ' + sys.argv[2].upper() + ' is not a valid project.\n')
exit()
data = {'project_id':projectId,'st':myUser} # along with the project id
response = requests.post(searchUrl,data=data) # perfom a form post and get the response
if response: # if we received a response
@@ -72,7 +82,7 @@ if response: # if we received a response
summary = soup.find('td',class_='htitle').text.lstrip() # find summary
if "Summary" in summary:
print(FORMAT.BOLD + FORMAT.YELLOW + '\nUser: ' + FORMAT.BLUE + myUser + FORMAT.REGULAR) # print user
print(FORMAT.BOLD + FORMAT.YELLOW + '\nUser: ' + FORMAT.RED + myUser + FORMAT.REGULAR) # print user
summary = " ".join(summary.split()) # remove extra spaces from summary
summary = summary.split('/') # split summary and project into 2 strings
project = summary[0] # store the project name from the summary into a variable
@@ -84,17 +94,17 @@ if response: # if we received a response
line = 0
for match in soup.find_all('td',align='right'): # search soup for table data
line +=1
if line == 1: # print overall rank
if line == 1: # get overall rank
overallRank = match.text.lstrip()
if overallRank[0] != "T" and overallRank[0] != "0":
overallRank = overallRank.split('(')
overallRank = overallRank[0]
if line == 2: # print current rank
if line == 2: # get current rank
currentRank = match.text.lstrip()
if currentRank[0] != "0":
currentRank = currentRank.split('(')
currentRank = currentRank[0]
if line == 3: # stop searching soup
if line == 3: # print current and overall rank
print (FORMAT.BOLD + FORMAT.YELLOW + 'Current Rank: ' + FORMAT.YELLOW + FORMAT.GREEN + FORMAT.UNDERLINE + currentRank + FORMAT.REGULAR)
print (FORMAT.BOLD + FORMAT.YELLOW + 'Overall Rank: ' + FORMAT.YELLOW + FORMAT.PINK + overallRank + '\n')
break