Update dnet-stats.py
Simplified the script.
This commit is contained in:
1 parent
db5e692941
commit
9f5f6a241a
1 file changed
+37
-80
+37
-80
@@ -1,12 +1,14 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# Description: A command-line interface to https://stats.distributed.net
|
# Description: A command-line interface to https://stats.distributed.net
|
||||||
# Usage: python3 dnet-stats.py -Project <project> -User <username>
|
# Usage: python3 dnet-stats.py -p <project> -u <username>
|
||||||
# Author: Justin Oros
|
# Author: Justin Oros
|
||||||
# Source: https://github.com/JustinOros
|
# Source: https://github.com/JustinOros
|
||||||
# Dependencies: pip install requests beautifulsoup4 lxml
|
# Dependencies: pip install requests argparse beautifulsoup4 lxml
|
||||||
|
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
import requests, sys
|
import requests
|
||||||
|
import argparse
|
||||||
|
import 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'
|
||||||
@@ -23,95 +25,47 @@ validProjects = {
|
|||||||
'OGR-28': 28,
|
'OGR-28': 28,
|
||||||
}
|
}
|
||||||
|
|
||||||
# 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'
|
|
||||||
|
|
||||||
# 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'
|
|
||||||
|
|
||||||
# Help Menu (Hint)
|
|
||||||
def helpHint():
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
if sys.argv[1].lower() == 'help':
|
|
||||||
helpMenu()
|
|
||||||
print(Text.Green + '\nType ' + Text.Bold + Text.Italics + 'dnet-stats.py help' + Text.Reset + Text.Green + ' for the help menu.\n' + Text.Reset)
|
|
||||||
exit()
|
|
||||||
|
|
||||||
# Help Menu (Main)
|
|
||||||
def helpMenu():
|
|
||||||
print(
|
|
||||||
Text.Bold + Text.Green + '\nHelp Menu:\n' +
|
|
||||||
Text.Bold + Text.Green + '\nUsage: ' +
|
|
||||||
Text.Reset + Text.Green + 'dnet-stats.py -Project <project> -User <username>\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: ' +
|
|
||||||
Text.Reset + Text.Green + 'python3 dnet-stats.py -Project RC5-72 -User bluecat9@penguinized.net\n' + Text.Reset
|
|
||||||
)
|
|
||||||
exit()
|
|
||||||
|
|
||||||
# Parse command-line arguments
|
# Parse command-line arguments
|
||||||
user = None
|
def parse_arguments():
|
||||||
project = None
|
parser = argparse.ArgumentParser(description='Fetch Distributed.net stats for user and project.')
|
||||||
|
parser.add_argument('-u', '--user', type=str, required=True, help='Username of the participant.')
|
||||||
|
parser.add_argument('-p', '--project', type=str, required=True, choices=validProjects.keys())
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Check if the arguments are provided and valid
|
return parser.parse_args()
|
||||||
for i in range(1, len(sys.argv)):
|
|
||||||
if sys.argv[i].lower() == "-project" and i + 1 < len(sys.argv):
|
|
||||||
project = sys.argv[i + 1].upper()
|
|
||||||
if sys.argv[i].lower() == "-user" and i + 1 < len(sys.argv):
|
|
||||||
user = sys.argv[i + 1]
|
|
||||||
|
|
||||||
# Ensure both project and user are present
|
# Main execution function
|
||||||
if not user or not project:
|
def main():
|
||||||
print(Text.Bold + Text.Red + "Error: Missing required arguments. Use -Project and -User." + Text.Reset)
|
# Parse the command-line arguments
|
||||||
helpMenu()
|
args = parse_arguments()
|
||||||
|
|
||||||
# Ensure the project is valid
|
# Retrieve user and project from parsed arguments
|
||||||
if project not in validProjects:
|
user = args.user
|
||||||
print(Text.Bold + Text.Red + f"Error: {project} is not a valid project.\n" + Text.Reset)
|
project = args.project
|
||||||
helpMenu()
|
|
||||||
|
|
||||||
# Get project ID
|
# Get project ID
|
||||||
projectId = validProjects[project]
|
projectId = validProjects[project]
|
||||||
|
|
||||||
# Prepare data for the request
|
# Prepare data for the request
|
||||||
data = {'project_id': projectId, 'st': user}
|
data = {'project_id': projectId, 'st': user}
|
||||||
|
|
||||||
# Perform a POST request to fetch the response
|
# Perform a POST request to fetch the response
|
||||||
response = requests.post(searchUrl, data=data)
|
response = requests.post(searchUrl, data=data)
|
||||||
|
|
||||||
if response: # Proceed if we received a response
|
if response: # Proceed if we received a response
|
||||||
soup = BeautifulSoup(response.text, 'lxml') # Load the response into BeautifulSoup
|
soup = BeautifulSoup(response.text, 'lxml') # Load the response into BeautifulSoup
|
||||||
|
|
||||||
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(Text.Bold + Color.Theme1 + '\nUser: ' + user + Text.Reset) # Print user
|
print(f'\nUser: {user}') # 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_name = summary[0] # Store the project name from the summary into a variable
|
project_name = summary[0] # Store the project name from the summary into a variable
|
||||||
print(Text.Bold + Color.Theme2 + 'Project: ' + project_name) # Print project
|
print(f'Project: {project_name}') # Print project
|
||||||
else:
|
else:
|
||||||
print('\n' + Text.Bold + Text.Red + 'Error: ' + user + ' not found for project ' + project + '.\n' + Text.Reset) # Notify if user not found
|
print(f"\nError: {user} not found for project {project}.\n") # Notify if user not found
|
||||||
exit()
|
sys.exit()
|
||||||
|
|
||||||
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
|
||||||
@@ -127,14 +81,17 @@ if response: # Proceed if we received a response
|
|||||||
currentRank = currentRank.split('(')
|
currentRank = currentRank.split('(')
|
||||||
currentRank = currentRank[0]
|
currentRank = currentRank[0]
|
||||||
if line == 3: # Print current and overall rank
|
if line == 3: # Print current and overall rank
|
||||||
print(Text.Bold + Color.Theme3 + 'Rank: ' + Text.Underline + currentRank + Text.Reset)
|
print(f'Rank: {currentRank}')
|
||||||
print(Text.Bold + Color.Theme4 + 'Overall: ' + overallRank + Text.Reset)
|
print(f'Overall: {overallRank}')
|
||||||
break
|
break
|
||||||
|
|
||||||
# Get last update date
|
# Get last update date
|
||||||
lastUpdate = soup.find('td', class_="lastupdate").text.split()
|
lastUpdate = soup.find('td', class_="lastupdate").text.split()
|
||||||
lastUpdate = lastUpdate[8].lstrip()
|
lastUpdate = lastUpdate[8].lstrip()
|
||||||
print(Text.Bold + Color.Theme5 + 'Updated: ' + lastUpdate + Text.Reset + '\n')
|
print(f'Updated: {lastUpdate}\n')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(Text.Bold + Text.Red + 'An error has occurred while fetching data.' + Text.Reset)
|
print('An error has occurred while fetching data.')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in new issue
Block a user