def simple_parser(self, res): result_date = parse(res, now=self.get_now(), allowed_results=[date]) result_time = parse(res, now=self.get_now(), allowed_results=[time]) res_tot = parse(res, now=self.get_now(), allowed_results=[datetime]) res_text = get_clear_text(res) if result_time != self.get_now_time(): return [result_time, result_date, res_text, True] else: return [result_time, result_date, res_text, False] return
def clear_posts_text_to_parse(domain=''): posts = get_wall_posts(domain=domain) for post in posts: concert = post['text'] if 'лет' in concert: clear_concert_text = concert.replace( 'лет', '') # here we reaplace word 'years' becouse parser get mistake concert_date = parse(clear_concert_text) return concert, concert_date elif '|' in concert: # we split text to create list where [1] object will be date "day(1) mounth(january)" and here we miss posts without "|" char clear_concert_text = concert.split('|') concert_date = parse(clear_concert_text[1]) return concert, concert_date
def message(update, context): eta, text = None, None mo = RE_MESSAGE.match(update.message.text) if mo: eta = arrow.get(arrow.get(mo.group(1)).datetime, tz.gettz()) text = mo.group(2) else: try: eta = arrow.get(parse(update.message.text, allowed_results=(datetime,), tz=os.environ['TZ'])) text = get_last_clear_text(update.message.text) except: logging.exception('error parsing date') logging.debug('eta: %s'%eta) logging.debug('text: %s'%text) if text: sqs.delay_message( eta = eta.to('UTC').naive, text = text, chat_id = update.effective_chat.id, reply_to_message_id = update.message.message_id, quote = True ) bot.send_message( text = _('will notify you at: %s')%eta.format(), chat_id = update.effective_chat.id, reply_to_message_id = update.message.message_id, quote = True ) else: start(update, context)
def proc_rus_date(self, s): if re.search( "[0-9]{1,2}-[0-9]{1,2}-[0-9]{4}", s): # костыль, чтобы rutimeparser понимал даты вида dd-mm-yyyy res = re.search("([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})", s) s = "{}.{}.{}".format(res.group(1), res.group(2), res.group(3)) if 'феврал' in s.lower() and re.search( "\\b29\\b", s ): # без этого костыля rutimeparser не понимает, что у февраля 29 дней if re.search("\\b[0-9]{4}\\b", s): year = int(re.search("\\b([0-9]{4})\\b", s).group(1)) else: year = 2020 d = datetime.date(year, 2, 29) else: d = parse(s, allowed_results=[datetime.date, None]) if not d: return None if re.search( "\\b[0-9]{4}\\b", s ): # костыль, потому что rutimeparser внезапно (!) неправильно видит год year = int(re.search("\\b([0-9]{4})\\b", s).group(1)) if year != d.year: d = datetime.date(year, d.month, d.day) else: now = datetime.datetime.now() if now.year != d.year: d = datetime.date(now.year, d.month, d.day) return d
def test_weekday_and_time(self): good = datetime(now.year, 4, 1, 17, 43) if good < now: good = datetime(now.year + 1, 4, 1, 17, 43) wd = datetime.weekday(good) days = 7 - (wd - 3) % 7 good = good + timedelta(days=days) src_text = 'в апреле в четверг в 17:43' with self.subTest(text=src_text): for text in make_mutations(src_text): result = parse(text, now=now) self.compare(result, good, False)
def test_weekday(self): year = now.year good = datetime(year, 4, 1).date() if good < now.date(): good = datetime(year + 1, 4, 1).date() wd = datetime.weekday(good) days = 7 - (wd - 2) % 7 good = good + timedelta(days=days) for text in make_mutations('в апреле в среду'): with self.subTest(text=text): result = parse(text, now=now) self.compare(result, good, True)
def test_delta(self): texts = ( '8 апреля у меня поезд. Напомни за день.', '9 апреля у меня поезд. Напомни за 2 дня.', '6 апреля через день', ) year = now.year good = datetime(year, 4, 7) if good < now: good = datetime(year + 1, 4, 7) for text in make_mutations(*texts): with self.subTest(text=text): result = parse(text, now=now) self.compare(result, good, True)
def import_dt(text, timezone='Europe/Moscow' ): # Функция забирает дату и время из сообщения result = parse(text, tz=timezone, now=None, remove_junk=True, allowed_results=(datetime, None), default_time=time(12, 0), default_datetime=None) dt_text = result localtz = pytz.timezone(servertz) timezone = pytz.timezone(timezone) if result is None: return 'null', 'null' if datetime.now(tz=timezone) > result: return 'old', 'old' result = result.astimezone(tz=localtz) result = ('{:%H:%M %d.%m.%Y}'.format(result)) dt_text = ('{:%H:%M %d.%m.%Y}'.format(dt_text)) return result, dt_text
def test_common(self): for src_text, good, is_date in common_tests: with self.subTest(src_text=src_text): for text in make_mutations(src_text): result = parse(text, now=now) self.compare(result, good, is_date)