-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeviceKeyBatchDownload.py
More file actions
96 lines (90 loc) · 4 KB
/
Copy pathDeviceKeyBatchDownload.py
File metadata and controls
96 lines (90 loc) · 4 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
87
88
89
90
91
92
93
94
95
96
import os
import requests
import sys
import time
from colorama import Fore, Back, Style, init
init()
retry_interval = 30
request_timeout = 120
api_key = os.environ.get('REVEL_SPLIT_PACKAGE_API_KEY')
if api_key is None:
print(Fore.RED + "API Key environmental variable not found.")
print("Please create a new variable using key name: 'REVEL_SPLIT_PACKAGE_API_KEY'")
print("Close script and try again or contact support." + Fore.RESET)
close = input("Press enter to exit script")
sys.exit()
def timerAnimation(duration):
while duration > -1:
sys.stdout.write('\r' + str(duration) + " seconds remaining ")
sys.stdout.flush()
time.sleep(1)
duration -= 1
print("")
def apiRequest(url):
global request_timeout
requestIncomplete = True
while requestIncomplete:
try:
response = requests.get(url, timeout=request_timeout)
except (requests.exceptions.RequestException, requests.exceptions.Timeout) as e:
sys.stdout.write('\r')
print(Fore.RED + "Connection Error")
print("Error: " + str(e) + Fore.RESET)
print("Retrying in " + str(retry_interval) + " seconds")
timerAnimation(retry_interval)
print("\nRetrying now")
else:
if response.status_code == requests.codes.ok:
requestIncomplete = False
return response
else:
sys.stdout.write('\r')
print(Fore.RED + "Bad response code recevied")
print("Response code: " + str(response.status_code))
print("Response body:" + str(response.content) + Fore.RESET)
print("Retrying in " + str(retry_interval) + " seconds")
timerAnimation(retry_interval)
print("\nRetrying now")
print("Please provide keyword(s) to filter devices by.")
print("If the keyword(s) are found within a device name, the respective device.key file will be created.")
print("If more than one word is provided the entire text string will be matched exactly as it appears.")
target_string = input("Please enter text...")
target_string = target_string.strip()
print("Keyword text: " + target_string)
print("gettting device data...")
response = apiRequest("https://api.reveldigital.com/api/devices/?api_key=" + api_key + "&include_snap=false")
devicesJSON = response.json()
response.close()
matchFound =False
for device in devicesJSON:
deviceName = device["name"].strip()
if target_string.lower() in deviceName.lower():
if not matchFound:
print("creating device.key files for the following devices:")
matchFound = True
if os.path.isdir(deviceName):
print("")
print(Fore.YELLOW + "device folder already created for another device with matching name")
print("Skipping device name: " + deviceName)
print("Registration Key:" + device["registration_key"] + Fore.RESET)
print("")
else:
try:
os.mkdir(deviceName)
except OSError as e:
print("")
print(Fore.RED + "directory could not be created for device name: " + deviceName)
print("Registration Key:" + device["registration_key"])
print("If the device name includes an invalid character or character sequence, the OS won't allow a folder to be created with that device name.")
print("error: " + str(e) + Fore.RESET)
print("")
if os.path.isdir(deviceName):
path = os.path.join('./' + deviceName, "device.key")
deviceKey = open(path, "w")
deviceKey.write(device["encrypted_registration_key"])
deviceKey.close()
print(deviceName)
if matchFound:
print(Fore.GREEN + "device.key creation complete" + Fore.RESET)
else:
print(Fore.RED + "No device name in the account contains text that matches the provided keyword(s): " + target_string + Fore.RESET)