/
mp3imager
executable file
·218 lines (177 loc) · 6.45 KB
/
mp3imager
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
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/python3
import subprocess, re, sys, stagger, urllib.request, os, base64
from stagger.id3 import *
# Amazon Settings
# SearchPrefix = 'http://www.amazon.co.uk/s/field-keywords='
# ProductImageRE = re.compile('.*<a href=\"http://www.amazon.co.uk/([^/]*)/.*<img[^>]*src=\"([^\"]*)"[^>]*alt=\"Product Details\".*')
# ProductDescGroup = 1
# ProductImageGroup = 2
# ImageBaseUrlRE = re.compile('(.*)\.[^.]*(\.[^.]*)')
# ImageBaseUrlGroup = 1
# ImageRestUrlGroup = 2
# ImgDimension = '._SL300'
# ImgSuffix = 'jpg'
# Retries = 5
# Last FM Settings
SearchPrefix = 'http://www.last.fm/search?q='
ProductImageRE = re.compile('.*<a href=\"([^\"]*)\".*<img\ *width=\"[^\"]*\"\ *src=\"([^\"]*)\"\ *class=\"art\"\ *class=\"albumCover[^\"]*\"\ */>.*')
ProductDescGroup = 1
ProductImageGroup = 2
AmazonResize = 0
ImageBaseUrlRE = re.compile('(.*serve/)[0-9a-z]*(/.*)')
ImageBaseUrlGroup = 1
ImageRestUrlGroup = 2
ImgDimension = '252'
ImgSuffix = 'png'
Retries = 1
# General Settings
CurlUserAgent = 'Mozilla/4.0'
Curl = 'curl'
CurlUserAgentFlag = '-A'
CurlSilent = '-s'
BufSize = 1000
FailImgUrl = 'NotFound'
Find = 'find'
FindRegExpFlag = '-regex'
FindMp3RegExp = '.*\\(mp3\\|MP3\\)'
# To remember what we already tried to download
# to avoid doing it for every song in the album...
tried_and_failed = set()
def GetURL(url):
print("Getting URL:", url)
return subprocess.Popen([Curl,
CurlSilent,
CurlUserAgentFlag,
CurlUserAgent,
url],
bufsize=BufSize,
stdout=subprocess.PIPE).stdout
def TakeFirstImage(page_data):
line = 'blah'
img_url = FailImgUrl
while (len(line) > 0) and (img_url == FailImgUrl):
try:
line = page_data.readline().decode('utf-8')
m = ProductImageRE.match(line)
if m:
img_url = m.group(ProductImageGroup)
except UnicodeDecodeError:
#print('Unicode decode error, ignoring line')
pass
return img_url
def GetCovers(page_data):
line = 'blah'
covers = []
while (len(line) > 0):
try:
line = page_data.readline().decode('utf-8')
m = ProductImageRE.match(line)
if m:
covers.append((m.group(ProductDescGroup),
m.group(ProductImageGroup)))
except UnicodeDecodeError:
#print('Unicode decode error, ignoring line')
pass
return covers
def AskImage(page_data):
covers = GetCovers(page_data)
for (desc, cover) in covers:
print(desc)
return FailImgUrl
def GetSimpleImgUrl(search_term):
# rawData = urllib.request.urlopen(SearchPrefix+search_term)
rawData = GetURL(SearchPrefix+search_term)
if choose_images:
return AskImage(rawData)
else:
return TakeFirstImage(rawData)
def MakeLarge(simple_url):
m = ImageBaseUrlRE.match(simple_url)
if m:
return (m.group(ImageBaseUrlGroup) +
ImgDimension +
m.group(ImageRestUrlGroup))
else:
#print('Error changing image size.')
return simple_url
def SearchTerm(artist, album):
both = artist + " " + album
return urllib.request.pathname2url(both)
def GetImageUrl(artist, album):
tries = 0
full_url = FailImgUrl
while (tries < Retries) and (full_url == FailImgUrl):
simple_url = GetSimpleImgUrl(SearchTerm(artist, album))
full_url = MakeLarge(simple_url)
tries+=1
return full_url
def SanitiseTag(tag):
return base64.urlsafe_b64encode(tag.encode("utf-8")).decode("utf-8")
def GetImageFileName(directory, artist, album):
return (directory + os.sep +
SanitiseTag(artist) + "-" +
SanitiseTag(album) +
"." + ImgSuffix)
def DoMp3(mp3_file):
try:
tag = stagger.read_tag(mp3_file)
if (len(tag.picture) > 0) and clobber_images:
pass
elif (len(tag.album) == 0) or (len(tag.artist) == 0):
print("One not found of " + tag.album + " and " + tag.artist)
elif ((tag.artist, tag.album) in tried_and_failed):
print("Already tried and failed",tag.artist,";",tag.album,";",tag.title)
else:
print("Doing " + tag.artist + "; " + tag.album + "; " + tag.title)
image_name = GetImageFileName(os.path.dirname(mp3_file),
tag.artist,
tag.album)
has_image = 1
if not os.path.exists(image_name):
print("Trying to download")
has_image = 0
full_url = GetImageUrl(tag.artist, tag.album)
if full_url != FailImgUrl:
rawImage = GetURL(full_url)
img_file = open(image_name, mode='wb')
img_file.write(rawImage.read())
img_file.close()
has_image = 1
else:
tried_and_failed.add((tag.artist, tag.album))
print("Failed... moving on.")
if has_image:
tag.picture = image_name
tag.write()
except stagger.NoTagError:
pass
def DoMp3s(directory):
print('Searching for MP3s in ' + directory)
rawData = subprocess.Popen([Find,
directory,
FindRegExpFlag,
FindMp3RegExp],
bufsize=BufSize,
stdout=subprocess.PIPE).stdout
line = 'blah'
while (len(line) > 0):
line = rawData.readline().decode('utf-8').strip()
if len(line) > 0:
DoMp3(line)
mp3dir = ''
clobber_images = 0
choose_images = 0
for arg in sys.argv[1:]:
if arg == '--clobber':
clobber_images == 1
elif arg == '--choose':
choose_images = 1
elif len(mp3dir) == 0:
mp3dir = arg
if len(mp3dir) > 0:
DoMp3s(mp3dir)
else:
print("usage: mp3imager <base-dir>")
print("warning: may trash your mp3s while trying to add images to them.")
print(" --clobber : override any existing embedded images")
print(" --choose : offer a menu of choices instead of taking the first one.")