forked from joribom/musicjerk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
album.py
99 lines (84 loc) · 3.28 KB
/
album.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
from wikireader import get_wiki_summary, get_wiki_info
from spotifyreader import get_spotify_data
from discogsreader import get_genres
from urllib import parse
from threading import Lock, Thread
def make_url(album, artist):
return '%s-%s' % (album.lower().replace(" ", "_"), artist.lower().replace(" ", "_"))
class Album:
def __init__(self, title = None, artist = None, chosen_by = None,
rating = None, best_tracks = None, worst_tracks = None, debug = False):
self.title = title
self.artist = artist
self.mutex = Lock()
self.update_values(chosen_by, rating, best_tracks, worst_tracks)
self.debug = debug
self._spotify_id = None
self._image_url = None
self._summary = None
self._genres = ['Still fetching...']
self._styles = ['Still fetching...']
def update_api_values(self):
if self.title is not None and self.artist is not None:
if not self.debug:
print("Fetching info about %s - %s..." % (str(self.artist), str(self.title)))
spotify_id, image_url = get_spotify_data(self.title, self.artist)
if image_url is None:
summary, image_url = get_wiki_info(self.title, self.artist)
else:
summary = get_wiki_summary(self.title, self.artist)
with self.mutex:
self._spotify_id = spotify_id
self._image_url = image_url
self._summary = summary
def update_slow_api_values(self):
if not self.debug:
print("Fetching slow info about %s - %s..." % (str(self.artist), str(self.title)))
genres, styles = get_genres(self.title, self.artist)
with self.mutex:
self._genres = genres if genres else []
self._styles = styles if styles else []
def update_values(self, chosen_by, rating, best_tracks, worst_tracks):
self.chosen_by = chosen_by
self.rating = float(rating.replace(',', '.')) if rating else None
self.best_tracks = best_tracks
self.worst_tracks = worst_tracks
def add_value(self, header, value):
if header == "Rating":
self.rating = int(value) if value else None
elif header == "Worst Track(s)":
self.best_tracks = best_tracks
elif header == "Best Track(s)":
self.worst_tracks = worst_tracks
@property
def url_unparsed(self):
with self.mutex:
return make_url(self.title, self.artist)
@property
def url(self):
# Mutex is done with url_unparsed
return parse.quote(self.url_unparsed.encode('utf-8')).lower()
@property
def spotify_id(self):
with self.mutex:
return self._spotify_id
@property
def genres(self):
with self.mutex:
return self._genres
@property
def styles(self):
with self.mutex:
return self._styles
@property
def image_description(self):
with self.mutex:
return 'Album Cover (from Wikipedia)' if self._image_url is not None else self.title
@property
def summary(self):
with self.mutex:
return self._summary
@property
def image_url(self):
with self.mutex:
return self._image_url