async def currency_data(args: FuncParameters) -> Optional[str]: url = f"https://www.nbrb.by/api/exrates/rates?periodicity=0" response = await args.session.get(url) if response.status != status.HTTP_200_OK: logger.warning("www.nbrb.by api call failed: %s", response) body = await response.text() logger.debug(body) return None payload = await response.json() new_dict_resp = { 'USD': 431, #145, 'EUR': 451, #292, '100 RUB': 456, #298, '100 UAH': 449, #290, '10 PLN': 452, #293, } try: for key in new_dict_resp: curr_filter = list( filter(lambda cur: cur['Cur_ID'] == new_dict_resp.get(key), payload))[-1]['Cur_OfficialRate'] new_dict_resp[key] = curr_filter except IndexError: return None else: return Translator.data_translation(loc=args.localization, data=new_dict_resp)
def language(args: FuncParameters) -> str: if args.localization == args.message.text[1:]: text = f"{vocabularies.SET_LANGUAGE[args.localization]}" else: text = f"{vocabularies.SET_LANGUAGE[set_language(args)]}" logger.debug(f"{text}") return text
async def if_bot_command(args: FuncParameters) -> Optional[str]: for attr in args.message.entities: if attr.type == "bot_command": logger.debug(f"This is a bot command - {args.message.text}") if args.message.text in VALID_BOT_COMMANDS.keys(): return await running_bot_command(args) return choice_of_greeting(args)
def recv_file(conn, num_retries=settings.MAX_RETRIES): try: consume_till_next(Constant.BEG_TX, conn) match_next(Constant.MSG_TYPE_FILE, conn) match_next(Constant.BEG_SIZE, conn) fname_size = recv_uint(conn) logger.debug("File name is {} bytes long".format(fname_size)) match_next(Constant.END_SIZE, conn) match_next(Constant.BEG_FNAME, conn) fname = conn.recv(fname_size).decode() logger.debug("Awaiting file " + fname) match_next(Constant.END_FNAME, conn) match_next(Constant.BEG_COUNT, conn) num_chunks = recv_uint(conn) num_left = num_chunks match_next(Constant.END_COUNT, conn) with open('./tmp/recv/{}'.format(fname), 'wb') as f: while num_left >= 0: f.write(recv_chunk(conn, num_retries=num_retries)) num_left -= 1 match_next(Constant.END_TX, conn) # logger.debug("Received string {}".format(pretty_print(ba))) # return bytes(ba) except LoggedException as le: le.log()
def on_measurement_received(measurement_json): logger.debug('[opentele] Measurement received: %s' % (measurement_json)) try: endpoints.process_measurement(measurement_json) except Exception, e: logger.error('[opentele] Error processing measurement: %s' % (e))
def post(self): logger.debug("[opentele] Attempting to post to OpenTele: %s" % (json.dumps(self.observation.get_payload()))) response = requests.post(self.url, auth=HTTPBasicAuth(self.credentials['user'], self.credentials['pass']), data=json.dumps(self.observation.get_payload())) return response
def perform_login(): logger.debug("[linkwatch] Getting auth token...") login_endpoint = LoginEndpoint(LINKWATCH_USER, LINKWATCH_PASSWORD) login_res = login_endpoint.post() if login_res.is_error(): logger.error("[linkwatch] Could not log demo user in. Login result: %s" % (login_res.get_error_reason())) return login_res
async def welcome_back(args: FuncParameters) -> str: last_message = get_last_message(args.message.from_.id) since = datetime.now() - timedelta(hours=3) if last_message and last_message.created_at < since: logger.debug( f"the message 'welcome back' will be sent to user: {args.message.from_.first_name}, " f"id: {args.message.from_.id}") return Translator.trl_welcome_back(loc=args.localization, data=args.message.from_.first_name)
async def handle_webhook( update: Update, client_session: ClientSession = Depends(http_client_session), ): msg = await send_message( client_session, chat_id=update.message.chat.id, text=update.json(indent=2, sort_keys=True), ) logger.debug(msg.json(indent=2, sort_keys=True))
def insert(self, query): self.query = query try: logger.debug('Running query: ' + self.query) DBHandler.cursor.execute(self.query) DBHandler.db.commit() logger.info('Query executed') except: logger.exception('Query Failed') DBHandler.db.rollback()
def execute(self, query): self.query = query try: logger.debug('Running query: ' + self.query) DBHandler.cursor.execute(self.query) result = DBHandler.cursor.fetchall() return result except: result = 'Query Failed' logger.exception('Query Failed') return result
def perform_login(): logger.debug("[linkwatch] Getting auth token...") login_endpoint = LoginEndpoint(LINKWATCH_USER, LINKWATCH_PASSWORD) login_res = login_endpoint.post() if login_res.is_error(): logger.error( "[linkwatch] Could not log demo user in. Login result: %s" % (login_res.get_error_reason())) return login_res
def __init__(self, query='use python;'): try: DBHandler.db = MySQLdb.connect(env.DB_IP, env.DB_USER, env.DB_PASSWORD, env.DB_DATABASE) logger.info('Connection created !!') DBHandler.cursor = DBHandler.db.cursor() self.query = query logger.debug('Running query: ' + self.query) DBHandler.cursor.execute(self.query) except: logger.exception('Connecetion Failed')
def send_chunk(chunk, conn, num_retries=settings.MAX_RETRIES, ab=(0, 0)): tries_left = num_retries while tries_left > 0: conn.sendall(chunk) if recv_inforeq(conn) != Constant.INFO_RECV_OK: tries_left -= 1 logger.error("Chunk {} of {} apparently received incorrectly " "({} out of {} retries left)".format( *ab, tries_left, num_retries)) else: logger.debug("Chunk {} of {} sent successfully".format(*ab)) break
def save_message(session: Session_db, data: Message) -> MessageModel: message = MessageModel( id_tg=data.message_id, author_id=data.from_.id, text=data.text, ) session.add(message) session.commit() session.refresh(message) logger.debug(f"save message: {message}") return message
def send_chunk(chunk, conn, num_retries=settings.MAX_RETRIES, ab=(0, 0)): tries_left = num_retries while tries_left > 0: conn.sendall(chunk) if recv_inforeq(conn) != Constant.INFO_RECV_OK: tries_left -= 1 logger.error( "Chunk {} of {} apparently received incorrectly " "({} out of {} retries left)" .format(*ab, tries_left, num_retries)) else: logger.debug("Chunk {} of {} sent successfully".format(*ab)) break
def general_data(self, start_word, end_word_not_including): self.start_word = start_word self.end_word_not_including = end_word_not_including data = scraping.driver.find_element_by_id('content').text #logger.info(data) value = data[data.find(self.start_word):data. find(self.end_word_not_including)] logger.info(value) logger.debug(type(value)) value = value.encode('ascii', 'ignore') logger.info(value) logger.debug(type(value)) dataProcessing.write_to_csv(value, env.CSV_FILE)
async def bitcoin_data(args: FuncParameters) -> Optional[str]: url = "https://apirone.com/api/v2/ticker?currency=btc" response = await args.session.get(url) if response.status != status.HTTP_200_OK: logger.warning("https://apirone.com api call failed: %s", response) body = await response.text() logger.debug(body) return None payload = await response.json() new_dict_resp = {'USD': payload['usd']} return Translator.data_translation(loc=args.localization, data=new_dict_resp)
def create_user(session: Session_db, data: Message) -> UserModel: user = UserModel( id_tg=data.from_.id, first_name=data.from_.first_name, is_bot=data.from_.is_bot, last_name=data.from_.last_name, username=data.from_.username, lang="ru", ) session.add(user) session.commit() session.refresh(user) logger.debug(f"created user: {user}") return user
def message_type_handler(args: FuncParameters) -> Optional[str]: message_type = { "text": choice_of_greeting, "animation": choice_of_greeting, "sticker": choice_of_greeting, } payload = "" for key, value in args.message.dict().items(): if key in message_type and value: # value == True logger.debug(f"message_type - {key}") payload = message_type[key](args) break logger.debug(f"payload - {payload if payload else 'None'}") return payload if payload else None
async def index( request: Request, client_session: ClientSession = Depends(http_client_session), ): logger.debug("handling index") webhook = await get_webhook_info(client_session) context = { "path_setup_webhook": PATH_SETUP_WEBHOOK, "path_get_users": f"{PATH_ROOT}/get_users/", "url_webhook_current": hide_webhook_token(webhook.url if webhook else "not set"), "url_webhook_new": hide_webhook_token(URL_WEBHOOK), } response = templates.TemplateResponse("index.html", {"request": request, **context}) return response
def data_translation(loc: str, data: dict) -> str: source_name = inspect.stack()[1].function # or [1][3] ensure_ascii = True if loc == 'en' else False try: vocabulary = getattr(vocabularies, source_name.upper())[loc] if not vocabulary: raise AttributeError except AttributeError: logger.debug('Err: Dictionary not found - data not translated!') return json.dumps(data, indent=2, ensure_ascii=ensure_ascii) else: translated_data = {} for key in data.keys(): if key in vocabulary: translated_data[vocabulary[key]] = data.get(key) return json.dumps(translated_data, indent=2, ensure_ascii=ensure_ascii)
def get_observation_from_measurement_json(measurement_json): logger.debug("[linkwatch] Converting measurement %s to linkwatch observation..." % (measurement_json)) t = datetime.datetime.fromtimestamp(measurement_json['timestamp']) #t = datetime.datetime.utcfromtimestamp(measurement_json['timestamp']) if measurement_json['type'] == 'weight': weight_measurement = Measurement(constants.MeasurementType.WEIGHT, measurement_json['value'], constants.UnitCode.KILOGRAM) return Observation(constants.DeviceType.WEIGHTING_SCALE, t, "TEST", measurements=[weight_measurement], comment=measurement_json['input_source']) elif measurement_json['type'] == 'heartrate': heartrate_measurement = Measurement(constants.MeasurementType.PULSE_RATE_NON_INV, measurement_json['value'], constants.UnitCode.BPM) return Observation(constants.DeviceType.BLOODPRESSURE, t, "TEST", measurements=[heartrate_measurement], comment=measurement_json['input_source']) elif measurement_json['type'] == 'steps': steps_measurement = Measurement(constants.MeasurementType.HF_STEPS, measurement_json['value'], constants.UnitCode.STEP) return Observation(constants.DeviceType.STEP_COUNTER, t, "TEST", measurements=[steps_measurement], comment=measurement_json['input_source']) raise Exception("Unsupported measurement type: %s" % (measurement_json['type']))
def process_measurement(measurement_json): logger.debug("[opentele] Processing %s measurement: %s" % (measurement_json['type'], measurement_json)) if measurement_json['type'] == 'weight': send_weight_req = SendWeight(get_credentials(), measurement_json['value'], measurement_json['timestamp']) res = send_weight_req.post() if (res.status_code < 400 ): logger.debug("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code))) else: logger.error("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code))) return elif measurement_json['type'] == 'heartrate': send_bp_req = SendBP(get_credentials(), systolic=None, diastolic=None, pulse=measurement_json['value'], timestamp = measurement_json['timestamp']) res = send_bp_req.post() if (res.status_code < 400 ): logger.debug("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code))) else: logger.error("[opentele] The result of posting %s to OpenTele: %s -- with HTTP STATUS: %s" % (measurement_json, str(res.text), str(res.status_code))) return raise Exception("Unsupported measurement type: %s" % (measurement_json['type']))
async def get_users(request: Request, password: str = Form(...), ): if password != settings.admin_password: raise HTTPException( status_code=status.HTTP_403_FORBIDDEN, detail="Invalid password", ) objects = crud.get_all_users() # !!!!!!!!!objects -> users logger.debug(f"get users: {objects}") users = [ User( id=user.id, first_name=user.first_name, is_bot=user.is_bot, last_name=user.last_name, username=user.username, ) for user in objects # List!! ] logger.debug("built users") context = { "users": users, "path_root": f"{PATH_ROOT}/", } logger.debug("built context") response = templates.TemplateResponse("get_users.html", {"request": request, **context}) return response
async def handle_webhook(update: Update, client_session: ClientSession = Depends(http_client_session), ): update_message = update.message if update.message is not None else update.edited_message user = crud.get_user_by_id(update_message.from_.id) if not user: await send_message(client_session, chat_id=update_message.chat.id, text=language_info()) user = crud.create_user(update_message) args = FuncParameters( session=client_session, message=update_message, localization=user.lang, ) msg_welcome_back = await welcome_back(args) if msg_welcome_back: await send_message(client_session, chat_id=update_message.chat.id, text=msg_welcome_back) crud.save_message(update_message) answer = await choice_of_answer(args) msg = await send_message(client_session, chat_id=update_message.chat.id, text=answer) logger.debug(msg.json(indent=2, sort_keys=True))
def get_observation_from_measurement_json(measurement_json): logger.debug( "[linkwatch] Converting measurement %s to linkwatch observation..." % (measurement_json)) t = datetime.datetime.fromtimestamp(measurement_json['timestamp']) #t = datetime.datetime.utcfromtimestamp(measurement_json['timestamp']) if measurement_json['type'] == 'weight': weight_measurement = Measurement(constants.MeasurementType.WEIGHT, measurement_json['value'], constants.UnitCode.KILOGRAM) return Observation(constants.DeviceType.WEIGHTING_SCALE, t, "TEST", measurements=[weight_measurement], comment=measurement_json['input_source']) elif measurement_json['type'] == 'heartrate': heartrate_measurement = Measurement( constants.MeasurementType.PULSE_RATE_NON_INV, measurement_json['value'], constants.UnitCode.BPM) return Observation(constants.DeviceType.BLOODPRESSURE, t, "TEST", measurements=[heartrate_measurement], comment=measurement_json['input_source']) elif measurement_json['type'] == 'steps': steps_measurement = Measurement(constants.MeasurementType.HF_STEPS, measurement_json['value'], constants.UnitCode.STEP) return Observation(constants.DeviceType.STEP_COUNTER, t, "TEST", measurements=[steps_measurement], comment=measurement_json['input_source']) raise Exception("Unsupported measurement type: %s" % (measurement_json['type']))
def recv_chunk(conn, num_retries): tries_left = num_retries while tries_left > 0: try: # Assume that the previous send failed, and skip over as much of # the stream as is required. consume_till_next(Constant.BEG_CHUNK, conn) match_next(Constant.BEG_SIZE, conn) size = recv_uint(conn) match_next(Constant.END_SIZE, conn) match_next(Constant.BEG_DATA, conn) data = conn.recv(size) match_next(Constant.END_DATA, conn) match_next(Constant.BEG_HASH, conn) actual_hash = conn.recv(settings.HASH_LEN) match_next(Constant.END_HASH, conn) match_next(Constant.END_CHUNK, conn) calc_hash = utils.get_hash(data) if (calc_hash == actual_hash): logger.debug("Chunk received, hashes match (both {})" .format(pretty_print(calc_hash))) send_info(Constant.INFO_RECV_OK, conn) return data else: tries_left -= 1 send_info(Constant.INFO_HASH_MISMATCH, conn) raise LoggedException("Hash mismatch!") except LoggedException as le: tries_left -= 1 le.log()
async def weather_data(args: FuncParameters) -> Optional[str]: url = f"https://api.openweathermap.org/data/2.5/weather?id=625144&appid={settings.open_weather_appid}&units" \ f"=metric&lang={args.localization}" response = await args.session.get(url) if response.status != status.HTTP_200_OK: logger.warning("openweathermap api call failed: %s", response) body = await response.text() logger.debug(body) return None resp_json = await response.json() resp_obj_format = WeatherData(**resp_json) new_dict_resp = { 'city': resp_obj_format.name, 'weather': resp_obj_format.weather[-1].description, 'temperature(С)': resp_obj_format.main.temp, 'feels like(C)': resp_obj_format.main.feels_like, 'humidity(%)': resp_obj_format.main.humidity, 'wind speed(m/s)': resp_obj_format.wind.speed, 'cloudiness(%)': resp_obj_format.clouds.all } return Translator.data_translation(loc=args.localization, data=new_dict_resp)
def recv_chunk(conn, num_retries): tries_left = num_retries while tries_left > 0: try: # Assume that the previous send failed, and skip over as much of # the stream as is required. consume_till_next(Constant.BEG_CHUNK, conn) match_next(Constant.BEG_SIZE, conn) size = recv_uint(conn) match_next(Constant.END_SIZE, conn) match_next(Constant.BEG_DATA, conn) data = conn.recv(size) match_next(Constant.END_DATA, conn) match_next(Constant.BEG_HASH, conn) actual_hash = conn.recv(settings.HASH_LEN) match_next(Constant.END_HASH, conn) match_next(Constant.END_CHUNK, conn) calc_hash = utils.get_hash(data) if (calc_hash == actual_hash): logger.debug("Chunk received, hashes match (both {})".format( pretty_print(calc_hash))) send_info(Constant.INFO_RECV_OK, conn) return data else: tries_left -= 1 send_info(Constant.INFO_HASH_MISMATCH, conn) raise LoggedException("Hash mismatch!") except LoggedException as le: tries_left -= 1 le.log()
data = { "chat_id": chat_id, "text": text, } result = await _call_tg_method(session, "sendMessage", json=data) message = Message.parse_obj(result) return message async def _call_tg_method(session: ClientSession, method_name: str, /, **kw): url = f"{URL_TELEGRAM_API}/{method_name}" response = await session.post(url, **kw) if response.status != status.HTTP_200_OK: logger.warning("telegram api call failed: %s", response) body = await response.text() logger.debug(body) return None payload = await response.json() if not (ok := payload.get("ok")): logger.warning("payload is not ok: %s", ok) logger.debug(payload) return None result = payload["result"] return result
def on_measurement_received(measurement_json): logger.debug('[linkwatch] Measurement received: %s' % (measurement_json)) endpoints.process_measurement(measurement_json)
def get_user_by_id(session: Session_db, user_id: int) -> UserModel: user = session.query(UserModel).filter_by(id_tg=user_id).first() logger.debug(f"get single user: {user}") return user
async def create_u(user: Message, ): user = crud.create_user(user) logger.debug(f"created user: {user}") return user