-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
167 lines (119 loc) · 4.25 KB
/
index.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
159
160
161
162
163
164
165
166
167
import json
import hashlib
import requests
from datetime import datetime
from random_user_agent.user_agent import UserAgent
USER_AGENT = UserAgent(
software_names=["chrome", "firefox"],
operating_systems=["windows", "macos"],
popularity=["very common"],
software_types=["browser -> web-browser"],
limit=1,
)
CONFIG_FILE = "config.json"
TEST_ENDPOINT = "https://www.foxesscloud.com/c/v0/plant/status/all"
AUTH_ENDPOINT = "https://www.foxesscloud.com/c/v0/user/login"
DATA_ENDPOINT = "https://www.foxesscloud.com/c/v0/device/history/raw"
def get_headers():
user_agent = USER_AGENT.get_random_user_agent()
return {
"User-Agent": user_agent,
"Accept": "application/json, text/plain, */*",
"lang": "en",
"Referer": "https://www.foxesscloud.com/",
}
# Load and validate secrets from config.json
def load_config():
try:
with open(CONFIG_FILE) as cf:
config = json.load(cf)
if config["username"] and config["password"] and config["device_id"]:
return config
except:
return None
# Load data variables from config.json
def load_variables():
try:
with open(CONFIG_FILE) as vf:
vars = json.load(vf)["data_variables"]
selected = []
for var in vars:
if vars[var] == 1:
selected.append(var)
if len(selected) > 0:
return selected
except:
return None
# Cache a currently valid token to reduce later requests
def cache_token(token):
with open(CONFIG_FILE) as cfr:
config = json.load(cfr)
config["token"] = token
with open(CONFIG_FILE, "w") as cfw:
json.dump(config, cfw, indent=2)
# Load the cached token
def load_cached_token():
with open(CONFIG_FILE) as cf:
return json.load(cf)["token"]
# Test the token with a light request
def test_token(headers):
test_request = requests.get(TEST_ENDPOINT, headers=headers)
if test_request.text is None:
return False
res = test_request.json()
if res["result"] and res["errno"] == 0:
return True
return False
# Authenticate and recieve token for further requests
def auth(username, password, headers):
auth_payload = f"user={username}&password={password}"
auth_request = requests.post(AUTH_ENDPOINT, auth_payload, headers=headers)
if auth_request.text is None:
return print("Unable to authenticate - no data recieved")
res = auth_request.json()
if res["result"] is None:
if res["errno"] == 41807:
print(f"Unable to authenticate - bad username or password.")
elif res["errno"] != 0:
print(f"Unable to authenticate - {res}")
else:
print(f"Error communicating with API - {res}")
return
if auth_request.status_code != 200:
return print(f"Error ${auth_request.status_code}")
return res["result"]["token"]
# Get the actual data from the device
def get_data(device_id, headers, variables):
now = datetime.now()
payload = f'{{"deviceID":"{device_id}","variables":{variables},"timespan":"hour","beginDate":{{"year":{now.year},"month":{now.month},"day":{now.day},"hour":{now.hour}}}}}'
data_request = requests.post(DATA_ENDPOINT, payload, headers=headers)
if data_request.text is None:
return print("No device data recieved")
res = data_request.json()
if res["result"] is None or res["errno"] != 0:
return print("Error fetching data")
return res["result"][0]
def main():
config = load_config()
variables = load_variables()
if not config:
return print("config.json is missing required properties")
if not variables:
return print("data_variables.json is missing required properties")
username = config["username"]
password = hashlib.md5(config["password"].encode()).hexdigest()
device_id = config["device_id"]
headers = get_headers()
token = load_cached_token()
if not test_token(headers | {"token": token}):
token = auth(username, password, headers)
cache_token(token)
if not token:
return
data = get_data(
device_id,
headers | {"token": token},
str(variables).replace("'", '"').replace(" ", ""),
)
return data
print(main())