Exemplo n.º 1
0
def see_more_caption(chat_id, reply_to_message_id, media_id):
    response = instagram_api.get_user_posts_info(media_id)
    bot_functions.send_chat_action(chat_id=chat_id, action='typing')
    if response is not None:
        if response == '400':

            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.page_is_private +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        elif response == '403':

            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.bot_not_work +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        else:
            post_caption = response['items'][0]['caption']['text']
            bot_functions.send_message(
                chat_id=chat_id,
                msg=urllib.parse.quote_plus(post_caption) +
                bot_functions.bot_address_caption,
                parse_mode='HTML',
                reply_to_message_id=reply_to_message_id)
Exemplo n.º 2
0
def get_biography(chat_id, user_id, message_id):
    biography = instagram_api.get_user_biography(
        username=bot_functions.get_last_username(user_id))
    bot_functions.send_chat_action(chat_id=chat_id, action='typing')
    bot_functions.send_message(chat_id=chat_id,
                               msg=urllib.parse.quote_plus(biography) +
                               bot_functions.bot_address_caption,
                               parse_mode='HTML',
                               reply_to_message_id=message_id)
Exemplo n.º 3
0
def get_user_profile(chat_id, user_id, message_id):
    profile_image_url = instagram_api.get_user_profile_image(
        username=bot_functions.get_last_username(user_id))
    bot_functions.send_chat_action(chat_id=chat_id, action='upload_photo')
    print("hi")
    bot_functions.send_photo(
        chat_id=chat_id,
        photo_link=urllib.parse.quote_plus(profile_image_url),
        caption=bot_functions.bot_address_caption,
        parse_mode='HTML',
        reply_to_message_id=message_id)
Exemplo n.º 4
0
def start_command_handler(chat_id, user_id, first_name, last_name, username):
    bot_functions.send_chat_action(chat_id=chat_id, action='typing')
    query = "select * from users WHERE user_id={}".format(user_id)
    db_config.cursor.execute(query)
    result = db_config.cursor.fetchall()
    row_number = len(result)
    if row_number == 0:
        query = "INSERT INTO users(user_id,first_name,last_name,username,step) VALUES (%s, %s, %s, %s, %s)"
        val = (user_id, first_name, last_name, username, 'home')
        db_config.cursor.execute(query, val)
        db_config.cnx.commit()

    bot_functions.send_message(chat_id=chat_id,
                               msg=messages.welcome_msg,
                               parse_mode='HTML')
Exemplo n.º 5
0
def get_user_post_info(callback_query_id, chat_id, media_id):
    response = instagram_api.get_user_posts_info(media_id)
    if response is not None:
        if response == '400':
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.page_is_private +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        elif response == '403':
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.bot_not_work +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        else:
            text = ''
            post = response['items'][0]
            like_count = post['like_count']
            comment_count = post['comment_count']
            post_sender = post['user']['username']
            media_type = post['media_type']
            created_at = post['taken_at']
            created_at = jdatetime.datetime.utcfromtimestamp(
                created_at).strftime('%H:%M:%S - %Y/%m/%d')

            if media_type == 1 or media_type == 8:
                text = '''\U0001F5E3 فرستنده پست: {}\n\U0001F5E3 تعداد لایک: {:,}\n\U0001F5E3 تعداد کامنت: {:,}\n\U0001F5E3 زمان ارسال پست: {}'''.format(
                    post_sender, like_count, comment_count, created_at)

            elif media_type == 2:
                view_count = post['view_count']
                text = '''\U0001F5E3 فرستنده پست: {}\n\U0001F5E3 تعداد لایک: {:,}\n\U0001F5E3 تعداد کامنت: {:,}\n\U0001F5E3 تعداد بیننده: {:,}\n\U0001F5E3 زمان ارسال پست: {}
                                        '''.format(post_sender, like_count,
                                                   comment_count, view_count,
                                                   created_at)

            bot_functions.answer_callback_query(query_id=callback_query_id,
                                                text=text,
                                                show_alert=True)
