Last active
February 2, 2025 15:17
-
-
Save frozenpandaman/da7ca936593eba993ef77b69bf98406b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib.request, csv, datetime, pytz, prettytable, lxml | |
from bs4 import BeautifulSoup | |
def main(): | |
statinkurl = "https://stat.ink/entire/users?_lang_=en-US" | |
page = urllib.request.urlopen(statinkurl).read() | |
bs = BeautifulSoup(page, "lxml") | |
table = bs.select_one("table.table-striped") | |
now = datetime.datetime.now(tz=pytz.utc) | |
now = now.astimezone(pytz.timezone('US/Hawaii')) | |
filename = str(now.year) + "-" + \ | |
str(now.month).zfill(2) + "-" + \ | |
str(now.day).zfill(2) + "_" + \ | |
str(now.hour).zfill(2) + \ | |
".txt" | |
return table, filename | |
def agents(table, filename): | |
agents = [item.text for item in table.select("th[rowspan]")] | |
battles = [item.text.replace(",", "") for item in table.find_all('td', {'class': 'text-right', 'rowspan': True})[::2]] | |
users = [item.text.replace(",", "") for item in table.find_all('td', {'class': 'text-right', 'rowspan': True})[1::2]] | |
with open(filename, "w") as f: | |
x = prettytable.PrettyTable(junction_char="|") | |
x.add_column("Agent Name", agents, align="l") | |
x.add_column("Battles", battles, align="l") | |
x.add_column("Users", users, align="l") | |
y = x.get_string() | |
z = y[y.find('\n'):y.rfind('\n')] + "\n\n" | |
now = datetime.datetime.now(tz=pytz.utc) | |
now = now.astimezone(pytz.timezone('US/Hawaii')) | |
b4 = (now - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S %Z") | |
f.write("Battles between {} and {}:\n".format(b4, now.strftime("%Y-%m-%d %H:%M:%S %Z"))) | |
f.write("via https://stat.ink/entire/users\n") | |
f.write(z) | |
def vers(table, filename): | |
versions = [item.text for item in table.find_all('th', {'scope': 'row'})] | |
battles = [item.text.replace(",", "") for item in table.find_all('td', {'class': 'text-right', 'rowspan': False})][::2] | |
users = [item.text.replace(",", "") for item in table.find_all('td', {'class': 'text-right', 'rowspan': False})][1::2] | |
for i, ver in enumerate(versions): | |
if sum(c.isalpha() for c in ver) >= len(ver) * .55 or ver == "s3s" or ver == "s3s3": # string is 55+% alphabetical, i.e. not ver # | |
battles.insert(i, "") | |
users.insert(i, "") | |
else: | |
versions[i] = " " + versions[i] | |
with open(filename, "a") as f: | |
x = prettytable.PrettyTable(junction_char="|") | |
x.add_column("Version", versions, align="l") | |
x.add_column("Battles", battles, align="l") | |
x.add_column("Users", users, align="l") | |
y = x.get_string() | |
z = y[y.find('\n'):y.rfind('\n')] | |
f.write(z) | |
if __name__=="__main__": | |
table, filename = main() | |
filename = "/home/public/s3s/" + filename | |
agents(table, filename) | |
vers(table, filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment