コード例 #1
0
    def send_with_album_sendings(self, message_generator):
        """
        Отправляет вложения альбома

        :param message_generator: генератор сообщений
        """
        attachments_album = []
        file_first = self.attachments.album_sendings[0]
        caption_message = message_generator.send(
            type(self).SYMBOLS_WITH_ATTACHMENT)

        attachments_album.append(
            InputMediaPhoto(file_first[PHOTO], caption=caption_message)
            if PHOTO in file_first.keys(
            ) else InputMediaVideo(file_first[VIDEO_ALBUM].media,
                                   duration=file_first[VIDEO_ALBUM].duration,
                                   width=file_first[VIDEO_ALBUM].width,
                                   caption=caption_message,
                                   height=file_first[VIDEO_ALBUM].height))

        for file in self.attachments.album_sendings[1:]:
            attachments_album.append(
                InputMediaPhoto(file[PHOTO]) if PHOTO in file.keys(
                ) else InputMediaVideo(file[VIDEO_ALBUM].media,
                                       duration=file[VIDEO_ALBUM].duration,
                                       width=file[VIDEO_ALBUM].width,
                                       height=file[VIDEO_ALBUM].height))

        self.tg_client.send_media_group(self.chat_link, attachments_album)
        time.sleep(3)
コード例 #2
0
    def send_with_no_text(self):
        """
        Отправляет пост, у которого нет исходного текста
        """
        album_attachments = []

        for file in self.attachments.album_sendings:
            album_attachments.append(
                InputMediaPhoto(file[PHOTO]) if PHOTO in file.keys(
                )[0] else InputMediaVideo(file[VIDEO_ALBUM].media,
                                          duration=file[VIDEO_ALBUM].duration,
                                          width=file[VIDEO_ALBUM].width,
                                          height=file[VIDEO_ALBUM].height))

        self.tg_client.send_media_group(self.chat_link, album_attachments)
        time.sleep(3)

        for file in self.attachments.videos:
            self.tg_client.send_video(self.chat_link,
                                      file.media,
                                      file.duration,
                                      width=file.width,
                                      height=file.height)
            time.sleep(3)

        for file in self.attachments.docs:
            self.tg_client.send_document(self.chat_link, file.media)
            time.sleep(3)
コード例 #3
0
    def send_post(self, destination):
        try:
            # Отправляем текст, нарезая при необходимости
            for text in cut_long_text(self.final_text):
                my_bot.send_message(
                    destination,
                    text,
                    parse_mode="HTML",
                    disable_web_page_preview=self.web_preview_url == '')

            # Отправляем отображаемые приложения к посту
            for url in self.gif_links:
                my_bot.send_document(destination, url)
            if len(self.image_links) > 0:
                my_bot.send_media_group(
                    destination,
                    [InputMediaPhoto(url) for url in self.image_links])
            if len(self.video_links) > 0:
                my_bot.send_media_group(
                    destination,
                    [InputMediaVideo(url) for url in self.video_links])
            for url in self.audio_links:
                my_bot.send_audio(destination, url)
        except apihelper.ApiException:
            action_log("VK Error: api exception")
コード例 #4
0
ファイル: main.py プロジェクト: fwolf/rss-to-telegram
def send_album(bot, chat_id, image_urls, video_urls):
    medias = []
    for url in image_urls :
        medias.append(InputMediaPhoto(url))
    for url in video_urls :
        medias.append(InputMediaVideo(video))
    try :
        bot.send_media_group(chat_id, medias)

    except telebot.apihelper.ApiException as e:
        for url in image_urls :
            bot.send_document(chat_id, url)
        for url in image_urls :
            bot.send_document(chat_id, url)
        bot.send_media_group(chat_id, medias)
コード例 #5
0
ファイル: main.py プロジェクト: justLive666/tg_albumMakerBot
def make_videoAlbum(message):
	media = []
	with sqlite3.connect("bot.db") as bd:
		sq = bd.cursor()
		for i in sq.execute(f"SELECT file_id,caption,type FROM user_values WHERE user_id = {message.chat.id}"):
			if(i[2] == "video"):
				media.append(InputMediaVideo(i[0]))
			elif(i[2] == "photo"):
				media.append(InputMediaPhoto(i[0]))
			else:
				print("Не добавляет :(")
		for i in sq.execute(f"SELECT caption FROM user_values WHERE user_id = {message.chat.id} LIMIT 1"):
			media[0].caption = i[0]
	bot.send_media_group(message.chat.id, media)
	media = []
	delete_values(message)
