Пример #1
0
    def test_equality(self):
        a = telegram.Audio(self.audio.file_id, self.audio.duration)
        b = telegram.Audio(self.audio.file_id, self.audio.duration)
        c = telegram.Audio(self.audio.file_id, 0)
        d = telegram.Audio("", self.audio.duration)
        e = telegram.Voice(self.audio.file_id, self.audio.duration)

        self.assertEqual(a, b)
        self.assertEqual(hash(a), hash(b))
        self.assertIsNot(a, b)

        self.assertEqual(a, c)
        self.assertEqual(hash(a), hash(c))

        self.assertNotEqual(a, d)
        self.assertNotEqual(hash(a), hash(d))

        self.assertNotEqual(a, e)
        self.assertNotEqual(hash(a), hash(e))
Пример #2
0
    def test_equality(self):
        a = telegram.StickerSet(self.name, self.title, self.contains_masks, self.stickers)
        b = telegram.StickerSet(self.name, self.title, self.contains_masks, self.stickers)
        c = telegram.StickerSet(self.name, None, None, None)
        d = telegram.StickerSet('blah', self.title, self.contains_masks, self.stickers)
        e = telegram.Audio(self.name, 0, None, None)

        self.assertEqual(a, b)
        self.assertEqual(hash(a), hash(b))
        self.assertIsNot(a, b)

        self.assertEqual(a, c)
        self.assertEqual(hash(a), hash(c))

        self.assertNotEqual(a, d)
        self.assertNotEqual(hash(a), hash(d))

        self.assertNotEqual(a, e)
        self.assertNotEqual(hash(a), hash(e))
Пример #3
0
    def url_message(self, bot, update):
        chat_id = update.message.chat_id
        user_name = update.message.from_user.first_name
        dt = datetime.datetime.now().strftime("%s")
        out_file = db_file + str(chat_id) + dt
        info_file = out_file + '.info.json'
        # parse message string to search youtube urls and stract ID
        try:
            url_id = get_url_id(update.message.text)
        except:
            bot.send_message(
                chat_id=chat_id,
                text=
                "Ups!\nSeems something went wrong while downloading the song\nCheck you sent me a valid youtube link"
            )

        # check if user exists, if not, register the user.
        self.user_exists(chat_id, user_name)

        # check if audio is on telegram server. consulting match between youtube id an file_id
        audio_file = self.db.get_file(url_id)
        if audio_file:
            file_record = audio_file
            self.db.update_record(url_id)
            t_audio = telegram.Audio(file_record['t_audio']['file_id'],
                                     file_record['t_audio']['duration'])
            filesize = ((int(float(file_record['filesize'])) / 1048576))
            bot.send_audio(
                chat_id=chat_id,
                audio=t_audio,
                caption="File size: {0:.2f} MB\nVia -> @Jutubot".format(
                    filesize),
                timeout=1000)
        else:
            message_info = bot.send_message(chat_id=chat_id,
                                            text='Downloading...',
                                            disable_notification='True')
            ydl_opts = {
                'outtmpl':
                out_file + '.%(ext)s',
                'writeinfojson':
                info_file,
                'format':
                'bestaudio',
                'postprocessors': [{
                    'key': 'FFmpegExtractAudio',
                    'preferredcodec': 'mp3',
                    'preferredquality': '192',
                }],
                'logger':
                MyLogger(),
                'progress_hooks': [my_hook]
            }

            # Download song from url_id
            with youtube_dl.YoutubeDL(ydl_opts) as ydl:
                try:
                    ydl.download([url_id])
                except Exception as e:
                    print(str(e))
                    bot.editMessageText(
                        chat_id=chat_id,
                        message_id=message_info['message_id'],
                        text=
                        "Ups!\nSeems something went wrong while downloading the song\nCheck you sent me a valid youtube link"
                    )

            out_file += '.mp3'
            data = parseInfoFile(info_file)
            os.remove(info_file)

            file_record = {
                '_id': url_id,
                'last_download': dt,
                'download_count': 1,
                **data
            }

            bot.editMessageText(chat_id=chat_id,
                                message_id=message_info['message_id'],
                                text='Sending...',
                                disable_notification='True')

            bot.send_chat_action(chat_id=chat_id,
                                 action='record_audio',
                                 timeout=100)

            tmp_send_file = db_file + file_record['title'] + '.mp3'
            shutil.copyfile(out_file, tmp_send_file)
            tag_file(tmp_send_file, file_record)
            filesize = ((int(float(file_record['filesize'])) / 1048576))

            # Send audio and save Telegram.Audio on t_audio
            t_audio = bot.send_audio(
                chat_id=chat_id,
                audio=open(tmp_send_file, 'rb'),
                title=file_record['title'],
                performer=file_record['performer'],
                caption="File size: {0:.2f} MB\nVia -> @Jutubot".format(
                    filesize),
                timeout=1000)['audio']

            file_record['t_audio'] = t_audio.to_dict()

            os.remove(tmp_send_file)
            os.remove(out_file)
            bot.delete_message(chat_id=chat_id,
                               message_id=message_info['message_id'])

        self.db.add_to_history(chat_id, file_record)
        try:
            self.db.insert_file_record(file_record)
        except Exception as e:
            print(str(e))
            pass