|
| 1 | +""" |
| 2 | +
|
| 3 | + Written by Sagar Vakkala @ionicc |
| 4 | + Original repository: https://github.com/ionicc/Youtube-mp3-downloader-light |
| 5 | +
|
| 6 | +""" |
| 7 | + |
| 8 | +import re, urllib, os, sys |
| 9 | +import urllib.request |
| 10 | +import urllib.parse |
| 11 | + |
| 12 | +user_input = input |
| 13 | +encode = urllib.parse.urlencode |
| 14 | +retrieve = urllib.request.urlretrieve |
| 15 | +cleanup = urllib.request.urlcleanup() |
| 16 | +urlopen = urllib.request.urlopen |
| 17 | + |
| 18 | +def get_title(url): |
| 19 | + website = urlopen(url).read() |
| 20 | + title = str(website).split('<title>')[1].split('</title>')[0] |
| 21 | + return title |
| 22 | + |
| 23 | +def screen_clear(): |
| 24 | + if os.name == 'nt': |
| 25 | + os.system('cls') |
| 26 | + else: |
| 27 | + os.system('clear') |
| 28 | + |
| 29 | +def init_message(): |
| 30 | + print("Built with <3 By Sagar Vakkala (^^) \n") |
| 31 | + print("YOUTUBE MP3 DOWNLOADER LIGHT \n \n") |
| 32 | + |
| 33 | +def exit_message(t): |
| 34 | + print("\n %s Has been downloaded" % t) |
| 35 | + |
| 36 | + |
| 37 | +def download(song=None): |
| 38 | + if not song: |
| 39 | + song = user_input('Enter the name of the song or the URL: ') |
| 40 | + |
| 41 | + if "youtube.com/" not in song: |
| 42 | + |
| 43 | + try: |
| 44 | + query = encode({"search_query" : song}) |
| 45 | + web_content = urlopen("http://www.youtube.com/results?" + query) |
| 46 | + results = re.findall(r'href=\"\/watch\?v=(.{11})', web_content.read().decode()) |
| 47 | + except: |
| 48 | + print("There's some problem in your network") |
| 49 | + return none |
| 50 | + |
| 51 | + command = 'youtube-dl --embed-thumbnail --no-warnings --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" ' + results[0] |
| 52 | + |
| 53 | + else: |
| 54 | + command = 'youtube-dl --embed-thumbnail --no-warnings --extract-audio --audio-format mp3 -o "%(title)s.%(ext)s" ' + song[song.find("=")+1:] |
| 55 | + song = get_title(song) |
| 56 | + print(song) |
| 57 | + |
| 58 | + try: |
| 59 | + print("Downloading %s" % song) |
| 60 | + os.system(command) |
| 61 | + exit_message(song) |
| 62 | + download() |
| 63 | + except: |
| 64 | + print('Error downloading %s' %song) |
| 65 | + return None |
| 66 | + |
| 67 | +def main(): |
| 68 | + try: |
| 69 | + screen_clear() |
| 70 | + init_message() |
| 71 | + download() |
| 72 | + except KeyboardInterrupt: |
| 73 | + exit(1) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + main() |
| 78 | + exit(0) |
0 commit comments