def word_to_date(word, previous_word): if re.match(r'[0-9]{2,4}-[0-9]{2}-[0-9]{2}', word): return parse(word, dayfirst=True) if re.match(r'[0-9]{2}.[0-9]{2}.[0-9]{2,4}', word): return parse(word, dayfirst=True) if word == 'неделя': if previous_word == 'через': return datetime.now() + timedelta(days=7) if word == 'послезавтра': return datetime.now() + timedelta(days=2) if word == 'сегодня': return datetime.now() if word == 'завтра': if previous_word == 'после': return datetime.now() + timedelta(days=2) return datetime.now() + timedelta(days=1) temp = to_int(word) if temp: return temp temp = month_number(word) if temp: return temp return False
def month_title(num, genitive=False): titles = MONTH_TITLES if genitive: titles = MONTH_TITLES_GENITIVE return titles.get(str(to_int(num)))
def _process_credit(self, bot): if self.description is None: self.description = bot.text bot.send(bot.t('CREDIT_ENTER_ZIP')) elif self.zip_code is None: self.zip_code = bot.text bot.send(bot.t('CREDIT_ENTER_REG_SERVICE')) elif self.reg_service is None: self.reg_service = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_ID')) elif self.options.get('loan_id') is None: self.options['loan_id'] = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_AMOUNT')) elif self.options.get('loan_amount') is None: self.options['loan_amount'] = utils.to_int(bot.text, None) if self.options['loan_amount'] is None: return bot.send(bot.t('ENTER_NUMBER')) bot.send(bot.t('CREDIT_ENTER_INTEREST_RATE')) elif self.options.get('interest_rate') is None: self.options['interest_rate'] = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_LENGTH')) elif self.options.get('loan_length') is None: self.options['loan_length'] = bot.text bot.send(bot.t('CREDIT_ENTER_LOAN_STATUS'), reply_markup=ReplyKeyboard([[bot.t('LOAN_STATUS_EARLY')], [bot.t('LOAN_STATUS_NORMAL')], [bot.t('LOAN_STATUS_LATE')]])) elif self.options.get('loan_status') is None: self.options['loan_status'] = bot.text bot.send(bot.t('CREDIT_ENTER_SELLERS_WARRANTY'), reply_markup=ReplyKeyboard([[ bot.t('WARRANTY_FULL'), bot.t('WARRANTY_PARTLY'), bot.t('WARRANTY_NONE') ]], one_time_keyboard=True)) elif self.options.get('sellers_warranty') is None: self.options['sellers_warranty'] = bot.text bot.send(bot.t('CREDIT_ENTER_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.price is None: self.price = utils.to_int(bot.text, None) if self.price is None: return bot.send(bot.t('ENTER_NUMBER')) self._create_contract_send(bot) else: self._create_contract_process(bot)
def flight_cost(flight): departure = flight.get('departure') if not isinstance(departure, datetime): departure = parse(departure) arrival = flight.get('arrival') if not isinstance(arrival, datetime): arrival = parse(arrival) time = arrival - departure price_per_second = 0.508 return to_int(price_per_second * time.seconds)
async def before_hook(self): self.user = await self.store.db.users.find_one( {'external': self.msg.unique_key}) if self.user is None: self.user = { 'external': self.msg.unique_key, 'last_use': to_int(datetime.now().timestamp()), 'state': self.state } if not self.user.get('name') and not self.user.get('surname'): if self.is_telegram: msg_from = self.msg.extra.get('from') self.user['name'] = msg_from.get('first_name') self.user['surname'] = msg_from.get('last_name') elif self.is_vk: vk = await self.vk_user_get(self.msg.from_id) if vk: self.user['name'] = vk.get('first_name') self.user['surname'] = vk.get('last_name')
def _process_factoring(self, bot): if self.description is None: self.description = bot.text bot.send(bot.t('FACTORING_ENTER_ZIP')) elif self.zip_code is None: self.zip_code = bot.text bot.send(bot.t('FACTORING_ENTER_REG_SERVICE')) elif self.reg_service is None: self.reg_service = bot.text bot.send(bot.t('FACTORING_ENTER_LOAN_ID')) elif self.options.get('loan_id') is None: self.options['loan_id'] = bot.text bot.send(bot.t('FACTORING_PAY_REQS'), reply_markup=ReplyKeyboard([ [bot.t('FACTORING_REGRESS')], [bot.t('FACTORING_NO_REGRESS')], ], one_time_keyboard=True)) elif self.options.get('pay_reqs') is None: self.options['pay_reqs'] = bot.text bot.send(bot.t('FACTORING_TITLE_SUPPLIER'), reply_markup=ReplyKeyboardHide()) elif self.options.get('title_supplier') is None: self.options['title_supplier'] = bot.text bot.send(bot.t('FACTORING_SUM_REQS')) elif self.options.get('sum_reqs') is None: self.options['sum_reqs'] = bot.text bot.send(bot.t('FACTORING_DATE_REQS_PAY')) elif self.options.get('date_reqs_pay') is None: self.options['date_reqs_pay'] = bot.text bot.send(bot.t('FACTORING_PRICE')) elif self.price is None: self.price = utils.to_int(bot.text, None) if self.price is None: return bot.send(bot.t('ENTER_NUMBER')) self._create_contract_send(bot) else: self._create_contract_process(bot)
def _process_futures(self, bot): if self.description is None: self.description = bot.text bot.send(bot.t('FUTURES_ENTER_ZIP')) elif self.zip_code is None: self.zip_code = bot.text bot.send(bot.t('FUTURES_ENTER_REG_SERVICE')) elif self.reg_service is None: self.reg_service = bot.text bot.send(bot.t('FUTURES_ENTER_LOAN_ID')) elif self.options.get('loan_id') is None: self.options['loan_id'] = bot.text bot.send(bot.t('FUTURES_CHOOSE_CONTRACT_TYPE'), reply_markup=ReplyKeyboard([ [bot.t('FUTURES_TYPE_SETTLEMENT')], [bot.t('FUTURES_TYPE_DELIVERABLE')], ], one_time_keyboard=True)) elif self.options.get('contract_type') is None: self.options['contract_type'] = bot.text bot.send(bot.t('FUTURES_ENTER_CONTRACT_SIZE'), reply_markup=ReplyKeyboardHide()) elif self.options.get('contract_size') is None: self.options['contract_size'] = bot.text bot.send(bot.t('FUTURES_CONTRACT_MATURITY')) elif self.options.get('contract_maturity') is None: self.options['contract_maturity'] = bot.text bot.send(bot.t('FUTURES_ENTER_DELIVERY_DATE')) elif self.options.get('delivery_date') is None: self.options['delivery_date'] = bot.text bot.send(bot.t('FUTURES_PRICE')) elif self.price is None: self.price = utils.to_int(bot.text, None) if self.price is None: return bot.send(bot.t('ENTER_NUMBER')) self._create_contract_send(bot) else: self._create_contract_process(bot)
def month_number(name): months = dict() months.update(dict((v, k) for k, v in MONTH_TITLES.items())) months.update(dict((v, k) for k, v in MONTH_TITLES_GENITIVE.items())) return to_int(months.get(name))
def cv(text): result = {} count = 0 sn_pos = None n_pos = None pat_pos = None text = tokenize(text, disable_normal=True) sex_points = { 'm': 0, 'f': 0, } def update_sex_points(tag): if 'masc' in tag: sex_points['m'] += 1 if 'femn' in tag: sex_points['f'] += 1 def get_tag(word): return morph.tag(word)[0] for word in text: try: tag = get_tag(word) if len(word) == 10 and to_int(word, 0) > 0: result['passport'] = word if 'Surn' in tag: sn_pos = count result['surname'] = word.title() update_sex_points(tag) if 'Name' in tag: n_pos = count result['name'] = word.title() update_sex_points(tag) if 'Patr' in tag: pat_pos = count result['patronymic'] = word.title() update_sex_points(tag) try: birthday = parse(word, dayfirst=True) if isinstance(birthday, datetime): result['birthday'] = birthday.strftime('%d.%m.%Y') except: pass except Exception as e: print(e) count += 1 if n_pos is None and sn_pos is not None and pat_pos is not None: if sn_pos < pat_pos and pat_pos - sn_pos == 2 and sn_pos + 1 < len(text): n_pos = sn_pos + 1 elif sn_pos > pat_pos and sn_pos - pat_pos == 1 and pat_pos > 0: n_pos = pat_pos - 1 if n_pos is not None: result['name'] = text[pat_pos - 1].title() update_sex_points(get_tag(result['name'])) if sn_pos is None and n_pos is not None and pat_pos is not None: if n_pos < pat_pos and pat_pos - n_pos == 1: sn_pos = n_pos - 1 if n_pos > 0 else pat_pos + 1 result['surname'] = text[sn_pos].title() update_sex_points(get_tag(result['surname'])) if sex_points['m'] != sex_points['f']: if sex_points['m'] > sex_points['f']: result['sex'] = 'm' else: result['sex'] = 'f' return result
def parse_option_text(self, option, default=0): if self.is_telegram: if option.startswith('/'): return to_int(option[1:], default) if self.is_vk: return to_int(option, default)
def _on_enter_amount(cls, **kwargs): text = kwargs.get('text') amount = utils.to_int(text, None) request = kwargs.get('request') if amount is None: cls.send(chat_id=kwargs.get('chat_id'), text=u'Введите число') else: request.amount = amount request.step = awa_client.STEP_CONFIRM request.save() keyboard = ReplyKeyboard(keyboard=[[ Text.format('CL_CONFIRM_YES'), Text.format('CL_CONFIRM_NO') ]]) currencies = {} for item in Currency.objects: currencies[item.code.upper() + '_' + item.direct] = float( item.value) if request.type == Request.TYPE_BUY_USD: course = str(currencies.get('USD_buy', 0)) + u' руб/$' operation = [ u'покупаете', cls.num_split(request.amount), u'$', course, cls.num_split(request.amount * currencies.get('USD_buy', 0)) ] elif request.type == Request.TYPE_SAIL_USD: course = str(currencies.get('USD_sale', 0)) + u' руб/$' operation = [ u'продаете', cls.num_split(request.amount), u'$', course, cls.num_split(request.amount * currencies.get('USD_sale', 0)) ] elif request.type == Request.TYPE_BUY_EUR: course = str(currencies.get('EUR_buy', 0)) + u' руб/€' operation = [ u'покупаете', cls.num_split(request.amount), u'€', course, cls.num_split(request.amount * currencies.get('EUR_buy', 0)) ] elif request.type == Request.TYPE_SAIL_EUR: course = str(currencies.get('EUR_sale', 0)) + u' руб/€' operation = [ u'продаете', cls.num_split(request.amount), u'€', course, cls.num_split(request.amount * currencies.get('EUR_sale', 0)) ] elif request.type == Request.TYPE_BUY_GBR: course = str(currencies.get('GBR_buy', 0)) + u' руб/' + GBR operation = [ u'покупаете', cls.num_split(request.amount), GBR, course, cls.num_split(request.amount * currencies.get('GBR_buy', 0)) ] elif request.type == Request.TYPE_SAIL_GBR: course = str(currencies.get('GBR_sale', 0)) + u' руб/' + GBR operation = [ u'продаете', cls.num_split(request.amount), GBR, course, cls.num_split(request.amount * currencies.get('GBR_sale', 0)) ] elif request.type == Request.TYPE_BUY_CHF: course = str(currencies.get('CHF_buy', 0)) + u' руб/' + CHF operation = [ u'покупаете', cls.num_split(request.amount), CHF, course, cls.num_split(request.amount * currencies.get('CHF_buy', 0)) ] elif request.type == Request.TYPE_SAIL_CHF: course = str(currencies.get('CHF_sale', 0)) + u' руб/' + CHF operation = [ u'продаете', cls.num_split(request.amount), CHF, course, cls.num_split(request.amount * currencies.get('CHF_sale', 0)) ] else: return cls.send(chat_id=kwargs.get('chat_id'), text=Text.format('CL_CONFIRM', *operation), reply_markup=keyboard.to_json())
def _buying(self, **kwargs): buy_keyboard = ReplyKeyboard([[ self.t('DEAL_BUY_PREV'), self.t('DEAL_BUY_NEW_SEARCH'), self.t('DEAL_BUY_NEXT'), ]], one_time_keyboard=False) if kwargs.get('callback_query'): try: offer = Offer.objects.get(id=kwargs.get('callback_query')) except Exception: return deal = Deal() deal.user = self.user deal.offer = offer deal.save() self.user.buy_step = User.STEP_BLOCKCHAIN self.user.current_deal = deal self.user.save() self.send(self.t('BUY_YOU_CREATE_CONTRACT'), reply_markup=ReplyKeyboard(keyboard=[[ self.t('YES_APPROVE'), self.t('NO_FEAR'), self.t('WHAT_IS_BLOCKCHAIN'), ]])) self.answer_callback_query( callback_query_id=kwargs.get('callback_query_id')) return if self.user.buy_step == User.STEP_BLOCKCHAIN: if self.match_command(self.t('YES_APPROVE')): self.send(self.t('REGISTER_BC_BEGIN'), reply_markup=ReplyKeyboardHide()) yield gen.sleep(5) self.user.current_deal.bc_hash = PasswordHelper.get_hash( datetime.now().isoformat()) self.user.current_deal.user = self.user.current_deal.user self.user.current_deal.save() return self._on_start(welcome_text=self.t('BEGIN_DEAL'), keyboard=ReplyKeyboard( [[self.t('THNX_UNDERSTAND')]])) elif self.match_command(self.t('NO_FEAR')): return self._on_start( welcome_text=self.t('FEAR_BLOCKCHAIN_WIKI')) elif self.match_command(self.t('WHAT_IS_BLOCKCHAIN')): return self.send(self.t('WHAT_IS_BLOCKCHAIN_WIKI')) offer_type = self.match_command( self.t(['BUY_CREDIT', 'BUY_FUTURES', 'BUY_FACTORING'])) if self.user.filter_buy_type is None: if not offer_type: return self.send(self.t('ENTER_CORRECT_CONTRACT_TYPE')) self.user.set_filter_buy_type(self, offer_type.get('command')) self.user.save() if self.user.filter_buy_type == Offer.TYPE_CREDIT: return self.send(self.t('DEAL_BUY_CREDIT_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.user.filter_buy_type == Offer.TYPE_FUTURES: return self.send(self.t('DEAL_BUY_FUTURES_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.user.filter_buy_type == Offer.TYPE_FACTORING: return self.send(self.t('DEAL_BUY_FACTORING_PRICE'), reply_markup=ReplyKeyboardHide()) elif self.user.filter_price is None: self.user.filter_price = utils.to_int(self.text, None) self.user.save() if self.user.filter_price is None: return self.send(self.t('ENTER_NUMBER')) return self.send(self.t('DEAL_BUY_MANUAL'), reply_markup=buy_keyboard) if self.match_command(self.t('DEAL_BUY_PREV')): self._prev_item(**kwargs) elif self.match_command(self.t('DEAL_BUY_NEW_SEARCH')): self.user.reset() self.user.save() self.text = self.t('DEAL_BUY') self._on_hook(kwargs) elif self.match_command(self.t('DEAL_BUY_NEXT')): self._next_item(**kwargs)
def _confirm(cls, data=None, request=None, exchange=None, external_id=None): if data is None: data = {} if exchange is None and request is None and external_id is None: number = utils.to_int(data.get('args')[0], -1) if number < 0: cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_BAD_NUMBER'), reply_markup=ReplyKeyboardHide().to_json() ) return try: external_id = utils.to_int(data.get('args')[1], -1) if external_id < 0: raise Exception except: cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_BAD_EXTERNAL_ID'), reply_markup=ReplyKeyboardHide().to_json() ) return try: request = Request.objects(number=number).get() except: cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_REQUEST_NOT_FOUND'), reply_markup=ReplyKeyboardHide().to_json() ) return if request.fail_sended: cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_REQUEST_TIMEOUT'), reply_markup=ReplyKeyboardHide().to_json() ) return exchange = data['exchange'] if request.exchange is None: request.exchange = exchange request.step = STEP_SEARCH_SUCCESS request.external_id = external_id try: request.distance = vincenty(request.geo.get('coordinates'), exchange.geo.get('coordinates')).meters / 1000.0 except: pass request.user = request.user request.save() cls.send( chat_id=exchange.out_id, text=Text.format('EX_SUCCESS', external_id), reply_markup=ReplyKeyboardHide().to_json() ) from bots.awa_client import AwaClientBot AwaClientBot.send( chat_id=request.user.out_id, text=Text.format('CL_SUCCESS_RESERV', exchange.address, exchange.phone, request.external_id) ) AwaClientBot.sendLocation( chat_id=request.user.out_id, latitude=exchange.geo.get('coordinates')[0], longitude=exchange.geo.get('coordinates')[1] ) time = datetime.datetime.now() time += datetime.timedelta(seconds=Interval.seconds('reserve_time')) AwaClientBot.send( chat_id=request.user.out_id, text=Text.format('CL_SUCCESS_WARNING', time.strftime('%d.%m.%Y %H:%M')) ) else: cls.send( chat_id=data.get('chat_id'), text=Text.format('EX_ALREADY_SEND'), reply_markup=ReplyKeyboardHide().to_json() )