Exemplo n.º 1
0
def clear_definitions(text):  # todo: move this to some `lib`
    # удаление лишнего:
    text = text.replace('{µ(Q)}', '')
    text = re.sub('<!--(.+?)-->', '', text)
    text = re.sub('<math>[^<>]+</math>', '', text)
    text = re.sub('{{пример({{[^}]+\}\}|[^\}])*\}\}', '', text)
    text = re.sub('{{семантика({{[^}]+\}\}|[^\}])+\}\}', '', text)
    text = re.sub('{{списки семантических связей\}\}', '', text)

    text = re.sub('^\s*{{прото\|([^}]+)\}\}',
                  'общее прототипическое значение: \\1', text)

    text = text.replace('<sub>2</sub>', '₂')
    text = text.replace('<sub>3</sub>', '₃')
    text = text.replace('<sub>4</sub>', '₄')

    # замена шаблонов:
    text = re.sub('{{итп\}\}', 'и т.п.', text)
    text = re.sub('{{итд\}\}', 'и т.д.', text)
    text = re.sub('\|[a-z_]+\}\}', '}}', text)

    # замена всех шаблонов {{...}} на {...}:
    text = text.replace('{{', '{').replace('}}', '}')

    # замены шаблонов:
    text = re.sub('{=\|([^|}]+)\}', 'то же, что \\1', text)
    text = re.sub('{=\|([^|}]+)\|([^|}]+)\}', 'то же, что \\1; \\2', text)
    text = re.sub('\s*{-\}\s*', ' — ', text)

    # ссылки [[...|...]] и [[...]]:
    text = re.sub('\[\[[^][]*\|([^][]*)\]\]', '\\1', text)
    text = re.sub('\[\[([^][]*)\]\]', '\\1', text)

    text = text.replace('<', '&lt;').replace('>', '&gt;')
    text = text.replace('&nbsp;', ' ')

    # пометы:
    text = text.replace('{п.}', '{перен.}')
    text = text.replace('{помета.}', '')

    # пометы курсивом:
    text = re.sub('{([ .\w-]+\.)\}(,?)', '<i>\\1\\2</i> ', text)
    text = re.sub("''([^']+?)''", '<i>\\1</i> ', text)

    # замены шаблонов:
    text = text.replace('{as ru}', '<i>(аналогично русскому слову)</i>')

    tpls = json_load(tpls_filename)
    for tpl, replace in tpls.items():
        text = re.sub(replace_tpl(tpl), replace_result(replace), text)

    return text.strip()
Exemplo n.º 2
0
def send_daily_word():
    bot = telegram.Bot(conf.telegram_token)
    chat_id = conf.new_channel_id

    now = datetime.now()
    year_month = now.strftime('%Y/%m')
    path = f'{conf.data_path}/word_of_day/{year_month}/latest.json'
    data = json_load(path)
    title = data[str(now.day)]
    print(title)

    text = '🔆 Слово дня\n' + Reply(title).text
    send(bot, chat_id, text)
Exemplo n.º 3
0
class messages:
    filename = join(conf.data_path, 'new_articles', 'messages.json')
    ids = json_load(filename)

    @classmethod
    def get(cls, title):
        return cls.ids[title]

    @classmethod
    def set(cls, title, message_id):
        if title in cls.ids:
            slack_error(f'⚠️ Сообщение для "`{title}`" уже было?')
        cls.ids[title] = message_id
        json_dump(cls.filename, cls.ids)
Exemplo n.º 4
0
def get_stats():
    new_data = {}
    for lang in langs:
        html_content = read(html_path(lang))
        # table = re.search(
        #     '<table class="wikitable mw-statistics-table">(.*?)</table>',
        #     html_content,
        #     re.DOTALL,
        # ).group(1)
        # write(f'table/{lang}.html', table)
        numbers = re.findall('<td class="mw-statistics-numbers">(.*?)</td>',
                             html_content)
        # write(f'numbers/{lang}.txt', '\n'.join(numbers))
        # print(':', ' | '.join(numbers))
        # if len(numbers) != 20:
        #     print(lang, len(numbers))
        values = []
        for index in [0, 1, 3, 5, 6]:
            value = re.sub('[^0-9]', '', numbers[index])
            if value:
                values.append(value)
        if values:
            # write(f'values/{lang}.txt', '\n'.join(values))
            new_data[lang] = {
                'goods': int(values[0]),
                'pages': int(values[1]),
                'edits': int(values[2]),
                'users': int(values[3]),
                'active': int(values[4]),
            }
        # else:
        #     print(lang)

    history_filename = join(conf.data_path, 'daily_stats', 'history', 'values',
                            f'{dtf("Ym/Ymd")}.json')
    active_filename = join(conf.data_path, 'daily_stats', 'active.json')

    old_data = json_load(active_filename)
    json_dump(history_filename, new_data)
    json_dump(active_filename, new_data)

    return old_data, new_data
Exemplo n.º 5
0
 def import_entries(self, suffix=''):
     filename = self.entries_filename(suffix)
     if not exists(filename):
         print(f'ImportEntriesError: File not found: "{filename}"')
         return
     self.entries = json_load(filename)