-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyBinder.py
126 lines (108 loc) · 3.58 KB
/
pyBinder.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
import urllib as ul
import httplib as hl
from BeautifulSoup import BeautifulStoneSoup as bs
from mutagen.mp3 import MP3
from mutagen import id3 as mu
import os.path as op
FRONT_COVER = 3
ARTWORK_TAG = u'APIC:'
MIME_PNG = 'image/png'
MIME_JPEG = 'image/jpeg'
SIZE_34X34=b'small'
SIZE_64X64=b'medium'
SIZE_174X174=b'large'
SIZE_300X300=b'extralarge'
SIZE_RAW=b'mega'
ERROR_MSG=b'FAYUUUL!'
sApiKey = b'c8e1cd060b177d97acd579faa5a4ef5e'
sLfmUrl='ws.audioscrobbler.com'
def fDownload(sSource, sTarget):
'''
Downloads file from Internet.
sSource - source file URL
sTarget - target file name
'''
if sSource != ERROR_MSG:
fSourceImg = ul.urlopen(sSource)
try:
fTargetImg = open(sTarget, "wb")
# A silly workaround for image filw write permissions
# Happens a lot, so needs to be solved
except IOError:
from random import randint
import os.path
nFileSuffix = randint(100,500)
fTargetImg = open( \
os.path.splitext(sTarget)[0] + \
'_' + str(nFileSuffix) + \
os.path.splitext(sTarget)[1],\
"wb")
sData = fSourceImg.read()
fTargetImg.write(sData)
fTargetImg.close()
fSourceImg.close()
def fBindArtwork(sTrackAddr, sImgAddr, sArtAppend=u'frontcover'):
'''
Writes image data to mp3 file as APIC id3 frame.
sTrackAddr - audio file address;
sImgAddr - PNG/JPEG image file address;
'''
#Open an Mp3 file
muTrack=MP3(sTrackAddr)
#Open picture file
fPicture=file(sImgAddr, 'rb')
sPic=fPicture.read()
#Cheching image format
if op.splitext(sImgAddr)[-1].lower()=='png': sMimeType=MIME_PNG
elif (op.splitext(sImgAddr)[-1].lower()=='jpg'
or op.splitext(sImgAddr)[-1].lower()=='jpeg'
or op.splitext(sImgAddr)[-1].lower()=='jpe'): sMimeType=MIME_JPEG
else: sMimeType=''
#Creating a temporary APIC instance
sArtworkTag=ARTWORK_TAG+sArtAppend
muPic=mu.APIC(encoding=3, mime=MIME_JPEG, type=FRONT_COVER,
desc=u'Album front cover', data=0)
muTrack.tags[sArtworkTag]=muPic
#Write image data to mp3 file
muTrack.tags[sArtworkTag].data=sPic
muTrack.save(v1=2)
fPicture.close()
def fGetSongInfo(sSongLocation):
'''Gets Artist and Album data from an mp3 file.
Returns a string (Artist, Album) tuple
sSongLocation - location of the file'''
muTrack=MP3(sSongLocation)
sAlbum = muTrack.tags['TALB'].text
sArtist = muTrack.tags['TPE1'].text
return sArtist, sAlbum
def fCreateLfmRequest((lArtist, lAlbum)):
'''
Returns Last.FM API request
'''
print (lArtist, lAlbum)
sArtist=''.join(lArtist).encode('cp1251')
sAlbum=''.join(lAlbum).encode('cp1251')
sArtistTmp=ul.quote_plus(sArtist)
sAlbumTmp=ul.quote_plus(sAlbum)
dParams={"api_key" : sApiKey, 'artist' : sArtistTmp, 'album' : sAlbumTmp, 'method' : 'album.getinfo'}
return ul.unquote(ul.urlencode(dParams))
def fQueryLfm(sLfmRequest):
'''
Returns an XML result from Last.FM
'''
cCon=hl.HTTPConnection(sLfmUrl)
cCon.request('GET', '/2.0/?'+sLfmRequest) #????? ? ?????? ????????? =)
cResult = cCon.getresponse()
return cResult.read()
cCon.close()
def fUnparseImgUrl(sSrc, sSizeId):
'''
Gets an image URL from Last.FM querry result; returns URL of image ("large", 300x300).
'''
bsSoup=bs(sSrc)
if bsSoup.find('image', size=sSizeId)==None: return ERROR_MSG
else:
try:
return bsSoup.find('image', size=sSizeId).text
except:
return ERROR_MSG