Пример #1
0
    def test_flow(self):
        telegraph = Telegraph()

        response = telegraph.create_account(
            short_name='python telegraph',
            author_name='Python Telegraph API wrapper',
            author_url='https://github.com/python273/telegraph')
        self.assertTrue('access_token' in response)
        self.assertTrue('auth_url' in response)

        response = telegraph.get_account_info()
        self.assertEqual(response['short_name'], 'python telegraph')

        response = telegraph.edit_account_info(
            short_name='Python Telegraph Wrapper')
        self.assertEqual(response['short_name'], 'Python Telegraph Wrapper')

        response = telegraph.create_page('Python Telegraph API wrapper',
                                         html_content='<p>Hello, world!</p>')

        telegraph.edit_page(response['path'],
                            'Python Telegraph API Wrapper',
                            html_content=POST_HTML_CONTENT)

        response = telegraph.get_views(response['path'])
        self.assertTrue('views' in response)

        response = telegraph.get_page_list()
        self.assertTrue(response['total_count'] > 0)

        response = telegraph.revoke_access_token()
        self.assertTrue('access_token' in response)
        self.assertTrue('auth_url' in response)
Пример #2
0
    def export_to_telegraph(self, content: str):
        telegraph = Telegraph(TELEGRAPH_USER_TOKEN)

        if self.is_changed():
            telegraph_response = telegraph.create_page(title=self.title, html_content=content)

        else:
            telegraph_response = telegraph.edit_page(path=self.telegraph_path, title=self.title, html_content=content)

        print(telegraph_response)
Пример #3
0
class TelegraphHelper:
    def __init__(self, author_name=None, author_url=None):
        self.telegraph = Telegraph()
        self.short_name = ''.join(random.SystemRandom().choices(
            string.ascii_letters, k=8))
        self.access_token = None
        self.author_name = author_name
        self.author_url = author_url
        self.create_account()

    def create_account(self):
        self.telegraph.create_account(short_name=self.short_name,
                                      author_name=self.author_name,
                                      author_url=self.author_url)
        self.access_token = self.telegraph.get_access_token()
        LOGGER.info(
            f"Creating TELEGRAPH Account using  '{self.short_name}' name")

    def create_page(self, title, content):
        try:
            return self.telegraph.create_page(title=title,
                                              author_name=self.author_name,
                                              author_url=self.author_url,
                                              html_content=content)
        except RetryAfterError as st:
            LOGGER.warning(
                f'Telegraph Flood control exceeded. I will sleep for {st.retry_after} seconds.'
            )
            sleep(st.retry_after)
            return self.create_page(title, content)

    def edit_page(self, path, title, content):
        try:
            return self.telegraph.edit_page(path=path,
                                            title=title,
                                            author_name=self.author_name,
                                            author_url=self.author_url,
                                            html_content=content)
        except RetryAfterError as st:
            LOGGER.warning(
                f'Telegraph Flood control exceeded. I will sleep for {st.retry_after} seconds.'
            )
            sleep(st.retry_after)
            return self.edit_page(path, title, content)
