-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_latest_speedtest.py
executable file
·66 lines (53 loc) · 1.79 KB
/
get_latest_speedtest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# filename: get_latest_speedtest.py
"""Get latest binaries for Ookla Speedtest CLI"""
__author__ = "Jugal Kishore <[email protected]>"
# Import Libraries
import json
import sys
import time
from bs4 import BeautifulSoup
import requests
def print_it(string) -> None:
"""Function to print to stdout"""
# If --silent argument is provided, skip printing most of the stuff
if "--silent" not in sys.argv[1:]:
print(string)
# Get HTML from speedtest.net website
start_time = time.time()
print_it("Getting HTML from speedtest.net website...")
data = requests.get("https://www.speedtest.net/apps/cli", timeout=30)
print_it(f"HTTP Status Code: {data.status_code}")
if data.status_code != 200:
print_it("Fatal: Error getting HTML file from: https://www.speedtest.net/apps/cli")
print_it("Exiting!")
sys.exit(1)
else:
print_it("200 HTTP Status Code, continuing...")
print_it("")
# Souping
soup = BeautifulSoup(data.text, "html.parser")
# Get links for Linux
linux_links = soup.find(id="linux-flyout")
linux_url = {}
for link in linux_links:
tag = link.find("a")
linux_url[tag.string] = tag.get("href")
# Get links for macOS & Windows
other_links = soup.find_all(
"a", {"class": "btn btn-sm btn-outline btn-full-width"})
other_url = {}
for link in other_links:
OS = link.text.split(" ")[-1]
other_url[OS] = link.get("href")
# Get links for FreeBSD
freebsd_links = soup.find(id="freebsd-flyout")
freebsd_url = {}
for link in freebsd_links:
tag = link.find("a")
freebsd_url[tag.string] = tag.get("href")
all_links = {"linux": linux_url, "others": other_url, "freebsd": freebsd_url}
print(json.dumps(all_links, indent=4))
execution_time = round(time.time() - start_time, 2)
print_it(f"Execution time: {execution_time} seconds")