-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSMD.py
204 lines (171 loc) · 4.97 KB
/
FSMD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from __future__ import unicode_literals
from __future__ import division
import urllib2
from bs4 import BeautifulSoup as bs #module for parsing html
import youtube_dl as ydl #youtube_dl for downloading youtube videos and mp3
from GIS import AlbumArt as gimg #custom module for fetching album arts
from ID3update import Song #custom module for fetchid metadata and updating the downloaded file
from gui import *
import string
import csv
import time
import os
import sys
import progressBar
pb = progressBar.progressor('linear')
reload(sys)
sys.setdefaultencoding('utf8')
#searching youtube for song
def search(keyword):
search = urllib2.quote(('').join(keyword.split('@')))
url = "https://www.youtube.com/results?search_query="+search
response = urllib2.urlopen(url)
html = response.read()
soup = bs(html, 'html.parser');
return soup
#get all links on page1 of youtube search
def grabLinks(parsed):
del songs[:]
for vid in parsed.findAll(attrs={'class':'yt-uix-tile-link'}):
if vid.find_next_sibling('span').get_text() == ' - Playlist' or vid.find_next_sibling('span').get_text() == ' - Channel':
pass
else:
songs.append({'title':vid['title'], 'link':vid['href']})
#show search results
def showSearchResults():
if execType == '-b':
print 'downloading --- '+songs[0]['title']
index = 0
return index
elif execType == '-d':
for i in range(0, 10):
try:
print i+1,'- ',songs[i]['title']
except:
pass
return input('which song to download? enter index \t')-1
#youtube-dl logger
class dl_logger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
#youtube-dl progress-hook
def prog_hook(d):
global filename
global pb
if d['status'] == 'finished':
pb.stop()
if d['filename'][-4:] == 'webm':
# global filename
filename = d['filename'][:-4]+'mp3'
elif d['filename'][-4:] == '.m4a':
# global filename
filename = d['filename'][:-3]+'mp3'
print('\n')
print 'download complete'
else:
percent = (d['downloaded_bytes']*100)//d['total_bytes']
pb.update(percent, d['eta'])
sys.stdout.flush()
#youtube-dl download
def downloadSong(i):
global pb
songObj = songs[i]
songLink = songObj['link']
songTitle = songObj['title']
with ydl.YoutubeDL(ydl_opts) as tube:
pb.start()
tube.download(['http://www.youtube.com'+songLink])
#download album art
def downloadAart(keyword, downloadDirectory):
newImg = gimg(keyword, downloadDirectory)
aapath = newImg.download()
return aapath
def update(keyword, filename, aapath, imgFormat, dd):
newSong = Song(keyword, filename, aapath, imgFormat, dd)
newSong.updateID3();
def execute(keyword, downloadDirectory, extra=''):
global pb
try:
parsed = search(keyword+extra)
grabLinks(parsed)
index = showSearchResults()
print('\n')
if execType == '-d':
try:
clear = lambda: os.system('clear')
clear()
except:
pass
else:
pass
downloadSong(index)
aapath = downloadAart(keyword, downloadDirectory)
imgFormat = aapath.split('.')[-1]
update(string.capwords(keyword), filename, aapath, imgFormat, downloadDirectory)
return True
except KeyboardInterrupt:
print 'Keyboard Interrupt. Now exiting'
print "Good Bye :')"
pb.stop()
sys.exit()
except:
print '\nSome unexpected error occured, Please try again :P'
try:
pb.stop()
except:
pass
return False
if __name__ == '__main__':
print('FSMD v2')
print('Author: Paneer\n')
time.sleep(1.5)
execType = '-d'
songs = []
batch = []
filename = 'Unknown'
downloadDirectory = '/home/praneet/Music/'
#youtube-dl options object
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': downloadDirectory+'%(title)s.%(ext)s',
'logger': dl_logger(),
'progress_hooks': [prog_hook],
'prefer_insecure':True
}
try:
execType = sys.argv[1]
batchCSV = sys.argv[2]
if execType == '-b':
if os.path.isfile(batchCSV):
with open(batchCSV, 'rb') as csvfile:
batchlist = csv.DictReader(csvfile)
for row in batchlist:
batch.append(row['Title'] + ' @' + row['Artist'] + ' @' + row['Album'])
for batchitem in batch:
print 'batchitem --- '+batchitem
execute(batchitem, downloadDirectory, ' official audio')
else:
print 'csv '+ batchCSV +' does not exist'
# print batchlist.types
else:
print 'invalid argument'
except:
if len(sys.argv)>1:
print '\nusage:\n\t', 'python FSMD.py <arg1> <arg2>\n\n','>>> if batch downloading, arg1 = -b and arg2 = path of .csv\n', '>>> for single file download no argument required, simply run python FSMD.py'
else:
title = raw_input('Enter song title:\n>>> ')
artist = raw_input('Enter artist name:\n>>> ')
album = raw_input('Enter album name:\n>>> ')
keyword = title + ' @' + artist + ' @' + album
execute(keyword, downloadDirectory)
#user input for song to download
#function calls