-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (32 loc) · 1.2 KB
/
main.py
File metadata and controls
44 lines (32 loc) · 1.2 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
import datetime
import logging
import requests
from requests.auth import HTTPDigestAuth
import yaml
logger = logging.getLogger(__name__)
def get_time(endpoint, auth):
uri = endpoint + "/cgi-bin/global.cgi?action=getCurrentTime"
r = requests.get(uri, auth=auth, timeout=10)
if r.status_code != 200:
logging.error(f"Status Code for {uri}: {r.status_code}")
else:
logging.info(f"Camera time is: {(r.text).strip()}")
def set_time(endpoint, auth):
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
uri = endpoint + "/cgi-bin/global.cgi?action=setCurrentTime&time=" + now
r = requests.get(uri, auth=auth, timeout=10)
if r.status_code != 200:
logging.error(f"Status Code for {endpoint}: {r.status_code}")
else:
logging.info(f"Set {endpoint} to {now}")
def main():
logging.basicConfig(level=logging.INFO)
with open("config.yml", encoding="utf-8") as f:
config = yaml.safe_load(f.read())
auth = HTTPDigestAuth(config["api_user"], config["api_password"])
for endpoint in config["endpoints"]:
get_time(endpoint, auth)
set_time(endpoint, auth)
get_time(endpoint, auth)
if __name__ == "__main__":
main()