/
get_data.py
119 lines (97 loc) · 3.34 KB
/
get_data.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
import json
import time
import hashlib
import hmac
import base64
import uuid
import requests
import sys
from rich.console import Console
from typing import Optional
import typer
import os
app = typer.Typer()
sys.stdout.reconfigure(encoding='utf-8')
# ------------------------------トークン取得------------------------------
def credentials():
credentials_file = "credentials.json"
if os.path.exists(credentials_file):
with open(credentials_file) as f:
credentials = json.load(f)
return credentials.get("token"), credentials.get("secret")
else:
console = Console()
console.print("Credentials file not found. Please enter your token and secret.")
token = console.input("Token: ")
secret = console.input("Secret: ")
credentials = {"token": token, "secret": secret}
with open(credentials_file, "w") as f:
json.dump(credentials, f)
return token, secret
token, secret = credentials()
# ------------------------------認証機構------------------------------
def gen_secret(secret):
return bytes(secret, 'utf-8')
def gen_sign(secret, t, nonce):
string_to_sign = '{}{}{}'.format(token, t, nonce)
string_to_sign = bytes(string_to_sign, 'utf-8')
sign = base64.b64encode(hmac.new(secret, msg=string_to_sign, digestmod=hashlib.sha256).digest())
return sign
def gen_t():
return str(int(round(time.time() * 1000)))
def gen_nonce():
return str(uuid.uuid4())
secret_bytes = gen_secret(secret)
t = gen_t()
nonce = gen_nonce()
sign = gen_sign(secret_bytes, t, nonce)
# -------------------------------データ取得-----------------------------------
def get():
get_id()
get_scene()
def get_id():
"""
Save the deviceID data as JSON in the current directory.
"""
url = 'https://api.switch-bot.com/v1.1/devices/'
headers = {
'Authorization': token,
'sign': sign,
't': t,
'nonce': nonce,
'Content-Type': 'application/json; charset=utf8'
}
response = requests.get(url, headers=headers)
# 整形して出力する jsonがなければ新規作成する
if response.status_code == 200:
id_data = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(id_data)
with open('id_data.json', 'w') as f:
json.dump(response.json(), f, indent=2, ensure_ascii=False)
print("DeviceID data saved successfully.")
else:
print(f"Error: {response.status_code} - {response.text}")
def get_scene():
"""
Save the scene data as JSON in the current directory.
"""
url = 'https://api.switch-bot.com/v1.1/scenes'
headers = {
'Authorization': token,
'sign': sign,
't': t,
'nonce': nonce,
'Content-Type': 'application/json; charset=utf8'
}
response = requests.get(url, headers=headers)
# 整形して出力する jsonがなければ新規作成する
if response.status_code == 200:
scene_data = json.dumps(response.json(), indent=2, ensure_ascii=False)
print(scene_data)
with open('scene_data.json', 'w') as f:
json.dump(response.json(), f, indent=2, ensure_ascii=False)
print("Scene data saved successfully.")
else:
print(f"Error: {response.status_code} - {response.text}")
if __name__ == '__main__':
get()