Пример #4
0
class TelegraphEditor(object):
    def __init__(self):
        self.tgph = Telegraph(TGPH_TOKEN)

    def new_comments(self):
        path_top = self.tgph.create_page(title='Komments | 0',
                                         html_content='-')['path']
        path_new = self.tgph.create_page(title='Komments | 0',
                                         html_content='-')['path']
        print('CREATED NEW COMMENTS: ', path_top)
        return path_new, path_top

    def update_answers(self, comment_id):
        comnt = db.get_comment(comment_id)
        answers = db.get_answers(comment_id)

        body = f'''
        {comnt.text_main}<br/>
         ✉️ {comnt.count_answers}  |  ❤️ {comnt.liked_count}  |  {comnt.date_add.strftime('%H:%M')}<br/>
        <aside><a href=\"https://t.me/KomentsBot?start=1{comnt.id}\">Write answer</a></aside>
        '''

        for answer in answers:
            body += f'''
            <b>{answer.user_name}</b><br/>
            {answer.text_main}<br/>
            ❤️ {comnt.liked_count}  |  {comnt.date_add.strftime('%H:%M')}<br/>
            '''

        title = comnt.user_name

        if comnt.answers_url:
            print('Update answer page')
            r = self.tgph.edit_page(path=comnt.answers_url,
                                    title=title,
                                    html_content=body)
            print(r)

        else:
            print('Create answer page')

            path = self.tgph.create_page(title=title,
                                         html_content=body)['path']
            print(path)
            db.set_comment(comment_id=comment_id,
                           set='answers_url',
                           value=path)

    def update_comments(self, post_id):
        post = db.get_post_info_comments(post_id)

        print(post)
        print(type(post))
        print(post.telegraph_path_new)

        comments_new = db.get_comments(post_id,
                                       sort_comnts='new',
                                       limit_comnts=25)
        comments_top = db.get_comments(post_id,
                                       sort_comnts='top',
                                       limit_comnts=25)

        path_new = post.telegraph_path_new
        path_top = post.telegraph_path_top

        base = ' <a href="http://t.me/KomentsBot?start=0' + str(
            post_id) + '"> Add comments</a><br/>'

        body_new = f'Sort <b>New</b> <a href="https://telegra.ph/{path_top}">Top</a> ' + base
        body_top = f'Sort <a href="https://telegra.ph/{path_new}">New</a> <b>Top</b> ' + base

        for com in comments_new:
            print(com.answers_url, type(com.answers_url))

            comment = f'''
            <h4>{com.user_name}</h4>
            {com.text_main}<br/>
            <a href=\"https://t.me/KomentsBot?start=1{com.id}\"> ✉ {com.count_answers}  |  ❤️ {com.liked_count}  |  {com.date_add.strftime('%H:%M')}</a><br/>
            '''

            if com.count_answers > 0:
                answer = db.get_one_answer(com.id)

                comment += f'''
                <b>{answer.user_name}</b><br/>
                <i>{answer.text_main}</i><br/>
                ❤️ {answer.liked_count}  |  {answer.date_add.strftime('%H:%M')}<br/>
                <a href=\"https://telegra.ph/{com.answers_url}\">Open all answers {com.count_answers}</a> <br/>
                '''

            body_new += comment

        for com in comments_top:
            print(com.answers_url, type(com.answers_url))

            comment = f'''
            <h4>{com.user_name}</h4>
            {com.text_main}<br/>
            <a href=\"https://t.me/KomentsBot?start=1{com.id}\"> ✉ {com.count_answers}  |  ❤️ {com.liked_count}  |  {com.date_add.strftime('%H:%M')}</a><br/>
            '''

            if com.count_answers > 0:
                answer = db.get_one_answer(com.id)

                comment += f'''
                <b>{answer.user_name}</b><br/>
                <i>{answer.text_main}</i><br/>
                ❤️ {answer.liked_count}  |  {answer.date_add.strftime('%H:%M')}<br/>
                <a href=\"https://telegra.ph/{com.answers_url}\">Open all answers {com.count_answers}</a> <br/>
                '''

            body_top += comment

        title = 'Komments | ' + str(post.all_comments)

        print(post[0], post[1])

        r = self.tgph.edit_page(path=post[0],
                                title=title,
                                html_content=body_new)
        self.tgph.edit_page(path=post[1], title=title, html_content=body_top)

        print(r)