コード例 #6
0
def get_reddit_content(message):
    if "https://www.reddit.com/" in message.text:
        chat_identity = message.chat.title if message.chat.id < 0 else str(
            message.chat.id)
        chat_identity = "!!!ME!!!" if chat_identity == me_chat_id else chat_identity
        chat_identity = "!!!RUSIK!!!" if chat_identity == rusik_chat_id else chat_identity
        chat_identity = "!!!MAX!!!" if chat_identity == max_chat_id else chat_identity

        try:
            file = open("id.txt", "r")
        except Exception as e:
            file = open("id.txt", "w")
            file.close()
            file = open("id.txt", "r")
        s = file.read()
        id = int(s) if s != '' else 1
        file.close()
        file = open("id.txt", "w")
        file.write(str(id + 1))
        file.close()
        print('-------------------------' + '\n' + get_current_time() +
              " id: " + str(id) + " Chat identity: " + chat_identity)
        url_message = message.text
        start_url = url_message.find("https")
        url = iri_to_uri(url_message[start_url:])
        print(get_current_time() + " id: " + str(id) + " URL: " + url)
        req = urllib.request.Request(
            url,
            data=None,
            headers={
                'User-Agent':
                'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
            })
        try:
            retry_count = 1
            url_response = ...
            while (retry_count <= 10):
                try:
                    time.sleep(0.1)
                    url_response = urllib.request.urlopen(req)
                    break
                except Exception as e:
                    bot.send_message(
                        '-556187948', "Chat identity: " + chat_identity +
                        '\n' + "Retry - " + str(retry_count))
                    retry_count += 1
            response_data = url_response.read().decode('utf-8')
            tittle = response_data[response_data.
                                   find('<h1 class="_eYtD2XCVieq6emjKBH3m">') +
                                   34:response_data.find('</h1>') + 1]
            tittle = tittle.replace("&#x27;", "'").replace("&quot;", "")
            if '"type":"image"' in response_data:
                point = response_data.find('"type":"image"')
                start_url = response_data.find("https://", point - 100)
                end_url = response_data.find('"', start_url)
                draft_url = response_data[start_url:end_url]
                time.sleep(0.1)
                urllib.request.urlretrieve(draft_url.replace("\\u0026", "&"),
                                           str(id) + ".jpg")
                foto = open(str(id) + '.jpg', 'rb')
                os.remove(str(id) + ".jpg")
                bot.send_media_group(
                    message.chat.id,
                    [InputMediaPhoto(foto, tittle[0:tittle.find(':')])], None,
                    message.id)
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "type":"image"')
            elif ('.mp4' in response_data) & ('.mp4,' not in response_data):
                arr = response_data.split('DASH_96.mp4"')
                sub = arr[0]
                resolution_arr = arr[1].split('"height":')
                resolution = resolution_arr[1][0:resolution_arr[1].find(',')]

                try:
                    try:
                        print(get_current_time() + " id: " + str(id) +
                              ' Try: "480.mp4" with sound')
                        urllib.request.urlopen(sub[-32:] + 'DASH_480.mp4')
                        url_for_combine = 'https://ds.redditsave.com/download-sd.php?permalink=' + url + '/&video_url=' + sub[
                            -32:] + 'DASH_480.mp4' + '&audio_url=' + sub[
                                -32:] + 'DASH_audio.mp4'
                    except Exception as e:
                        print(get_current_time() + " id: " + str(id) +
                              ' Error 480.mp4 with sound: ' + str(e))
                        try:
                            time.sleep(0.01)
                            print(get_current_time() + " id: " + str(id) +
                                  ' Try: "360.mp4" with sound')
                            urllib.request.urlopen(sub[-32:] + 'DASH_360.mp4')
                            url_for_combine = 'https://ds.redditsave.com/download-sd.php?permalink=' + url + '/&video_url=' + sub[
                                -32:] + 'DASH_360.mp4' + '&audio_url=' + sub[
                                    -32:] + 'DASH_audio.mp4'
                        except Exception as e:
                            print(get_current_time() + " id: " + str(id) +
                                  ' Error 360.mp4 with sound: ' + str(e))
                            print(get_current_time() + " id: " + str(id) +
                                  ' Try: "240.mp4" with sound')
                            url_for_combine = 'https://ds.redditsave.com/download-sd.php?permalink=' + url + '/&video_url=' + sub[
                                -32:] + 'DASH_240.mp4' + '&audio_url=' + sub[
                                    -32:] + 'DASH_audio.mp4'
                    urllib.request.urlretrieve(url_for_combine,
                                               str(id) + ".mp4")
                    video = open(str(id) + '.mp4', 'rb')
                    os.remove(str(id) + ".mp4")
                    bot.send_media_group(message.chat.id, [
                        InputMediaVideo(video, None,
                                        tittle[0:tittle.find(':')])
                    ], None, message.id)
                    print(get_current_time() + " id: " + str(id) +
                          ' Success: ".mp4" with sound')
                except Exception as e:
                    print(get_current_time() + " id: " + str(id) +
                          ' Error 240.mp4 with sound: ' + str(e))
                    try:
                        urllib.request.urlopen(sub[-32:] + 'DASH_' +
                                               resolution + '.mp4')
                        bot.reply_to(
                            message, tittle[0:tittle.find(':')] + '\n' +
                            sub[-32:] + 'DASH_' + resolution + '.mp4')
                        print(get_current_time() + " id: " + str(id) +
                              ' Success: "resolution.mp4"')
                    except Exception as e:
                        print(get_current_time() + " id: " + str(id) +
                              ' Error resolution.mp4 without sound: ' + str(e))
                        urllib.request.urlopen(sub[-32:] + 'DASH_240.mp4')
                        bot.reply_to(
                            message, tittle[0:tittle.find(':')] + '\n' +
                            sub[-32:] + 'DASH_240.mp4')
                        print(get_current_time() + " id: " + str(id) +
                              ' Success: "240.mp4"')
            elif 'https://gfycat.com/' in response_data:
                draft_url = response_data[response_data.
                                          find('https://gfycat.com/'):]
                inner_url = draft_url[:draft_url.find('"')]
                inner_url_response = urllib.request.urlopen(inner_url)
                inner_response_data = inner_url_response.read().decode('utf-8')
                draft_url = inner_response_data[inner_response_data.
                                                find("og:video:secure_url") +
                                                30:]
                url = draft_url[:draft_url.find('"')]
                bot.reply_to(message, tittle[0:tittle.find(':')] + '\n' + url)
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "https://gfycat.com/"')
            elif 'https://clips.twitch.tv' in response_data:
                draft_url = response_data[response_data.
                                          find('https://clips.twitch.tv'):]
                inner_url = draft_url[:draft_url.find('"')]
                bot.reply_to(message,
                             tittle[0:tittle.find(':')] + '\n' + inner_url)
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "https://clips.twitch.tv"')
            elif '"type":"gifvideo"' in response_data:
                arr = response_data.split('"type":"gifvideo"')
                sub = arr[0][-200:]
                draft_url = sub[sub.find('https://'):]
                url = draft_url[:draft_url.find('"')]
                bot.reply_to(
                    message, tittle[0:tittle.find(':')] + '\n' +
                    url.replace("\\u0026", "&"))
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "type":"gifvideo"')
            elif 'class="_3BxRNDoASi9FbGX01ewiLg' in response_data or 'class="_3spkFGVnKMHZ83pDAhW3Mx' in response_data or 'class="_35oEP5zLnhKEbj5BlkTBUA' in response_data:
                separator = ''
                if 'class="_3BxRNDoASi9FbGX01ewiLg' in response_data:
                    separator = 'class="_3BxRNDoASi9FbGX01ewiLg'
                elif 'class="_3spkFGVnKMHZ83pDAhW3Mx' in response_data:
                    separator = 'class="_3spkFGVnKMHZ83pDAhW3Mx'
                elif 'class="_35oEP5zLnhKEbj5BlkTBUA' in response_data:
                    separator = 'class="_35oEP5zLnhKEbj5BlkTBUA'
                else:
                    print(get_current_time() + " id: " + str(id) +
                          ' Unsupport multimedia separator')
                arr = response_data.split(separator)
                img_arr = []
                img_count = 0
                part_string = ""
                part_count = 0
                for sub in arr:
                    if 'href="https://preview.redd.it/' in sub:
                        draft_url = sub[sub.find('https://preview.redd.it/'):]
                        url = draft_url[:draft_url.find('"')]
                        image_dimension = url[url.find("?") - 3:url.find("?")]
                        time.sleep(0.1)
                        urllib.request.urlretrieve(
                            url.replace('amp;', ''),
                            str(id) + "." + image_dimension)
                        foto = open(str(id) + '.' + image_dimension, 'rb')
                        if len(img_arr) == 0:
                            if len(arr) > 21:
                                part_count += 1
                                part_string = " (Part " + str(part_count) + ")"
                            img_arr.append(
                                InputMediaPhoto(
                                    foto,
                                    tittle[0:tittle.find(':')] + part_string))
                        else:
                            img_arr.append(InputMediaPhoto(foto))
                        os.remove(str(id) + "." + image_dimension)
                        img_count += 1
                        if img_count == 10:
                            bot.send_media_group(message.chat.id, img_arr,
                                                 None, message.id)
                            img_arr = []
                            img_count = 0
                if img_count % 10 != 0:
                    bot.send_media_group(message.chat.id, img_arr, None,
                                         message.id)
                print(get_current_time() + " id: " + str(id) + ' Success: ' +
                      separator)
            elif '<meta property="og:type" content="image" />' in response_data:
                point = response_data.find('property="og:image"')
                start_url = response_data.find("https://", point)
                end_url = response_data.find('"', start_url)
                draft_url = response_data[start_url:end_url]
                time.sleep(0.1)
                urllib.request.urlretrieve(draft_url.replace("amp;", ''),
                                           str(id) + ".jpg")
                foto = open(str(id) + '.jpg', 'rb')
                os.remove(str(id) + ".jpg")
                bot.send_media_group(
                    message.chat.id,
                    [InputMediaPhoto(foto, tittle[0:tittle.find(':')])], None,
                    message.id)
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "property="og:image""')
            elif 'https://streamwo.com/' in response_data:
                draft_url = response_data[response_data.
                                          find('https://streamwo.com/'):]
                inner_url = draft_url[:draft_url.find('"')]
                req = urllib.request.Request(
                    inner_url,
                    data=None,
                    headers={
                        'User-Agent':
                        'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
                    })
                inner_url_response = urllib.request.urlopen(req)
                inner_response_data = inner_url_response.read().decode('utf-8')
                draft_url = inner_response_data[inner_response_data.
                                                find("source src=") + 12:]
                url = draft_url[:draft_url.find('"')]
                urllib.request.urlretrieve(url, str(id) + ".mp4")
                video = open(str(id) + '.mp4', 'rb')
                os.remove(str(id) + ".mp4")
                bot.send_media_group(
                    message.chat.id,
                    [InputMediaVideo(video, None, tittle[0:tittle.find(':')])],
                    None, message.id)
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "https://streamwo.com/')
            elif 'https://streamable.com/' in response_data:
                draft_url = response_data[response_data.
                                          find('https://streamable.com/'):]
                inner_url = draft_url[:draft_url.find('"')]
                inner_url_response = urllib.request.urlopen(inner_url)
                inner_response_data = inner_url_response.read().decode('utf-8')
                draft_url = inner_response_data[inner_response_data.
                                                find("og:video:secure_url") +
                                                30:]
                url = draft_url[:draft_url.find('"')]
                urllib.request.urlretrieve(url, str(id) + ".mp4")
                video = open(str(id) + '.mp4', 'rb')
                os.remove(str(id) + ".mp4")
                bot.send_media_group(
                    message.chat.id,
                    [InputMediaVideo(video, None, tittle[0:tittle.find(':')])],
                    None, message.id)
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "https://streamable.com/')
            elif 'youtu' in response_data:
                draft_url = response_data[response_data.
                                          find('https://www.youtu'):]
                if draft_url == ' ':
                    draft_url = response_data[response_data.
                                              find('https://youtu'):]
                if draft_url == ' ':
                    draft_url = response_data[response_data.
                                              find('https://m.youtu'):]
                inner_url = draft_url[:draft_url.find('"')]
                bot.reply_to(
                    message, tittle[0:tittle.find(':')] + '\n' +
                    inner_url.replace('amp;', ''))
                print(get_current_time() + " id: " + str(id) +
                      ' Success: YouTube')
            elif 'https://i.imgur.com/' in response_data:
                draft_url = response_data[response_data.
                                          find('https://i.imgur.com/'):]
                url = draft_url[:draft_url.find('"') - 4]
                bot.reply_to(message,
                             tittle[0:tittle.find(':')] + '\n' + url + 'mp4')
                print(get_current_time() + " id: " + str(id) +
                      ' Success: "https://i.imgur.com/"')
            else:
                file = open("logs_fails.txt", "a")
                file.write(get_current_time() + " id: " + str(id) + '\n' +
                           str(message.chat.id) + '\n' + url + '\n' + '\n')
                file.close()
                bot.reply_to(message,
                             "Supported content for extract not found")
                print(get_current_time() + " id: " + str(id) +
                      ' "Supported content for extract not found"')
        except Exception as e:
            bot.send_message(
                dev_chat_id,
                "Chat identity: " + chat_identity + '\n' + 'Error: ' + str(e))
            file = open("logs_errors.txt", "a")
            file.write(get_current_time() + " id: " + str(id) + '\n' +
                       str(message.chat.id) + '\n' + url + '\n' + str(e) +
                       '\n' + '\n')
            file.close()
            bot.reply_to(message, "something went wrong")
            print(get_current_time() + " id: " + str(id) +
                  ' something went wrong' + '\n' + str(e))