-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPingVFS.py
executable file
·158 lines (127 loc) · 4.94 KB
/
PingVFS.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
import os
import sys
import time
import json
import requests
import subprocess
from playsound import playsound
from urllib.parse import urlencode, quote_plus
from datetime import datetime, timedelta
class PingVFS:
# default constructor
def __init__(self, params):
self.url = params["url"]
self.urlparams = params["urlparams"]
self.paths = params["paths"]
self.auth = ""
self.start_time = datetime.now()
self.sound = params["sound"]
def get_auth_token(self):
if not os.path.isfile(self.paths["auth"]):
return False
path = os.path.realpath(self.paths["auth"])
read = open(path, "r")
auth = read.read().replace("\n", " ")
read.close()
if auth == self.auth:
return self.auth
self.auth = auth
# os.remove(path)
return self.auth
def store_output(self, output):
# Saving the output in result.txt.
with open(self.paths["output"], "a") as file_object:
# Append 'hello' at the end of file
file_object.write(output)
def get_foramtted_date(self, date):
return datetime.strptime(str(date), "%Y-%m-%d").strftime("%d/%m/%Y")
def hit_vfs(self):
headers = {
"Content-length" : "0",
"Content-type" : "application/json",
}
headers["Authorization"] = self.auth
from_date = datetime.now().date() + timedelta(days = 1)
to_date = from_date + timedelta(days = 90)
self.urlparams["fromDate"] = str(self.get_foramtted_date(from_date))
self.urlparams["toDate"] = str(self.get_foramtted_date(to_date))
url = self.url \
+ "?" \
+ urlencode(self.urlparams, quote_via=quote_plus)
try:
resp = requests.get(url, headers=headers)
except:
return "ERROR Connection Refused"
if os.path.isfile(self.paths["auth"]):
self.get_auth_token()
try:
resp = resp.json()
except:
return "ERROR " + str(resp.status_code)
return json.dumps(resp)
def init(self):
auth = self.get_auth_token()
count = 0
print("""
██ ██ ███████ ███████ ███████ ██ ██████ ████████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ █████ ███████ ███████ ██ ██ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████ ██ ███████ ███████ ███████ ██████ ██ ███████ ██ ██ ██""")
print("\n")
print("Started at:", end =" ")
print(datetime.now())
print("Trying to access VFS appointment API for slots...")
print("\n")
while True:
time.sleep(5)
request = self.hit_vfs()
count += 1
output = "\nOutput: " \
+ str(request) \
+ "\nTime: " \
+ str(datetime.now()) \
+ "\nCount: " \
+ str(count) \
+ "\n==="
self.store_output(output)
# Printing the output in terminal.
# print(output)
print(".", end="", flush=True),
if request == "[[]]":
continue
try:
request = json.loads(request)
except:
continue
try:
if request[0]["counters"] != None:
break
except:
continue
subprocess.call([
"/usr/bin/notify-send",
"VFS Slots!!!",
"Something Positive May Have Happend."
])
print("\n")
print(str(count) + " times HTTP 200 response received.")
print("Ended at:", end =" ")
print(datetime.now())
time_diff = datetime.now() - self.start_time
time_diff = time_diff.total_seconds() / 60.0
print("Script ran for " + str(time_diff) + " minutes")
# Will play the alert sound.
playsound(self.sound)
def main(params):
if not os.path.isfile(params):
return False
path = os.path.realpath(params)
read = open(path, "r")
params = json.loads(read.read())
read.close()
ping = PingVFS(params)
ping.init()
if __name__ == "__main__":
main("./ping_creds.json")