async def youtube_download(_client, message): args = message.text.split(None, 2) await edrep(message, text="Checking") if len(args) == 1: await edrep(message, text="Write any args here!") return try: yt = YouTube(args[1]) except ValueError: await edrep(message, text="Invalid URL!") return if len(args) == 2: link = args[1] text = "🎬 **Title:** [{}]({})\n".format(escape_markdown(yt.title), link) YouTube(link).streams.first().download('nana/downloads', filename="tempvid") await app.send_video(message.chat.id, video="nana/downloads/tempvid.mp4") os.remove('nana/downloads/tempvid.mp4') await edrep(message, text=text, disable_web_page_preview=True) return if len(args) == 3: link = args[1] reso = args[2] text = "🎬 **Title:** [{}]({})\n".format(escape_markdown(yt.title), link) stream = yt.streams.filter(file_extension='mp4').filter(resolution="{}".format(reso)).first() stream.download('nana/downloads', filename="tempvid") await app.send_video(message.chat.id, video="nana/downloads/tempvid.mp4") os.remove('nana/downloads/tempvid.mp4') await edrep(message, text=text, disable_web_page_preview=True) return
async def youtube_download(client, message): args = message.text.split(None, 2) await message.edit("Checking") if len(args) == 1: await message.edit("Write any args here!") return try: yt = YouTube(args[1]) except ValueError: await message.edit("Invalid URL!") return if len(args) == 2: link = args[1] text = "🎬 **Title:** [{}]({})\n".format(escape_markdown(yt.title), link) status = "**Downloading video...**\n" await message.edit(status + text, disable_web_page_preview=True) YouTube(link).streams.first().download('nana/downloads', filename="tempvid") status = "**Uploading File To Telegram...**\n" await message.edit(status + text, disable_web_page_preview=True) await app.send_video(message.chat.id, video="nana/downloads/tempvid.mp4") status = "**Removing Temp File...**" await message.edit(status) os.remove('nana/downloads/tempvid.mp4') status = "** Done ✔️✔️**\n" await message.edit(status + text, disable_web_page_preview=True) return if len(args) == 3: link = args[1] reso = args[2] text = "🎬 **Title:** [{}]({})\n".format(escape_markdown(yt.title), link) status = "**Downloading video...**\n" await message.edit(status + text, disable_web_page_preview=True) stream = yt.streams.filter(file_extension='mp4').filter( resolution="{}".format(reso)).first() stream.download('nana/downloads', filename="tempvid") status = "**Uploading File To Telegram...**\n" await message.edit(status + text, disable_web_page_preview=True) await app.send_video(message.chat.id, video="nana/downloads/tempvid.mp4") status = "**Removing Temp File...**" await message.edit(status) os.remove('nana/downloads/tempvid.mp4') status = "**Done ✔️✔️**\n" await message.edit(status + text, disable_web_page_preview=True) return
async def no_longer_afk(client, message): if not DB_AVAIABLE: return global MENTIONED get = get_afk() if get and get['afk']: await client.send_message(message.from_user.id, "You are no longer afk!") set_afk(False, "") text = "You are no longer afk!\n**Total {} mentioned you**\n".format(len(MENTIONED)) for x in MENTIONED: msg_text = x["text"] if len(msg_text) >= 11: msg_text = "{}...".format(x["text"]) text += "- [{}](https://t.me/c/{}/{}) ({}): {}\n".format(escape_markdown(x["user"]), x["chat_id"], x["message_id"], x["chat"], msg_text) await client.send_message(Owner, text) MENTIONED = []
async def no_longer_afk(client, message): global MENTIONED get = get_afk() if get and get['afk']: await setbot.send_message(message.from_user.id, "Yeay! Sekarang kamu sudah tidak AFK!") set_afk(False, "") text = "**Total {} menyebutmu**\n".format(len(MENTIONED)) for x in MENTIONED: msg_text = x["text"] if len(msg_text) >= 11: msg_text = "{}...".format(x["text"]) text += "- [{}](https://t.me/c/{}/{}) ({}): {}\n".format( escape_markdown(x["user"]), x["chat_id"], x["message_id"], x["chat"], msg_text) await setbot.send_message(message.from_user.id, text) MENTIONED = []
async def youtube_music(_client, message): args = message.text.split(None, 1) if len(args) == 1: await message.reply("Send URL here!") return teks = args[1] try: video = pafy.new(teks) except ValueError: await message.reply("Invaild URL!") return try: audios = [audio for audio in video.audiostreams] audios.sort(key=lambda a: (int(a.quality.strip('k')) * -1)) music = audios[0] text = "[](https://i.ytimg.com/vi/{}/0.jpg)🎬 **Title:** [{}]({})\n".format( video.videoid, escape_markdown(video.title), video.watchv_url) text += "👤 **Author:** `{}`\n".format(video.author) text += "🕦 **Duration:** `{}`\n".format(video.duration) origtitle = re.sub(r'[\\/*?:"<>|\[\]]', "", str(music.title + "." + music._extension)) musictitle = re.sub(r'[\\/*?:"<>|\[\]]', "", str(music.title)) musicdate = video._ydl_info['upload_date'][:4] r = requests.get( f"https://i.ytimg.com/vi/{video.videoid}/maxresdefault.jpg", stream=True) if r.status_code != 200: r = requests.get( f"https://i.ytimg.com/vi/{video.videoid}/hqdefault.jpg", stream=True) if r.status_code != 200: r = requests.get( f"https://i.ytimg.com/vi/{video.videoid}/sddefault.jpg", stream=True) if r.status_code != 200: r = requests.get( f"https://i.ytimg.com/vi/{video.videoid}/mqdefault.jpg", stream=True) if r.status_code != 200: r = requests.get( f"https://i.ytimg.com/vi/{video.videoid}/default.jpg", stream=True) if r.status_code != 200: avthumb = False if r.status_code == 200: avthumb = True with open("nana/cache/thumb.jpg", "wb") as stk: shutil.copyfileobj(r.raw, stk) try: os.remove(f"nana/downloads/{origtitle}") except FileNotFoundError: pass # music.download(filepath="nana/downloads/{}".format(origtitle)) if "manifest.googlevideo.com" in music.url: download = await download_url(music._info['fragment_base_url'], origtitle) else: download = await download_url(music.url, origtitle) if download == "Failed to download file\nInvaild file name!": return await message.reply(download) try: subprocess.Popen("ffmpeg", stdout=subprocess.PIPE, stderr=subprocess.PIPE) except Exception as err: if "The system cannot find the file specified" in str( err) or "No such file or directory" in str(err): await message.reply( "You need to install ffmpeg first!\nCheck your assistant for more information!" ) await setbot.send_message( message.from_user.id, "Hello 🙂\nYou need to install ffmpeg to make audio works better, here is guide how to install it:\n\n**If you're using linux**, go to your terminal, type:\n`sudo apt install ffmpeg`\n\n**If you're using Windows**, download ffmpeg here:\n`https://ffmpeg.zeranoe.com/builds/`\nAnd then extract (if was archive), and place ffmpeg.exe to workdir (in current dir)\n\n**If you're using heroku**, type this in your workdir:\n`heroku buildpacks:add https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git`\nOr if you not using heroku term, follow this guide:\n1. Go to heroku.com\n2. Go to your app in heroku\n3. Change tabs/click Settings, then search for Buildpacks text\n4. Click button Add build pack, then type `https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest`\n5. Click Save changes, and you need to rebuild your heroku app to take changes!\n\nNeed help?\nGo @nanabotsupport and ask there" ) return if avthumb: os.system( f'ffmpeg -loglevel panic -i "nana/downloads/{origtitle}" -i "nana/cache/thumb.jpg" -map 0:0 -map 1:0 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" -metadata title="{music.title}" -metadata author="{video.author}" -metadata album="{video.author}" -metadata album_artist="{video.author}" -metadata genre="{video._category}" -metadata date="{musicdate}" -acodec libmp3lame -aq 4 -y "nana/downloads/{musictitle}.mp3"' ) else: os.system( f'ffmpeg -loglevel panic -i "nana/downloads/{origtitle}" -metadata title="{music.title}" -metadata author="{video.author}" -metadata album="{video.author}" -metadata album_artist="{video.author}" -metadata genre="{video._category}" -metadata date="{musicdate}" -acodec libmp3lame -aq 4 -y "nana/downloads/{musictitle}.mp3"' ) try: os.remove("nana/downloads/{}".format(origtitle)) except FileNotFoundError: pass getprev = requests.get(video.thumb, stream=True) with open("nana/cache/prev.jpg", "wb") as stk: shutil.copyfileobj(getprev.raw, stk) await app.send_audio(message.chat.id, audio=f"nana/downloads/{musictitle}.mp3", thumb="nana/cache/prev.jpg", title=music.title, caption=f"🕦 `{video.duration}`", reply_to_message_id=message.message_id) try: os.remove("nana/cache/prev.jpg") except FileNotFoundError: pass try: os.remove("nana/cache/thumb.jpg") except FileNotFoundError: pass titletext = "**Done! 🤗**\n" await message.reply(titletext + text, disable_web_page_preview=True) except Exception as err: if "command not found" in str(err) or "is not recognized" in str(err): await message.reply( "You need to install ffmpeg first!\nCheck your assistant for more information!" ) await setbot.send_message( message.from_user.id, "Hello 🙂\nYou need to install ffmpeg to make audio works better, here is guide " "how to install it:\n\n**If you're using linux**, go to your terminal, " "type:\n`sudo apt install ffmpeg`\n\n**If you're using Windows**, download " "ffmpeg here:\n`https://ffmpeg.zeranoe.com/builds/`\nAnd then extract (if was " "archive), and place ffmpeg.exe to workdir (in current dir)\n\n**If you're using " "heroku**, type this in your workdir:\n`heroku buildpacks:add " "https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git`\nOr if you " "not using heroku term, follow this guide:\n1. Go to heroku.com\n2. Go to your " "app in heroku\n3. Change tabs/click Settings, then search for Buildpacks " "text\n4. Click button Add build pack, then type " "`https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest`\n5. Click Save " "changes, and you need to rebuild your heroku app to take changes!\n\nNeed " "help?\nGo to @NanaBotSupport and ask there") return exc_type, exc_obj, exc_tb = sys.exc_info() errors = traceback.format_exception(etype=exc_type, value=exc_obj, tb=exc_tb) await message.reply( "**An error has accured!**\nCheck your assistant for more information!" ) button = InlineKeyboardMarkup([[ InlineKeyboardButton("🐞 Report bugs", callback_data="report_errors") ]]) await setbot.send_message(message.from_user.id, "**An error has accured!**\n```{}```".format( "".join(errors)), reply_markup=button) logging.exception("Execution error")
async def youtube_music(client, message): args = message.text.split(None, 1) if len(args) == 1: await message.edit("Send URL here!") return teks = args[1] try: video = pafy.new(teks) except ValueError: await message.edit("Invaild URL!") return try: audios = [audio for audio in video.audiostreams] audios.sort(key=lambda a: (int(a.quality.strip('k')) * -1)) music = audios[0] text = "🎬 Title: [{}]({})\n".format(escape_markdown(video.title), video.watchv_url) text += "👤 Author: `{}`\n".format(video.author) text += "🕦 Duration: `{}`\n".format(video.duration) origtitle = re.sub(r'[\\/*?:"<>|]', "", str(music.title + "." + music._extension)) musictitle = re.sub(r'[\\/*?:"<>|]', "", str(music.title)) titletext = "**Downloading music...**\n" await message.edit(titletext + text, disable_web_page_preview=True) r = requests.get("https://i.ytimg.com/vi/{}/maxresdefault.jpg".format( video.videoid), stream=True) if r.status_code != 200: r = requests.get("https://i.ytimg.com/vi/{}/hqdefault.jpg".format( video.videoid), stream=True) if r.status_code != 200: r = requests.get( "https://i.ytimg.com/vi/{}/sddefault.jpg".format( video.videoid), stream=True) if r.status_code != 200: r = requests.get( "https://i.ytimg.com/vi/{}/mqdefault.jpg".format( video.videoid), stream=True) if r.status_code != 200: r = requests.get( "https://i.ytimg.com/vi/{}/default.jpg".format( video.videoid), stream=True) if r.status_code != 200: avthumb = False if r.status_code == 200: avthumb = True with open("nana/cache/thumb.jpg", "wb") as stk: shutil.copyfileobj(r.raw, stk) try: os.remove("nana/downloads/{}".format(origtitle)) except FileNotFoundError: pass music.download(filepath="nana/downloads/{}".format(origtitle)) titletext = "**Converting music...**\n" await message.edit(titletext + text, disable_web_page_preview=True) if avthumb: os.system( f'ffmpeg -loglevel panic -i "nana/downloads/{origtitle}" -i "nana/cache/thumb.jpg" -map 0:0 -map 1:0 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (Front)" -metadata title="{music.title}" -metadata author="{video.author}" -metadata album_artist="{video.author}" -acodec libmp3lame -aq 4 -y "nana/downloads/{musictitle}.mp3"' ) else: os.system( f'ffmpeg -loglevel panic -i "nana/downloads/{origtitle}" -metadata title="{music.title}" -metadata author="{video.author}" -metadata album_artist="{video.author}" -acodec libmp3lame -aq 4 -y "nana/downloads/{musictitle}.mp3"' ) try: os.remove("nana/downloads/{}".format(origtitle)) except FileNotFoundError: pass titletext = "**Uploading...**\n" await message.edit(titletext + text, disable_web_page_preview=True) getprev = requests.get(video.thumb, stream=True) with open("nana/cache/prev.jpg", "wb") as stk: shutil.copyfileobj(getprev.raw, stk) await app.send_audio(message.chat.id, audio="nana/downloads/{}.mp3".format(musictitle), thumb="nana/cache/prev.jpg", title=music.title) os.remoev("nana/cache/prev.jpg") try: os.remove("nana/cache/thumb.jpg") except FileNotFoundError: pass titletext = "**Done! 🤗**\n" await message.edit(titletext + text, disable_web_page_preview=True) except Exception as err: if "command not found" in str(err) or "is not recognized" in str(err): await message.edit( "You need to install ffmpeg first!\nCheck your assistant for more information!" ) await setbot.send_message( message.from_user.id, "Hello 🙂\nYou need to install ffmpeg to make audio works better, here is guide how to install it:\n\n**If you're using linux**, go to your terminal, type:\n`sudo apt install ffmpeg`\n\n**If you're using Windows**, download ffmpeg here:\n`https://ffmpeg.zeranoe.com/builds/`\nAnd then extract (if was archive), and place ffmpeg.exe to workdir (in current dir)\n\n**If you're using heroku**, type this in your workdir:\n`heroku buildpacks:add https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest.git`\nOr if you not using heroku term, follow this guide:\n1. Go to heroku.com\n2. Go to your app in heroku\n3. Change tabs/click Settings, then search for Buildpacks text\n4. Click button Add build pack, then type `https://github.com/jonathanong/heroku-buildpack-ffmpeg-latest`\n5. Click Save changes, and you need to rebuild your heroku app to take changes!\n\nNeed help?\nGo @AyraSupport and ask there" ) return exc_type, exc_obj, exc_tb = sys.exc_info() errors = traceback.format_exception(etype=exc_type, value=exc_obj, tb=exc_tb) await message.edit( "**An error has accured!**\nCheck your assistant for more information!" ) button = InlineKeyboardMarkup([[ InlineKeyboardButton("🐞 Report bugs", callback_data="report_errors") ]]) await setbot.send_message(message.from_user.id, "**An error has accured!**\n```{}```".format( errors[-1]), reply_markup=button) logging.exception("Execution error")