2021-09-07 09:35:02 -07:00
|
|
|
#!/usr/bin/python3
|
2023-07-17 07:21:13 -07:00
|
|
|
# Description: A command-line interface to stats.distributed.net
|
|
|
|
|
# Usage: python3 dnet-stats.py <user> <project>
|
2021-12-16 21:51:07 -07:00
|
|
|
# Author: Justin Oros
|
|
|
|
|
# Source: https://github.com/JustinOros
|
2023-07-17 07:21:13 -07:00
|
|
|
|
2021-09-06 16:30:12 -07:00
|
|
|
from bs4 import BeautifulSoup
|
2021-09-08 09:39:12 -07:00
|
|
|
import requests, sys
|
2021-09-06 16:30:12 -07:00
|
|
|
|
2021-12-09 07:30:57 -07:00
|
|
|
# Distributed.net Participant Search Form
|
2021-10-26 17:21:38 -07:00
|
|
|
searchUrl = 'https://stats.distributed.net/participant/psearch.php'
|
|
|
|
|
|
2021-12-09 07:30:57 -07:00
|
|
|
# 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,
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 13:59:11 -07:00
|
|
|
# Text Colors & Formatting
|
|
|
|
|
class Text:
|
|
|
|
|
Pink = '\033[95m'
|
|
|
|
|
Black = '\u001b[30m'
|
|
|
|
|
Red = '\u001b[31m'
|
|
|
|
|
Green = '\u001b[32m'
|
|
|
|
|
Yellow = '\u001b[33m'
|
|
|
|
|
Blue = '\u001b[34m'
|
|
|
|
|
Magenta = '\u001b[35m'
|
|
|
|
|
Cyan = '\u001b[36m'
|
|
|
|
|
White = '\u001b[37m'
|
|
|
|
|
Bold = '\033[1m'
|
|
|
|
|
Italics = '\x1B[3m'
|
|
|
|
|
Underline = '\033[4m'
|
|
|
|
|
Reset = '\u001b[0m'
|
2021-12-08 11:26:46 -07:00
|
|
|
|
2021-12-18 13:59:11 -07:00
|
|
|
# Text Color Theme
|
|
|
|
|
class Color:
|
|
|
|
|
Theme1 = '\u001b[38;5;165m'
|
|
|
|
|
Theme2 = '\u001b[38;5;164m'
|
|
|
|
|
Theme3 = '\u001b[38;5;163m'
|
|
|
|
|
Theme4 = '\u001b[38;5;162m'
|
|
|
|
|
Theme5 = '\u001b[38;5;161m'
|
|
|
|
|
|
2021-12-09 07:30:57 -07:00
|
|
|
# Help Menu (Hint)
|
|
|
|
|
def helpHint():
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
|
if sys.argv[1].lower() == 'help':
|
|
|
|
|
helpMenu()
|
2021-12-18 13:59:11 -07:00
|
|
|
print(Text.Green + '\nType ' + Text.Bold + Text.Italics + 'dnet-stats.py help' + Text.Reset + Text.Green + ' for the help menu.\n' + Text.Reset)
|
2021-12-09 07:30:57 -07:00
|
|
|
exit()
|
2021-10-26 17:21:38 -07:00
|
|
|
|
2021-12-09 07:30:57 -07:00
|
|
|
# Help Menu (Main)
|
|
|
|
|
def helpMenu():
|
2021-10-26 17:21:38 -07:00
|
|
|
print(
|
2021-12-18 13:59:11 -07:00
|
|
|
Text.Bold + Text.Green + '\nHelp Menu:\n' +
|
|
|
|
|
Text.Bold + Text.Green + '\nUsage: ' +
|
|
|
|
|
Text.Reset + Text.Green + 'dnet-stats.py <username> <project>\n' +
|
|
|
|
|
Text.Bold + Text.Green + '\nProjects: ' +
|
|
|
|
|
Text.Reset + Text.Green + 'RC5-56, RC5-64, RC5-72, OGR-24, OGR-25, OGR-26, OGR-27, OGR-28\n' +
|
|
|
|
|
Text.Bold + Text.Green + '\nExample: ' +
|
2021-12-18 14:05:49 -07:00
|
|
|
Text.Reset + Text.Green + 'dnet-stats.py bluecat9@penguinized.net RC5-72\n' + Text.Reset
|
2021-09-08 09:39:12 -07:00
|
|
|
)
|
|
|
|
|
exit()
|
2021-09-06 17:36:48 -07:00
|
|
|
|
2021-12-09 07:30:57 -07:00
|
|
|
# If no arguments are given, display Help Menu hint
|
2021-10-26 17:21:38 -07:00
|
|
|
if len(sys.argv) <= 2:
|
2021-12-09 07:30:57 -07:00
|
|
|
helpHint()
|
2021-09-06 16:30:12 -07:00
|
|
|
|
2021-10-26 17:21:38 -07:00
|
|
|
# get username from user input
|
|
|
|
|
myUser = sys.argv[1]
|
2021-09-06 16:30:12 -07:00
|
|
|
|
2021-10-26 17:21:38 -07:00
|
|
|
# get project from user input
|
|
|
|
|
myProject = sys.argv[2].upper()
|
2021-09-08 09:39:12 -07:00
|
|
|
|
2021-10-26 17:21:38 -07:00
|
|
|
# if we have all arguments needed, proceed...
|
2021-09-08 09:39:12 -07:00
|
|
|
if len(sys.argv) == 3:
|
2021-10-26 17:21:38 -07:00
|
|
|
# ensure the project argument is a valid project
|
2021-11-21 19:07:10 -07:00
|
|
|
if sys.argv[2].upper() in ['RC5-56','RC5-64','RC5-72','OGR-24','OGR-25','OGR-26','OGR-27','OGR-28']:
|
2021-10-26 17:21:38 -07:00
|
|
|
# if it is valid, store the Project ID into a variable
|
|
|
|
|
projectId = validProjects[sys.argv[2].upper()]
|
2021-09-08 09:39:12 -07:00
|
|
|
else:
|
2021-10-26 17:21:38 -07:00
|
|
|
# otherwise, let the user know their project was not valid
|
2021-12-18 13:59:11 -07:00
|
|
|
print(Text.Bold + Text.Red + '\nError: ' + sys.argv[2].upper() + ' is not a valid project.\n' + Text.Reset)
|
2021-09-08 09:39:12 -07:00
|
|
|
exit()
|
|
|
|
|
|
2021-10-26 17:21:38 -07:00
|
|
|
data = {'project_id':projectId,'st':myUser} # along with the project id
|
2021-12-09 07:30:57 -07:00
|
|
|
|
2021-10-26 17:21:38 -07:00
|
|
|
response = requests.post(searchUrl,data=data) # perfom a form post and get the response
|
2021-09-06 16:30:12 -07:00
|
|
|
|
2021-12-13 12:01:05 -07:00
|
|
|
if response: # if we received a response
|
|
|
|
|
soup = BeautifulSoup(response.text, 'lxml') # load the response into BeautifulSoup
|
|
|
|
|
|
|
|
|
|
summary = soup.find('td',class_='htitle').text.lstrip() # find summary
|
|
|
|
|
|
|
|
|
|
if "Summary" in summary:
|
2021-12-18 13:59:11 -07:00
|
|
|
print(Text.Bold + Color.Theme1 + '\nUser: ' + myUser + Text.Reset) # print user
|
2021-12-13 12:01:05 -07:00
|
|
|
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
|
2021-12-18 13:59:11 -07:00
|
|
|
print(Text.Bold + Color.Theme2 + 'Project: ' + project) # print project
|
2021-12-13 12:01:05 -07:00
|
|
|
else:
|
2021-12-18 13:59:11 -07:00
|
|
|
print('\n' + Text.Bold + Text.Red + 'Error: ' + myUser + ' not found for project ' + myProject + '.\n' + Text.Reset) # notify if user not found
|
2021-09-08 09:39:12 -07:00
|
|
|
exit()
|
2021-09-06 16:30:12 -07:00
|
|
|
|
|
|
|
|
line = 0
|
2021-09-06 17:36:48 -07:00
|
|
|
for match in soup.find_all('td',align='right'): # search soup for table data
|
2021-09-06 16:30:12 -07:00
|
|
|
line +=1
|
2021-12-09 07:30:57 -07:00
|
|
|
if line == 1: # get overall rank
|
2021-10-26 17:21:38 -07:00
|
|
|
overallRank = match.text.lstrip()
|
|
|
|
|
if overallRank[0] != "T" and overallRank[0] != "0":
|
|
|
|
|
overallRank = overallRank.split('(')
|
|
|
|
|
overallRank = overallRank[0]
|
2021-12-09 07:30:57 -07:00
|
|
|
if line == 2: # get current rank
|
2021-10-26 17:21:38 -07:00
|
|
|
currentRank = match.text.lstrip()
|
|
|
|
|
if currentRank[0] != "0":
|
|
|
|
|
currentRank = currentRank.split('(')
|
|
|
|
|
currentRank = currentRank[0]
|
2021-12-09 07:30:57 -07:00
|
|
|
if line == 3: # print current and overall rank
|
2021-12-18 13:59:11 -07:00
|
|
|
print (Text.Bold + Color.Theme3 + 'Rank: ' + Text.Underline + currentRank + Text.Reset)
|
|
|
|
|
print (Text.Bold + Color.Theme4 + 'Overall: ' + overallRank + Text.Reset)
|
2021-09-06 16:30:12 -07:00
|
|
|
break
|
2021-12-16 21:51:07 -07:00
|
|
|
|
|
|
|
|
# get last update date
|
|
|
|
|
lastUpdate = soup.find('td',class_="lastupdate").text.split()
|
|
|
|
|
lastUpdate = lastUpdate[8].lstrip()
|
2021-12-18 13:59:11 -07:00
|
|
|
print(Text.Bold + Color.Theme5 + 'Updated: ' + lastUpdate + Text.Reset + '\n')
|
2021-12-16 21:51:07 -07:00
|
|
|
|
2021-09-06 16:30:12 -07:00
|
|
|
else:
|
2021-12-18 13:59:11 -07:00
|
|
|
print(Text.Bold + Text.Red + 'An error has occuRed.' + Text.Reset)
|