Files
Python/dnet-stats.py
T

98 lines
3.2 KiB
Python
Raw Normal View History

2021-09-07 09:35:02 -07:00
#!/usr/bin/python3
2021-09-06 16:30:12 -07:00
from bs4 import BeautifulSoup
import requests, sys
2021-09-06 16:30:12 -07:00
2021-10-26 17:21:38 -07:00
# Distributed.net participant search form
searchUrl = 'https://stats.distributed.net/participant/psearch.php'
# Valid Distributed.net Projects (and associated ID)
validProjects = {
2021-11-21 19:07:10 -07:00
'RC5-56' : 3,
'RC5-64' : 5,
'RC5-72' : 8,
2021-10-26 17:21:38 -07:00
'OGR-24' : 24,
'OGR-25' : 25,
'OGR-26' : 26,
'OGR-27' : 27,
'OGR-28' : 28,
}
# Help menu
def printHelp():
print(
'\nUsage: dnet-stats.py [user] [project]\n' +
2021-10-26 17:21:38 -07:00
'\nPROJECTs' +
'\n========' +
2021-11-21 19:07:10 -07:00
'\n RC5-56 ' +
'\n RC5-64 ' +
'\n RC5-72 ' +
2021-10-26 17:21:38 -07:00
'\n OGR-24 ' +
'\n OGR-25 ' +
'\n OGR-26 ' +
'\n OGR-27 ' +
'\n OGR-28 ' +
'\n========\n'
)
exit()
2021-09-06 17:36:48 -07:00
2021-10-26 17:21:38 -07:00
# If no arguments are given, display Help Menu
if len(sys.argv) <= 2:
print('\nError: Please specify user and project.')
printHelp()
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-10-26 17:21:38 -07:00
# if we have all arguments needed, proceed...
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()]
else:
2021-10-26 17:21:38 -07:00
# otherwise, let the user know their project was not valid
print('\nError: ' + sys.argv[2].upper() + ' is not a valid project.')
printHelp()
exit()
2021-10-26 17:21:38 -07:00
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
2021-09-06 16:30:12 -07:00
2021-09-06 17:36:48 -07:00
if response: # if we received a response
soup = BeautifulSoup(response.text, 'lxml') # load the response into BeautifulSoup
2021-09-06 16:30:12 -07:00
2021-09-06 17:36:48 -07:00
summary = soup.find('td',class_='htitle').text.lstrip() # find summary
2021-09-06 16:30:12 -07:00
if "Summary" in summary:
2021-10-26 17:21:38 -07:00
print('\nUser: ' + myUser) # print user
2021-09-06 17:36:48 -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
print('Project: ' + project) # print project
2021-09-06 16:30:12 -07:00
else:
2021-10-26 17:21:38 -07:00
print('\n' + myUser + ' not found for project ' + myProject + '.\n') # notify if user not found
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-09-06 17:36:48 -07:00
if line == 1: # print 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-09-06 17:36:48 -07:00
if line == 2: # print 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-09-06 17:36:48 -07:00
if line == 3: # stop searching soup
2021-10-26 17:21:38 -07:00
print ('Current Rank: ' + currentRank)
print ('Overall Rank: ' + overallRank + '\n')
2021-09-06 16:30:12 -07:00
break
else:
print('An error has occured.')