-
Notifications
You must be signed in to change notification settings - Fork 0
/
ID3update.py
111 lines (100 loc) · 3.83 KB
/
ID3update.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
import urllib2
import eyed3
import mechanize
import os
from bs4 import BeautifulSoup as bs
import unicodedata as ud
import sys
import string
reload(sys)
sys.setdefaultencoding('utf-8')
class Song:
def __init__(self, keyword, filename, albumart, aaformat, dd='/home/praneet/Music/'):
self.info = keyword.split('@')
self.filename = os.path.join(dd, filename).encode('utf-8')
self.keyword = urllib2.quote(('').join(self.info))
self.albumart = albumart
self.aaformat = aaformat
self.album = ''
self.artist = string.capwords(self.info[1])
self.title = self.info[0]
self.feat = ' '
self.genre = 'Unknown'
self.dd = dd
self.fetchID3()
def fetchID3(self):
browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.addheaders = [('User-agent','Mozilla')]
searchURL = "https://www.google.co.in/search?site=imghp&source=hp&biw=1414&bih=709&q="+urllib2.quote(self.title+' '+self.artist+' song')
html = browser.open(searchURL)
soup = bs(html, 'html.parser')
souplist = soup.findAll(attrs={'class':'_o0d'})
for i in range(1,len(souplist)):
if souplist[i].get_text().split(':')[0].lower() == 'album' or souplist[i].get_text().split(':')[0].lower() == 'movie':
self.album = souplist[i].get_text().split(':')[1]
print 'album ',souplist[i].get_text().split(':')[1]
elif souplist[i].get_text().split(':')[0].lower() == 'artist' or souplist[i].get_text().split(':')[0].lower() == 'artists':
self.artist = souplist[i].get_text().split(':')[1]
print 'artist ',souplist[i].get_text().split(':')[1]
elif souplist[i].get_text().split(':')[0].lower() == 'genre' or souplist[i].get_text().split(':')[0].lower() == 'genres':
self.genre = souplist[i].get_text().split(':')[1]
print 'genre ',souplist[i].get_text().split(':')[1]
elif souplist[i].get_text().split(':')[0].lower() == 'featured artist' or souplist[i].get_text().split(':')[0].lower() == 'featured artists':
self.feat = souplist[i].get_text().split(':')[1]
print 'featured artist ',souplist[i].get_text().split(':')[1]
else:
pass
self.fetchalbum()
def fetchalbum(self):
browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.addheaders = [('User-agent','Mozilla')]
searchURL = "https://www.google.co.in/search?site=imghp&source=hp&biw=1414&bih=709&q="+urllib2.quote(self.title+' '+self.artist+' album name')
html = browser.open(searchURL)
soup = bs(html, 'html.parser')
for i in soup.findAll(attrs={'class':'_B5d'}):
if self.album == '':
self.album = i.get_text()
print self.album
break
if self.album == '':
if not self.info[2].isspace() and self.info[2] != '':
self.album = string.capwords(self.info[2])
else:
self.album = self.title + '- Single'
print 'album', self.album
def updateID3(self):
audiofile = eyed3.load(self.filename)
try:
audiofile.tag.artist = unicode(self.artist, "utf-8")
except:
audiofile.tag.artist = self.artist
try:
audiofile.tag.album = unicode(self.album, "utf-8")
except:
audiofile.tag.album = self.album
title = ''
if self.feat == ' ':
title = self.title
else:
title = self.title+' ft. '+self.feat
try:
audiofile.tag.title = unicode(title, "utf-8")
except:
audiofile.tag.title = title
try:
audiofile.tag.genre = unicode(self.genre, "utf-8")
except:
audiofile.tag.genre = self.genre
audiofile.tag.images.set(3, open(self.albumart,'rb').read(), 'image/'+self.aaformat)
audiofile.tag.save()
if not os.path.isfile(self.dd+title+'.mp3'):
os.rename(self.filename, self.dd+title.rstrip()+'.mp3')
else:
newTitle = raw_input('Similar file already exits, enter new file name: ')
os.rename(self.filename, self.dd+newTitle.rstrip()+'.mp3')
print 'update complete'
os.remove(self.albumart)
# newsong = Song('Rockabye','Rockabye.mp3', 'rockabye','rockabye album art.jpeg','jpeg')
# newsong.updateID3()