Update dnet-stats.py

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