Files

110 lines
3.6 KiB
Python
Raw Permalink 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
}
# Normalize valid project names for case-insensitive lookup
normalizedProjects = {k.lower(): v for k, v in validProjects.items()}
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, help='Project name (case-insensitive).')
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_input = args.project.strip().lower()
# Validate project name (case-insensitive)
if project_input not in normalizedProjects:
print(f"\nError: Invalid project '{args.project}'. Valid options are:")
for p in validProjects.keys():
print(f" - {p}")
sys.exit(1)
2024-11-18 15:39:56 -07:00
# Get project ID
projectId = normalizedProjects[project_input]
project = [k for k, v in validProjects.items() if v == projectId][0] # Get the canonical name
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)
if response:
soup = BeautifulSoup(response.text, 'lxml')
2024-11-18 10:20:57 -07:00
summary = soup.find('td', class_='htitle').text.lstrip()
2024-11-18 15:39:56 -07:00
2024-11-20 17:47:00 -07:00
if 'Summary' in summary:
print(f'\nUser: {user}')
summary = ' '.join(summary.split())
summary = summary.split('/')
project_name = summary[0]
print(f'Project: {project_name}')
2024-11-18 15:39:56 -07:00
else:
print(f'\nError: {user} not found for project {project}.\n')
2024-11-18 15:39:56 -07:00
sys.exit()
line = 0
for match in soup.find_all('td', align='right'):
2024-11-18 15:39:56 -07:00
line += 1
if line == 1:
2024-11-18 15:39:56 -07:00
overallRank = match.text.lstrip()
2024-11-20 17:47:00 -07:00
if overallRank[0] != 'T' and overallRank[0] != '0':
overallRank = overallRank.split('(')[0]
if line == 2:
2024-11-18 15:39:56 -07:00
currentRank = match.text.lstrip()
2024-11-20 17:47:00 -07:00
if currentRank[0] != '0':
currentRank = currentRank.split('(')[0]
if line == 3:
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
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
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()