Пример #5
0
class PostEditor(object):
    def __init__(self):
        self.tgph = Telegraph(TGPH_TOKEN)

    def new_post(self, bot, msg):
        post = msg.channel_post

        post_id = db.new_post(chennel_id=post.chat.id,
                              msg_id=post.message_id,
                              telegraph_path_new=page_new['path'],
                              telegraph_path_top=page_top['path'])

        standart_bts = [[
            Button('Написать коментарий',
                   url='t.me/KomentsBot?start=0' + str(post_id)),
        ]]

        if post.photo:
            type_msg = 'photo'
            text_msg = post.caption
        else:
            type_msg = 'text'
            text_msg = post.text

        text_post = add_entities(text_msg, post.entities)

        page_url = page_top['url']
        new_text = f'{text_post}\n<a href="">&#65524;</a>'

        self.edit_msg(bot, post.chat.id, post.message_id, new_text,
                      standart_bts)

    def edit_msg(self, bot, chat_id, msg_id, text, bts):
        try:
            bot.edit_message_text(chat_id=chat_id,
                                  message_id=msg_id,
                                  text=text,
                                  reply_markup=Markup(bts),
                                  parse_mode='html')

        except Exception as e:
            print('Error edit msg  in channel: ', e)
            bot.edit_message_caption(chat_id=chat_id,
                                     message_id=msg_id,
                                     caption=text,
                                     reply_markup=Markup(bts),
                                     parse_mode='html')

    def new_comment(self,
                    bot,
                    user_id,
                    text,
                    user_name,
                    post_id=None,
                    comment_id=None):
        if comment_id:
            post_id = db.new_subcomment(user_id, text, user_name, comment_id)
        else:
            post_id = db.new_comment(user_id, text, user_name, post_id)

        self.update_post(bot, post_id)
        bot.send_message(user_id, 'You comments sended!\nThank you!')

    def update_post(self, bot, post_id=None, comment_id=None):
        post = db.get_post(post_id=post_id, comment_id=comment_id)

        #============= EDIT PAGE IN TELEGRAPH =================
        print('POST ID ', post_id)
        comments_new = db.get_comments(post_id=post.id,
                                       sort_comnts='new',
                                       limit_comnts=25)
        comments_top = db.get_comments(post_id=post.id,
                                       sort_comnts='top',
                                       limit_comnts=25)

        base = ' <a href="http://t.me/KomentsBot?start=0' + str(
            post.id) + '"> Add comments</a><br/>'

        body_new = f'Sort <b>New</b> <a href="https://telegra.ph/{post.telegraph_path_top}">Top</a> ' + base
        body_top = f'Sort <a href="https://telegra.ph/{post.telegraph_path_new}">New</a> <b>Top</b> ' + base

        b_com = '<h4>{}</h4>{}<br><a href="http://t.me/KomentsBot?start=1{}"> ✉️ {}  |  ❤️ {}  |  {}</a><br/>'

        print()

        for com in comments_new:

            body_new += b_com.format(com.user_name, com.text, com.id,
                                     com.count_subcomnt, com.liked_count,
                                     com.date_add)
            print(com.count_subcomnt)
            if com.count_subcomnt > 0:
                subcomnts = db.get_subcomments(com.id)
                print(subcomnts)

                for subcomnt in subcomnts:
                    body_new += f' |    <a href="/"><u><b>{subcomnt.user_name}</b>  |  ❤️ {subcomnt.liked_count}  |  {subcomnt.date_add}</u></a><br/> |     <aside>{subcomnt.text}</aside>  <br/>'

        for com in comments_top:
            body_top += b_com.format(com.user_name, com.text, com.id,
                                     com.count_subcomnt, com.liked_count,
                                     com.date_add)

        print(body_new)
        title = 'Komments | ' + str(post.all_comments)

        print(
            self.tgph.edit_page(path=post.telegraph_path_new,
                                title=title,
                                html_content=body_new))
        print(
            self.tgph.edit_page(path=post.telegraph_path_top,
                                title=title,
                                html_content=body_top))
        #=======================================================

        msg = bot.forward_message(chat_id='@gpalik',
                                  from_chat_id=post.channel_id,
                                  message_id=post.msg_id)

        text_post = msg.text.split('\ufff4')[0]
        text_post = add_entities(text_post, msg.entities)

        text = f'<a href="Bot">&#65524;</a><b>Коментарии  {post.all_comments}</b>'

        print(post.telegraph_path_top, post.telegraph_path_new)

        for comm in comments_new[:-3]:

            text += f'\n <b>{comm.user_name}</b>\n  <i>{comm.text}</i>\n ❤️ {comm.liked_count}  |  {comm.date_add}'

        standart_bts = [[
            Button('Открить коментарии',
                   url="https://telegra.ph/" + post.telegraph_path_top)
        ]]

        self.edit_msg(bot, post.channel_id, post.msg_id, text_post + text,
                      standart_bts)