from __future__ import unicode_literals
import os
import re
import logging
from unicodedata import normalize
import requests
import youtube_dl
from bs4 import BeautifulSoup
from telegram.ext import Updater, CommandHandler, MessageHandler,
Filters, CallbackContext
#from html.parser import HTMLParser
#from selenium import webdriver
logging.basicConfig(filename='sample.log',
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level = logging.INFO)
def normalize_special_char(txt):
return normalize('NFKD', txt).encode('ASCII',
'ignore').decode('ASCII')
def search_youtube(text):
url = 'https://www.youtube.com'
r = requests.get(url + '/results', params={'search_query': text})
soap = BeautifulSoup(r.content, 'html.parser')
for tag in soap.find_all('a', {'rel': 'spf-prefetch'}):
title, video_url = tag.text, url + tag['href']
if 'googleads' not in video_url:
return normalize_special_char(title), video_url
def download(title, video_url):
ydl_opts = {
'outtmpl': '{}.%(ext)s'.format(title),
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
return {
'audio': open(title + '.mp3', 'rb'),
'title': title,
}
def start(update, context):
update.message.reply_text('YouTube Music Downloader')
def music(bpt, update, context):
title, video_url = search_youtube(update.message.text)
update.message.reply_text('Start download ' + title)
music_dict = download(title, video_url)
update.message.reply_text('Convert to mp3 ' + title)
update.message.reply_audio(**music_dict, timeout=9999)
os.remove(title + '.mp3')
def main():
u = Updater('my api')
dp = u.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(MessageHandler(Filters.text, music))
u.start_polling()
u.idle()
if __name__ == '__main__':
main()