def radtherm_set_str(what, value, trace): """Set the value of a piece of string data from the thermostat""" try: if what == "uma_line0": line = 0 resource = '/uma' elif what == "uma_line1": line = 1 resource = '/uma' else: wg_error_print("radtherm_set_str", " Invalid 'what' argument " + what) return RADTHERM_STR_ERROR pman = PoolManager() encoded_body = json.dumps({"line": line, "message": value}) ret = pman.request_encode_url('POST', 'http://' + TSTAT_IP + '/tstat' + resource, headers={'Content-Type': 'application/json'}, body=encoded_body) retval = json.loads(ret.data.decode('utf-8')) if 'success' not in retval: wg_error_print("radtherm_set_str", " Unsuccessful POST request (error) of " + what) return RADTHERM_STR_ERROR except Exception: #pylint: disable=W0703 wg_error_print("radtherm_set_str", " Unsuccessful POST request (exception) of " + what) return RADTHERM_STR_ERROR return RADTHERM_STR_SUCCESS
def radtherm_set_int(what, value, trace): """Set the value of a piece of integer data from the thermostat""" try: if what in ("fmode", "tmode", "hold"): resource = "" elif what == "mode": resource = "/save_energy" elif what == "intensity": resource = "/night_light" else: wg_error_print("radtherm_set_int", " Invalid 'what' argument " + what) return RADTHERM_INT_ERROR pman = PoolManager() encoded_body = json.dumps({what: value}) ret = pman.request_encode_url('POST', 'http://' + TSTAT_IP + '/tstat' + resource, headers={'Content-Type': 'application/json'}, body=encoded_body) retval = json.loads(ret.data.decode('utf-8')) if 'success' not in retval: wg_error_print("radtherm_set_int", " Unsuccessful POST request (error) of " + what) return RADTHERM_INT_ERROR return RADTHERM_INT_SUCCESS except Exception: #pylint: disable=W0703 wg_error_print("radtherm_set_int", " Unsuccessful POST request (exception) of " + what) return RADTHERM_INT_ERROR
class _HTTP: __slots__ = ['version', 'http'] def __init__(self, version: str, pool_size: int): self.version = version self.http = PoolManager(num_pools=pool_size) def get(self, uri: str = '', params: dict = {}, headers: dict = {}) -> response: header = { 'User-Agent': 'Python3 SDK v%s' % self.version, } header.update(headers) return self.http.request_encode_url('GET', uri, fields=params, headers=header) def post(self, uri: str = '', params: dict = {}, headers: dict = {}) -> response: header = { 'User-Agent': 'Python3 SDK v%s' % self.version, } header.update(headers) return self.http.request_encode_body('POST', uri, fields=params, headers=headers, encode_multipart=False)
class Hatebu(object): http = None def __init__(self): user_agent = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0'} self.http = PoolManager(headers=user_agent) def get_rss_data(self, word, threshold): """ はてブから キーワード( = word)、ブックマーク数 (= threshold)を基に xmlデータを取得してくるメソッド """ url = urllib.request.quote("b.hatena.ne.jp/keyword/{0}?mode=rss&sort=current&threshold={1}".format(str(word), str(threshold))) response = self.http.request_encode_url('GET', url) result = response.data.decode('utf-8') return result def parse_xml_data(self, xml): """ 取得してきたXMLデータを解析して必要な情報のみを抜き出す。 """ result = [] feed = feedparser.parse(xml) """ 各ブックマークは、XMLデータのentriesタグの中にItemタグ単位で保存されている。 feed["entries"]でentriesの中から一つずつItemを取り出し、dataに格納する。 dataは、Itemタグ内のtitleやdateの情報がparse関数によってdict型に変換されて 格納されているため、data["title"]などで必要な情報が得られる。 """ for data in feed["entries"]: # hatebu_bookmarkcountの項目がない場合があるため、項目がある場合のみ取得 if "hatena_bookmarkcount" in data.keys(): tmp = dict(title=data["title"], date=data["date"], url=data["links"][0]["href"], bookmark_count=data["hatena_bookmarkcount"]) result.append(tmp) # resultにブックマークを格納した時に、取得した順と逆に格納されてしまうため、取得順になるように reversed関数で配列を逆順にしている。 return reversed(result)
def radtherm_set_float(what, value, trace): """Set the value of a piece of floating point data from the thermostat""" try: if what != "t_heat": wg_error_print("radtherm_set_float", " Invalid 'what' argument " + what) return RADTHERM_FLOAT_ERROR pman = PoolManager() encoded_body = json.dumps({what: value}) ret = pman.request_encode_url('POST', 'http://' + TSTAT_IP +'/tstat', headers={'Content-Type': 'application/json'}, body=encoded_body) retval = json.loads(ret.data.decode('utf-8')) if 'success' not in retval: wg_error_print("radtherm_set_float", " Unsuccessful POST request (error) of " + what) return RADTHERM_FLOAT_ERROR return RADTHERM_FLOAT_SUCCESS except Exception: #pylint: disable=W0703 wg_error_print("radtherm_set_float", " Unsuccessful POST request (exception) of " + what) return RADTHERM_FLOAT_ERROR