YoutubeToMp3.py 文件源码

python
阅读 17 收藏 0 点赞 0 评论 0

项目:youtubetomp3bot 作者: pcanapa 项目源码 文件源码
def downloader(self, message, chat_id, user):
        try:
            curr_dir = self.working_dir + '/' + str(chat_id).replace("-", "")
            os.chdir(curr_dir)
            RequestUrl = 'http://www.youtubeinmp3.com/fetch/index.php?format=json&video=%s' % (message)
            JsonVideoData = json.load(urlopen(RequestUrl))
            self.bot.sendMessage(chat_id=chat_id, text = 'Downloading ' +  JsonVideoData['title'] + '...', reply_markup = self.kb_markup);
            f = urlopen(JsonVideoData['link'], JsonVideoData['title'].replace('/', '').encode('utf-8').strip() + '.mp3')
            fulltitle = (curr_dir + '/' + JsonVideoData['title'].replace('/', '') + '.mp3')
            fulltitle = unicodedata.normalize('NFKD', fulltitle).encode('utf-8').strip()
            with open(fulltitle, 'wb') as mp3:
                mp3.write(f.read());
            with open(fulltitle) as mp3:
                self.bot.sendAudio(chat_id=chat_id, audio=mp3)
            os.remove(fulltitle);
            with open('log', 'a') as f:
                f.write(message + " " + str(datetime.now().time()))
                f.write("\n");
                f.close()

            url_data = urlparse.urlparse(message)
            if url_data.hostname == 'youtu.be':
                video = url_data.path[1:]   
            else:
                query = urlparse.parse_qs(url_data.query)
                video = query["v"][0]

            VideosUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&relatedToVideoId=%s&type=video&key=xx' % video
            VideosJson = json.load(urlopen(VideosUrl))

            videokb = [[telegram.KeyboardButton('https://www.youtube.com/watch?v='+VideosJson['items'][0]['id']['videoId'])], [telegram.KeyboardButton('https://www.youtube.com/watch?v='+VideosJson['items'][1]['id']['videoId'])], [telegram.KeyboardButton('https://www.youtube.com/watch?v='+VideosJson['items'][2]['id']['videoId'])], [telegram.KeyboardButton('Offer me a coffee'), telegram.KeyboardButton('Source Code'), telegram.KeyboardButton('Vote Me')] ]
            videokb_markup = telegram.ReplyKeyboardMarkup(videokb, resize_keyboard=True)

            self.bot.sendMessage(chat_id=chat_id, text = 'Here on the keyboard there are some related videos, in order:\n\n1) ' + VideosJson['items'][0]['snippet']['title'] + '\n\n2) ' + VideosJson['items'][1]['snippet']['title'] + '\n\n3) ' + VideosJson['items'][2]['snippet']['title'], reply_markup = videokb_markup);


        except ValueError as e:
            self.bot.sendMessage(chat_id=chat_id, text = 'Video not found on the database of www.youtubeinmp3.com, but you can download the mp3 (and update the database) by using this link: ');
            RequestUrl = 'http://www.youtubeinmp3.com/download/?video=%s' % (message)
            self.bot.sendMessage(chat_id=chat_id, text = RequestUrl)
        except Exception as e:
            if str(e) != "101":                 
                self.bot.sendMessage(chat_id=chat_id, text = 'Something went wrong, maybe the video does not exists,\nif the error persist send the code in the next message to the developer, Telegram: @aakagoree')
                self.bot.sendMessage(chat_id=chat_id, text = str(chat_id))
                #print str(e)
            with open('log', 'a') as f:
                f.write("!!EXCEPTION!!! " + message + " " + str(datetime.now().time()) + " " + str(e))
                f.write("\n");
                f.close()           
        finally:
            os.chdir(self.working_dir)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号