/
songfile.py
81 lines (69 loc) · 2.27 KB
/
songfile.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
from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3
import json
from exceptions import *
CACHE_VERSION = 1
class SongFile:
# Load id3 info from a file
def load(self,mp3path):
data = MP3(mp3path, ID3=EasyID3)
self.mp3path = mp3path
# Note, these are lists, not single values.
self.artist = data['artist']
self.album = data['album']
self.title = data['title']
# extract the track num. ie: '2/14' -> 2
try:
self.track = [ int(i.split('/')[0]) for i in data['tracknumber'] ]
except (KeyError):
self.track = []
# make a nice pretty string to print out
def __repr__(self):
allArtists = ', '.join(self.artist)
allAlbums = ', '.join(self.album)
allTitles = ', '.join(self.title)
allTracks = ', '.join([str(i) for i in self.track])
return '"{0} by {1} #{2} on {3}"'.format(allTitles, allArtists, allTracks, allAlbums)
# Use the hash of all the id3 info combined into a string
def __hash__(self):
s = ''.join(self.artist + self.album + self.title + [str(i) for i in self.track])
return hash(s)
# The has file that will be used for identifying json snippets
def jsonHash(self):
return hash(self.mp3path)
# Convert to json
def toJson(self):
data = self.data()
j = json.dumps(data)
return j
# From json
def fromJson(self, jsonData):
if int(jsonData['cache_version']) != CACHE_VERSION:
print jsonData['cache_version'], CACHE_VERSION
raise CacheVersionError(jsonData['cache_version'], CACHE_VERSION)
self.mp3path = jsonData['path']
self.artist = [jsonData['artist']]
self.album = [jsonData['album']]
self.title = [jsonData['title']]
self.track = [jsonData['track']]
# ignore the path, just worry about the artist,album,title, etc
def __eq__(self, other):
return self.__hash__() == other.__hash__()
# Generate a dictionary with all our data
def data(self, root=None, single=True):
data = {}
data['path'] = self.mp3path
data['cache_version'] = CACHE_VERSION
if root:
data['root'] = root
if single:
data['artist'] = ', '.join(self.artist)
data['album'] = ', '.join(self.album)
data['title'] = ', '.join(self.title)
data['track'] = ', '.join([str(i) for i in self.track])
else:
data['artist'] = self.artist
data['album'] = self.album
data['title'] = self.title
data['track'] = self.track
return data