Exemplo n.º 6
0
def show_more_user_posts(chat_id, user_id, next_max_id):
    response = instagram_api.show_more_post(user_id, next_max_id)
    if response is not None:
        if response == '400':
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.page_is_private +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        elif response == '403':
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.bot_not_work +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        else:
            parse_user_posts_response(chat_id, response)
    else:
        bot_functions.send_chat_action(chat_id=chat_id, action='typing')
        bot_functions.send_message(chat_id=chat_id,
                                   msg=messages.no_exist_post +
                                   bot_functions.bot_address_caption,
                                   parse_mode='HTML')
Exemplo n.º 7
0
def parse_user_posts_response(chat_id, response):
    posts = response['items']
    if len(posts) != 0:
        for post in posts:
            media_id = post['id']
            post_code = post['code']
            media_type = post['media_type']
            post_caption = post['caption']['text']
            user_id = post['user']['pk']
            if media_type == 1:  # send photo
                post_image_url = post['image_versions2']['candidates'][0][
                    'url']
                markup, post_caption = check_more_available_and_caption_length(
                    response, post, post_caption, post_code, media_id, user_id)

                bot_functions.send_chat_action(chat_id=chat_id,
                                               action='upload_photo')
                bot_functions.send_photo(
                    chat_id=chat_id,
                    photo_link=urllib.parse.quote_plus(post_image_url),
                    caption=urllib.parse.quote_plus(post_caption) +
                    bot_functions.bot_address_caption,
                    parse_mode='HTML',
                    markup=json.dumps(markup))

            elif media_type == 2:  # send video
                post_video_url = post['video_versions'][0]['url']
                markup, post_caption = check_more_available_and_caption_length(
                    response, post, post_caption, post_code, media_id, user_id)

                bot_functions.send_chat_action(chat_id=chat_id,
                                               action='upload_video')
                bot_functions.send_video(
                    chat_id=chat_id,
                    video_link=urllib.parse.quote_plus(post_video_url),
                    caption=urllib.parse.quote_plus(post_caption) +
                    bot_functions.bot_address_caption,
                    parse_mode='HTML',
                    markup=json.dumps(markup))

            elif media_type == 8:  # send media album(photo or video or both)
                current_message_id = ''
                first_media = post['carousel_media'][0]
                media_type = first_media['media_type']

                if media_type == 1:  # send photo
                    post_image_url = first_media['image_versions2'][
                        'candidates'][0]['url']
                    markup, post_caption = check_more_available_and_caption_length(
                        response, post, post_caption, post_code, media_id,
                        user_id)

                    bot_functions.send_chat_action(chat_id=chat_id,
                                                   action='upload_photo')
                    res = bot_functions.send_photo(
                        chat_id=chat_id,
                        photo_link=urllib.parse.quote_plus(post_image_url),
                        caption=urllib.parse.quote_plus(post_caption) +
                        bot_functions.bot_address_caption,
                        parse_mode='HTML',
                        markup=json.dumps(markup))
                    current_message_id = res['result']['message_id']

                elif media_type == 2:  # send video

                    post_video_url = first_media['video_versions'][0]['url']
                    markup, post_caption = check_more_available_and_caption_length(
                        response, post, post_caption, post_code, media_id,
                        user_id)

                    bot_functions.send_chat_action(chat_id=chat_id,
                                                   action='upload_video')
                    res = bot_functions.send_video(
                        chat_id=chat_id,
                        video_link=urllib.parse.quote_plus(post_video_url),
                        caption=urllib.parse.quote_plus(post_caption) +
                        bot_functions.bot_address_caption,
                        parse_mode='HTML',
                        markup=json.dumps(markup))
                    current_message_id = res['result']['message_id']

                other_media = post['carousel_media'][1:]
                other_media_array = []
                for media in other_media:
                    media_type = media['media_type']

                    if media_type == 1:  # photo
                        post_image_url = media['image_versions2'][
                            'candidates'][0]['url']
                        other_media_array.append({
                            'type':
                            'photo',
                            'media':
                            urllib.parse.quote(post_image_url)
                        })

                    elif media_type == 2:  # video
                        post_video_url = media['video_versions'][0]['url']
                        other_media_array.append({
                            'type':
                            'video',
                            'media':
                            urllib.parse.quote(post_video_url)
                        })

                bot_functions.send_chat_action(chat_id=chat_id,
                                               action='upload_document')
                bot_functions.send_media_group(
                    chat_id=chat_id,
                    media=json.dumps(other_media_array),
                    reply_to_message_id=current_message_id)
