Skip to content

Instantly share code, notes, and snippets.

@poggenpower
Created January 9, 2022 16:20
Show Gist options
  • Save poggenpower/39a521f935ebaf4ec80cd9b8fdb807da to your computer and use it in GitHub Desktop.
Save poggenpower/39a521f935ebaf4ec80cd9b8fdb807da to your computer and use it in GitHub Desktop.
PyViCare crawl all values
import re
from PyViCare.PyViCare import PyViCare
# make sure call back URL in the Oauth client is set to: vicare://oauth-callback/everest
client_id = "42cfdf4e473fd436*************"
email = "[email protected]"
password = "********************"
vicare = PyViCare()
vicare.initWithCredentials(email, password, client_id, "token.save")
class Crawler():
primitive = (int, float, str, bool, type(None))
basic_iterable = (dict, list, tuple)
basic_list = (list, tuple)
def __init__(self, check_obj, check_obj_name, var_pattern, func_pattern) -> None:
self.check_obj = check_obj
self.check_obj_name = check_obj_name
self.var_pattern = var_pattern
self.func_pattern = func_pattern
def is_primitive(self, thing):
return isinstance(thing, self.primitive)
def is_basic_iterable(self, thing):
return isinstance(thing, self.basic_iterable)
def is_basic_list(self, thing):
return isinstance(thing, self.basic_list)
def get_functions(self, check_obj, pattern):
function_names = []
for func in dir(check_obj):
if re.match(pattern, func):
function_names.append(func)
return function_names
def get_vars(self, check_obj, pattern):
var_names = []
for var in check_obj.__dict__.keys():
if re.match(pattern, var):
var_names.append(var)
return var_names
def start_crawl(self):
self.crawl(self.check_obj, self.check_obj_name, hirarchy=[] )
def crawl(self, check_obj, check_obj_name, hirarchy):
hirarchy.append(check_obj_name)
if self.is_basic_iterable(check_obj):
if self.is_basic_list(check_obj):
# convert to dict with index as keys
check_obj = dict(zip(range(len(check_obj)),check_obj))
for key, value in check_obj.items():
self.crawl(value, str(key), hirarchy)
elif self.is_primitive(check_obj) or not hasattr(check_obj, '__dict__'):
print(f"{'.'.join(hirarchy)}: {check_obj}")
else:
for func_str in self.get_functions(check_obj, self.func_pattern):
print(f"{'.'.join(hirarchy)} *{func_str}")
func = getattr(check_obj, func_str)
try:
self.crawl(func(), func_str, hirarchy)
except TypeError as te:
#can't call funtion, ignore
print(f"{'.'.join(hirarchy)} *{func_str} not callable: {te}")
for var_str in self.get_vars(check_obj, self.var_pattern):
var = check_obj.__dict__[var_str]
# print(f"{'.'.join(hirarchy)}: {var_str}, {type(var)}")
self.crawl(var, var_str, hirarchy)
hirarchy.pop()
c = Crawler(vicare, 'vicare', r"^(?![_]).*", r'^(get).*')
c.start_crawl()
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment