Files
Python/dnet-user-stats.py
T

106 lines
3.7 KiB
Python
Raw Normal View History

2021-09-07 09:35:02 -07:00
#!/usr/bin/python3
2024-11-18 10:42:17 -07:00
# Description: A command-line interface to https://stats.distributed.net
2025-01-30 13:26:12 -07:00
# Usage: python3 dnet-user-stats.py -p <project> -u <username>
2021-12-16 21:51:07 -07:00
# Author: Justin Oros
# Source: https://github.com/JustinOros
2024-11-18 15:39:56 -07:00
# Dependencies: pip install requests argparse beautifulsoup4 lxml
2024-11-18 10:20:57 -07:00
2021-09-06 16:30:12 -07:00
from bs4 import BeautifulSoup
2024-11-20 17:47:00 -07:00
import requests, argparse, 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 = {
2024-11-18 10:20:57 -07:00
'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-09 07:30:57 -07:00
}
2024-11-18 10:20:57 -07:00
# Parse command-line arguments
2024-11-18 15:39:56 -07:00
def parse_arguments():
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()
2021-09-06 16:30:12 -07:00
2024-11-18 15:39:56 -07:00
return parser.parse_args()
2021-09-06 16:30:12 -07:00
2024-11-18 15:39:56 -07:00
# Main execution function
def main():
# Parse the command-line arguments
args = parse_arguments()
2024-11-18 15:39:56 -07:00
# Retrieve user and project from parsed arguments
user = args.user
project = args.project
2024-11-18 15:39:56 -07:00
# Get project ID
projectId = validProjects[project]
2021-12-09 07:30:57 -07:00
2024-11-18 15:39:56 -07:00
# Prepare data for the request
data = {'project_id': projectId, 'st': user}
2021-09-06 16:30:12 -07:00
2024-11-18 15:39:56 -07:00
# Perform a POST request to fetch the response
response = requests.post(searchUrl, data=data)
2024-11-18 15:39:56 -07:00
if response: # Proceed if we received a response
soup = BeautifulSoup(response.text, 'lxml') # Load the response into BeautifulSoup
2024-11-18 10:20:57 -07:00
2024-11-18 15:39:56 -07:00
summary = soup.find('td', class_='htitle').text.lstrip() # Find summary
2024-11-20 17:47:00 -07:00
if 'Summary' in summary:
2024-11-18 15:39:56 -07:00
print(f'\nUser: {user}') # Print user
2024-11-20 17:47:00 -07:00
summary = ' '.join(summary.split()) # Remove extra spaces from summary
2024-11-18 15:39:56 -07:00
summary = summary.split('/') # Split summary and project into 2 strings
project_name = summary[0] # Store the project name from the summary into a variable
print(f'Project: {project_name}') # Print project
else:
2024-11-20 17:47:00 -07:00
print(f'\nError: {user} not found for project {project}.\n') # Notify if user not found
2024-11-18 15:39:56 -07:00
sys.exit()
line = 0
for match in soup.find_all('td', align='right'): # Search soup for table data
line += 1
if line == 1: # Get overall rank
overallRank = match.text.lstrip()
2024-11-20 17:47:00 -07:00
if overallRank[0] != 'T' and overallRank[0] != '0':
2024-11-18 15:39:56 -07:00
overallRank = overallRank.split('(')
overallRank = overallRank[0]
if line == 2: # Get current rank
currentRank = match.text.lstrip()
2024-11-20 17:47:00 -07:00
if currentRank[0] != '0':
2024-11-18 15:39:56 -07:00
currentRank = currentRank.split('(')
currentRank = currentRank[0]
if line == 3: # Print current and overall rank
2024-11-20 17:47:00 -07:00
currentRank = f'{int(currentRank):,}'
2024-11-18 15:39:56 -07:00
print(f'Rank: {currentRank}')
2024-11-20 17:47:00 -07:00
overallRank = f'{int(overallRank):,}'
2024-11-18 15:39:56 -07:00
print(f'Overall: {overallRank}')
break
2024-11-20 17:47:00 -07:00
# Grab the date an update to rank was posted
lastUpdate = soup.find('td', class_='lastupdate').text.split()
2024-11-18 15:39:56 -07:00
lastUpdate = lastUpdate[8].lstrip()
2024-11-20 17:47:00 -07:00
# Format the date
Year = lastUpdate[7:12]
Month = lastUpdate[3:6]
Day = lastUpdate[0:2]
formattedDate = f'{Month} {Day}, {Year}'
print(f'Updated: {formattedDate}\n')
2024-11-18 10:20:57 -07:00
else:
2024-11-18 15:39:56 -07:00
print('An error has occurred while fetching data.')
2021-09-06 16:30:12 -07:00
2024-11-18 15:39:56 -07:00
if __name__ == '__main__':
main()