-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (63 loc) · 2.6 KB
/
Copy pathmain.py
File metadata and controls
87 lines (63 loc) · 2.6 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
### Code written by Notchie
import time
import requests
from datetime import datetime
from bs4 import BeautifulSoup
timecheckcooldown=0
difference=0
#handles the apparrent difference between the unix time and the time reported by the site
def timestamp(forceupdate=False):
now=datetime.timestamp(datetime.now())
global timecheckcooldown
global difference
if timecheckcooldown==0 or forceupdate == True:
print("ping")
r=requests.get("https://gcpdot.com/gcpindex.php")
soup=BeautifulSoup(r.text,"html.parser")
servertime=int(soup.find("servertime").get_text())
difference=now-servertime
timecheckcooldown=5
else:
timecheckcooldown =-1
difference
return now - difference
return datetime.timestamp(datetime.now())-difference
#the main dot class
class Dot():
def __init__(self) -> None:
self.color=None #not yet implemented
self.decimal=0
self.percentage=self.decimal*100
self.currentdict={}
self.lastupdated=0
#updates the values, this allows for them to be used without spamming the api
def update(self, forceupdate=False):
now=round(timestamp(forceupdate=forceupdate))
#check for when the values were last updated
if now-self.lastupdated>60 or forceupdate==True:
self.lastupdated=round(timestamp(forceupdate=forceupdate))
#captures the current data provided by the site and formats it into a dictionary
#note that the site updates every minute(i think)
with requests.get("https://gcpdot.com/gcpindex.php") as r:
print("ping")#display when a web request is made
soup=BeautifulSoup(r.text,"html.parser")
for s in soup.find_all("s"):
t=int(s.get("t"))
self.currentdict[t]=s.get_text()
#this section is needed incase the websites updates are out of sync with the code
if now in self.currentdict:
self.decimal=float(self.currentdict[now])
unroundedpercentage=float(self.decimal)*100
self.percentage=round(unroundedpercentage,5)
else:
#this will force an update, hopefully pushing the program back in line with the server
if forceupdate==False:
print("trying to force an update")
self.update(forceupdate=True)
else:
print("already forcing update, theres not much i can do")
dot=Dot()
for i in range(1,120):
dot.update()
print(dot.percentage)
time.sleep(1)