Exemplo n.º 8
0
def username_command_handler(chat_id, user_id, text):
    if '@' in str(text):
        at_sign_char_index = str(text).index('@')
        if at_sign_char_index == 0:
            text = text[1:]
    if 'https://instagram.com/' in str(text):
        url_char_index = str(text).index('https://instagram.com/')
        if url_char_index == 0:
            text = text[22:]

    bot_functions.set_last_username(user_id, text)
    username_info = instagram_api.get_username_info(text)

    if username_info is not None:

        if username_info == '404':  # 404 mean : user not found
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.user_not_found +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        elif username_info == '401':  # 401 mean : wrong input
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.wrong_input +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        elif username_info == '403':  # 403 mean : login required (the problem is ours : cookies is expired)
            bot_functions.send_chat_action(chat_id=chat_id, action='typing')
            bot_functions.send_message(chat_id=chat_id,
                                       msg=messages.bot_not_work +
                                       bot_functions.bot_address_caption,
                                       parse_mode='HTML')

        else:
            full_name = username_info['full_name']
            username = username_info['username']
            media_count = username_info['media_count']
            follower_count = username_info['follower_count']
            following_count = username_info['following_count']

            is_private = username_info['is_private']
            if is_private:
                page_status = 'قفل (Private)'
            else:
                page_status = 'باز (Public)'

            biography = username_info['biography']
            external_url = username_info['external_url']

            caption = '''\U0001F5E3 <b>نام نمایشی: </b>{}
            
\U0001F464 <b>نام کاربری: </b>{}

\U0001F4E5 <b>تعداد پست: </b>{:,}

\N{leftwards black arrow} <b>تعداد فالوور: </b>{:,}

\N{black rightwards arrow} <b>تعداد فالووینگ: </b>{:,}

\N{lock} <b>وضعیت صفحه: </b>{}

\U0001F4DC <b>بیوگرافی:</b>
{}

\U0001F517 <b>لینک:</b>

<a href='{}'>{}</a>
'''.format(full_name, username, media_count, follower_count, following_count,
            page_status, urllib.parse.quote_plus(biography), external_url,
            external_url) + bot_functions.bot_address_caption

            markup = {
                'inline_keyboard':
                [[{
                    'text': '\U0001F4E5 ' + 'بیوگرافی',
                    'callback_data': 'get_biography'
                }, {
                    'text': '\U0001F4E5 ' + 'پروفایل',
                    'callback_data': 'get_profile'
                }],
                 [{
                     'text': '\U0001F4E5 ' + 'هایلایت ها',
                     'callback_data': 'get_highlights'
                 }, {
                     'text': '\U0001F4E5 ' + 'استوری ها',
                     'callback_data': 'get_stories'
                 }, {
                     'text': '\U0001F4E5 ' + 'پست ها',
                     'callback_data': 'get_posts'
                 }],
                 [{
                     'text': '\U0001F4E5 ' + 'مشاهده صفحه در اینستاگرام',
                     'url': 'https://instagram.com/{}'.format(text)
                 }]]
            }

            bot_functions.send_chat_action(chat_id=chat_id,
                                           action='upload_photo')
            response = bot_functions.send_photo(
                chat_id=chat_id,
                photo_link=username_info['hd_profile_pic_url_info']['url'],
                caption=caption,
                parse_mode='HTML',
                markup=json.dumps(markup))
            message_id = response['result']['message_id']
            bot_functions.set_last_message_id(user_id, message_id)