From 1637b28178ce4f0a5e24388bab4fdcaa0b7e0fc2 Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Fri, 15 Jul 2016 22:51:37 +0530 Subject: [PATCH] fixed bugs and improved stability --- YTMusic.egg-info/PKG-INFO | 10 ++ YTMusic.egg-info/SOURCES.txt | 6 + YTMusic.egg-info/dependency_links.txt | 1 + YTMusic.egg-info/top_level.txt | 1 + bin/YTMusic | 166 ++++++++++++++++++++++++++ build/scripts-2.7/YTMusic | 166 ++++++++++++++++++++++++++ dist/YTMusic-0.1-py2.7.egg | Bin 0 -> 2683 bytes setup.py | 18 +++ 8 files changed, 368 insertions(+) create mode 100644 YTMusic.egg-info/PKG-INFO create mode 100644 YTMusic.egg-info/SOURCES.txt create mode 100644 YTMusic.egg-info/dependency_links.txt create mode 100644 YTMusic.egg-info/top_level.txt create mode 100644 bin/YTMusic create mode 100644 build/scripts-2.7/YTMusic create mode 100644 dist/YTMusic-0.1-py2.7.egg create mode 100644 setup.py diff --git a/YTMusic.egg-info/PKG-INFO b/YTMusic.egg-info/PKG-INFO new file mode 100644 index 0000000..ac76ee0 --- /dev/null +++ b/YTMusic.egg-info/PKG-INFO @@ -0,0 +1,10 @@ +Metadata-Version: 1.0 +Name: YTMusic +Version: 0.1 +Summary: Download songs just by entering the song name and artist. +Home-page: https://www.github.com/Ritiek/YTMusic/ +Author: Ritiek Malhotra +Author-email: ritiekmalhotra123@gmail.com +License: UNKNOWN +Description: UNKNOWN +Platform: UNKNOWN diff --git a/YTMusic.egg-info/SOURCES.txt b/YTMusic.egg-info/SOURCES.txt new file mode 100644 index 0000000..f97069d --- /dev/null +++ b/YTMusic.egg-info/SOURCES.txt @@ -0,0 +1,6 @@ +setup.py +YTMusic.egg-info/PKG-INFO +YTMusic.egg-info/SOURCES.txt +YTMusic.egg-info/dependency_links.txt +YTMusic.egg-info/top_level.txt +bin/YTMusic \ No newline at end of file diff --git a/YTMusic.egg-info/dependency_links.txt b/YTMusic.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/YTMusic.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/YTMusic.egg-info/top_level.txt b/YTMusic.egg-info/top_level.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/YTMusic.egg-info/top_level.txt @@ -0,0 +1 @@ + diff --git a/bin/YTMusic b/bin/YTMusic new file mode 100644 index 0000000..c890c26 --- /dev/null +++ b/bin/YTMusic @@ -0,0 +1,166 @@ +#!/bin/python + +import mechanize +from bs4 import BeautifulSoup as soup +import pafy +import os + +if not os.path.exists("Music"): + os.makedirs("Music") +print '' + +avconv = 1 + +if os.name == 'nt': + opsys = 'win' + if not os.path.isfile('ffmpeg.exe'): + print 'avconv not found, will not convert to mp3 (from m4a). install libav-tools and then try again' + avconv = 0 +else: + opsys = 'linux' + check_install = os.system('which avconv > /dev/null') + if check_install == '': + print 'avconv not found, will not convert to mp3 (from m4a). use sudo apt-get install libav-tools and then try again' + avonv = 0 + +def Main(): + script_dir = os.getcwd() + Title = '' + while True: + try: + print('') + print('') + raw_song = raw_input('Enter song to download: ') + if raw_song == "exit": + exit() + elif raw_song == "play": + if not Title == '': + if opsys == 'win': + if os.path.isfile(script_dir + "\Music\\" + Unencoded_Title + ".mp3"): + os.system('"' + script_dir + "\Music\\" + Unencoded_Title + ".mp3" + '"') + else: + os.system('"' + script_dir + "\Music\\" + Unencoded_Title + ".m4a" + '"') + elif opsys == 'linux': + if os.path.isfile(script_dir + '/Music/' + Unencoded_Title + '.mp3'): + os.system('aplay "' + script_dir + '/Music/' + Unencoded_Title + '.mp3"') + else: + os.system('aplay "' + script_dir + '/Music/' + Unencoded_Title + '.m4a"') + + else: + print 'No log to read from..' + elif raw_song == "convert": + y = 1 + x = 0 + print '' + if avconv == 1: + for m in os.listdir(script_dir + '/Music/'): + if m.endswith(".m4a"): + x = x + 1 + print 'Total songs to convert = ' + str(x) + ' songs' + for m in os.listdir(script_dir + '/Music/'): + if m.endswith(".m4a"): + print '' + print str(y) + '. ' 'Converting ' + m + ' to mp3..' + y = y + 1 + song = m.replace(".m4a", "") + os.system('sudo avconv -loglevel 0 -i "Music/' + song + '.m4a" ' + '"Music/' + song + '.mp3"') + os.remove("Music/" + m) + else: + print 'ffmpeg is not installed, cant convert to mp3' + elif raw_song == "spotify": + print '' + f = open('Music/spotify.txt') + lines = f.readlines() + print 'Total songs in spotify.txt = ' + str(len(lines)) + ' songs' + y = 1 + for x in lines: + print '' + song = x.replace(" ", "%20") + br = mechanize.Browser() + br.set_handle_robots(False) + br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")] + URL = "https://www.youtube.com/results?search_query=" + song + items = br.open(URL).read() + + items_parse = soup(items, "html.parser") + br.close() + first_result = items_parse.find(attrs={'class':'yt-uix-tile-link'})['href'] + + full_link = "youtube.com" + first_result + #print full_link + + video = pafy.new(full_link) + Unencoded_Title = ((video.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")) + Title = Unencoded_Title.encode("utf-8") + print str(y) + '. ' + Title + y = y + 1 + if os.path.exists("Music/" + Unencoded_Title + ".m4a.temp"): + os.remove("Music/" + Unencoded_Title + ".m4a.temp") + if os.path.exists("Music/" + Unencoded_Title + ".m4a") or os.path.exists("Music/" + Unencoded_Title + ".mp3"): + with open('Music/spotify.txt', 'r') as fin: + data = fin.read().splitlines(True) + with open('Music/spotify.txt', 'w') as fout: + fout.writelines(data[1:]) + else: + audiostreams = video.audiostreams + for a in audiostreams: + if a.bitrate == "128k" and a.extension == "m4a": + a.download(filepath="Music/" + Unencoded_Title + ".m4a") + with open('Music/spotify.txt', 'r') as fin: + data = fin.read().splitlines(True) + with open('Music/spotify.txt', 'w') as fout: + fout.writelines(data[1:]) + print '' + + else: + song = raw_song.replace(" ", "%20") + + br = mechanize.Browser() + br.set_handle_robots(False) + br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")] + URL = "https://www.youtube.com/results?search_query=" + song + items = br.open(URL).read() + + items_parse = soup(items, "html.parser") + br.close() + first_result = items_parse.find(attrs={'class':'yt-uix-tile-link'})['href'] + + full_link = "youtube.com" + first_result + #print full_link + + video = pafy.new(full_link) + Unencoded_Title = ((video.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")) + Title = Unencoded_Title.encode("utf-8") + print Title + trespass = 0 + if os.path.exists("Music/" + Unencoded_Title + ".m4a.temp"): + os.remove("Music/" + Unencoded_Title + ".m4a.temp") + + if os.path.exists("Music/" + Unencoded_Title + ".m4a") or os.path.exists("Music/" + Unencoded_Title + ".mp3"): + prompt = raw_input('Song with same name has already been downloaded.. re-download? (y/n/play): ') + if prompt == "y": + if os.path.exists("Music/" + Unencoded_Title + ".mp3"): + os.remove("Music/" + Unencoded_Title + ".mp3") + else: + os.remove("Music/" + Unencoded_Title + ".m4a") + audiostreams = video.audiostreams + for a in audiostreams: + if a.bitrate == "128k" and a.extension == "m4a": + a.download(filepath="Music/" + Unencoded_Title + ".m4a") + print '' + elif prompt =="play": + print 'Playing: ' + Title + os.system('aplay "' + script_dir + "\Music\\" + Unencoded_Title + ".m4a" + '"') + else: + pass + + else: + audiostreams = video.audiostreams + for a in audiostreams: + if a.bitrate == "128k" and a.extension == "m4a": + a.download(filepath="Music/" + Unencoded_Title + ".m4a") + print '' + except KeyboardInterrupt: + pass + +Main() diff --git a/build/scripts-2.7/YTMusic b/build/scripts-2.7/YTMusic new file mode 100644 index 0000000..15d020a --- /dev/null +++ b/build/scripts-2.7/YTMusic @@ -0,0 +1,166 @@ +#!/usr/bin/python + +import mechanize +from bs4 import BeautifulSoup as soup +import pafy +import os + +if not os.path.exists("Music"): + os.makedirs("Music") +print '' + +avconv = 1 + +if os.name == 'nt': + opsys = 'win' + if not os.path.isfile('ffmpeg.exe'): + print 'avconv not found, will not convert to mp3 (from m4a). install libav-tools and then try again' + avconv = 0 +else: + opsys = 'linux' + check_install = os.system('which avconv > /dev/null') + if check_install == '': + print 'avconv not found, will not convert to mp3 (from m4a). use sudo apt-get install libav-tools and then try again' + avonv = 0 + +def Main(): + script_dir = os.getcwd() + Title = '' + while True: + try: + print('') + print('') + raw_song = raw_input('Enter song to download: ') + if raw_song == "exit": + exit() + elif raw_song == "play": + if not Title == '': + if opsys == 'win': + if os.path.isfile(script_dir + "\Music\\" + Unencoded_Title + ".mp3"): + os.system('"' + script_dir + "\Music\\" + Unencoded_Title + ".mp3" + '"') + else: + os.system('"' + script_dir + "\Music\\" + Unencoded_Title + ".m4a" + '"') + elif opsys == 'linux': + if os.path.isfile(script_dir + '/Music/' + Unencoded_Title + '.mp3'): + os.system('aplay "' + script_dir + '/Music/' + Unencoded_Title + '.mp3"') + else: + os.system('aplay "' + script_dir + '/Music/' + Unencoded_Title + '.m4a"') + + else: + print 'No log to read from..' + elif raw_song == "convert": + y = 1 + x = 0 + print '' + if avconv == 1: + for m in os.listdir(script_dir + '/Music/'): + if m.endswith(".m4a"): + x = x + 1 + print 'Total songs to convert = ' + str(x) + ' songs' + for m in os.listdir(script_dir + '/Music/'): + if m.endswith(".m4a"): + print '' + print str(y) + '. ' 'Converting ' + m + ' to mp3..' + y = y + 1 + song = m.replace(".m4a", "") + os.system('sudo avconv -loglevel 0 -i "Music/' + song + '.m4a" ' + '"Music/' + song + '.mp3"') + os.remove("Music/" + m) + else: + print 'ffmpeg is not installed, cant convert to mp3' + elif raw_song == "spotify": + print '' + f = open('Music/spotify.txt') + lines = f.readlines() + print 'Total songs in spotify.txt = ' + str(len(lines)) + ' songs' + y = 1 + for x in lines: + print '' + song = x.replace(" ", "%20") + br = mechanize.Browser() + br.set_handle_robots(False) + br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")] + URL = "https://www.youtube.com/results?search_query=" + song + items = br.open(URL).read() + + items_parse = soup(items, "html.parser") + br.close() + first_result = items_parse.find(attrs={'class':'yt-uix-tile-link'})['href'] + + full_link = "youtube.com" + first_result + #print full_link + + video = pafy.new(full_link) + Unencoded_Title = ((video.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")) + Title = Unencoded_Title.encode("utf-8") + print str(y) + '. ' + Title + y = y + 1 + if os.path.exists("Music/" + Unencoded_Title + ".m4a.temp"): + os.remove("Music/" + Unencoded_Title + ".m4a.temp") + if os.path.exists("Music/" + Unencoded_Title + ".m4a") or os.path.exists("Music/" + Unencoded_Title + ".mp3"): + with open('Music/spotify.txt', 'r') as fin: + data = fin.read().splitlines(True) + with open('Music/spotify.txt', 'w') as fout: + fout.writelines(data[1:]) + else: + audiostreams = video.audiostreams + for a in audiostreams: + if a.bitrate == "128k" and a.extension == "m4a": + a.download(filepath="Music/" + Unencoded_Title + ".m4a") + with open('Music/spotify.txt', 'r') as fin: + data = fin.read().splitlines(True) + with open('Music/spotify.txt', 'w') as fout: + fout.writelines(data[1:]) + print '' + + else: + song = raw_song.replace(" ", "%20") + + br = mechanize.Browser() + br.set_handle_robots(False) + br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")] + URL = "https://www.youtube.com/results?search_query=" + song + items = br.open(URL).read() + + items_parse = soup(items, "html.parser") + br.close() + first_result = items_parse.find(attrs={'class':'yt-uix-tile-link'})['href'] + + full_link = "youtube.com" + first_result + #print full_link + + video = pafy.new(full_link) + Unencoded_Title = ((video.title).replace("\\", "_").replace("/", "_").replace(":", "_").replace("*", "_").replace("?", "_").replace('"', "_").replace("<", "_").replace(">", "_").replace("|", "_").replace(" ", "_")) + Title = Unencoded_Title.encode("utf-8") + print Title + trespass = 0 + if os.path.exists("Music/" + Unencoded_Title + ".m4a.temp"): + os.remove("Music/" + Unencoded_Title + ".m4a.temp") + + if os.path.exists("Music/" + Unencoded_Title + ".m4a") or os.path.exists("Music/" + Unencoded_Title + ".mp3"): + prompt = raw_input('Song with same name has already been downloaded.. re-download? (y/n/play): ') + if prompt == "y": + if os.path.exists("Music/" + Unencoded_Title + ".mp3"): + os.remove("Music/" + Unencoded_Title + ".mp3") + else: + os.remove("Music/" + Unencoded_Title + ".m4a") + audiostreams = video.audiostreams + for a in audiostreams: + if a.bitrate == "128k" and a.extension == "m4a": + a.download(filepath="Music/" + Unencoded_Title + ".m4a") + print '' + elif prompt =="play": + print 'Playing: ' + Title + os.system('aplay "' + script_dir + "\Music\\" + Unencoded_Title + ".m4a" + '"') + else: + pass + + else: + audiostreams = video.audiostreams + for a in audiostreams: + if a.bitrate == "128k" and a.extension == "m4a": + a.download(filepath="Music/" + Unencoded_Title + ".m4a") + print '' + except KeyboardInterrupt: + pass + +Main() diff --git a/dist/YTMusic-0.1-py2.7.egg b/dist/YTMusic-0.1-py2.7.egg new file mode 100644 index 0000000000000000000000000000000000000000..e6f7365a52a7a7c787fc4249317e4890d0a3d8e2 GIT binary patch literal 2683 zcmZ`*c{r5o8~%pu$`X-1vKy2&q*OC#7-X3&Wf>C_GbUr1jFb=!p-ehr!m(w|E*YW; z$&}ql$x=v^k}N078NYMYnL7QR?|QEHdavud@Ap~$xNln+`!*2(0JwpQs(FhPPR4$A z<_*NmUCe}-n}aRkP#dkF0Gzu&Dg@=P8AJ$r!v@&?1bc0hNqx%vU5<$pVujhl{=q|% zI^i^JLCEq3X*F_a`y7?6kZ-RAi9njv-93?AA^idgSts@idJq$XWViDyshn)Idny$b z<1+6Me@7#ns;h`E*yx8d-q#b7X8b*6r?sJTd)l?VSJa%x2%Nk}uF`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YSxoL-Ql!`H3H+f}tJZGOr2A@98%FPhzK6P9A4GA5^oh0owmk+naI@{L$ z$uP}#&gKtjG>XA?CrUytQE3VA7&`JyseaC(oDz?=V>Dm%x0;iSV8Bw=>w@Syg_?B6 zBYd%Emi@!oeQk=>R3vzK;O5|5ME~9n(i5HfT?Y`&K)RdSs>x~0r?3FTrqETS`7v-} zN^mMD`LcSOr;pl5+U}3^@EV;jUPMko0q-6^npuC4;xaZG$e+IK4{=zk9EwF=7UhoNnfbIqNM5YKstt76y7vmbPmnps^a2l$3`^hY~J~eJ$AXJBIyVc%3>Xf7~q8%*TKA3(n z#_~Q~pf0_M*qAT1(ObP+)uhNc0KA+)E44G!=gd@R1DV>Nky_i(afgfat=g=-Dx58v zQIMdehcd=wZDAlb5%&M@XqaO4zsQZ;I)3Q&PyXgtj$iG~KMVkn8)?AG@!jmO!hdHH z{)KBZ{nC#TSS-V@46I_kCGX!OH}XC!!*{V~h5zd6wzU3x=ncq$3lil1~x4!`=j%ls{ literal 0 HcmV?d00001 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8802b44 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +#!/usr/bin/python + +import sys +import os + +if sys.version_info > (3,0): + sys.exit("YTMusic requires python 2.") + +from setuptools import setup, find_packages + +setup(name='YTMusic', + version='0.1', + description='Download songs just by entering the song name and artist.', + author='Ritiek Malhotra', + author_email='ritiekmalhotra123@gmail.com', + url='https://www.github.com/Ritiek/YTMusic/', + scripts=['bin/YTMusic'], + )