16
16
import os
17
17
18
18
import headphones
19
- from headphones import db , helpers , logger , lastfm , request , mb
19
+ from headphones import db , helpers , logger , lastfm , request , mb , os
20
+ from fanart .music import Artist
20
21
21
22
LASTFM_API_KEY = "690e1ed3bc00bc91804cd8f7fe5ed6d4"
22
23
24
+ os .environ .setdefault ('FANART_APIKEY' , '1f081b32bcd780219f4e6d519f78e37e' )
23
25
24
26
class Cache (object ):
25
27
"""
@@ -106,22 +108,6 @@ def _is_current(self, filename=None, date=None):
106
108
else :
107
109
return False
108
110
109
- def _get_thumb_url (self , data ):
110
-
111
- thumb_url = None
112
-
113
- try :
114
- images = data [self .id_type ]['image' ]
115
- except KeyError :
116
- return None
117
-
118
- for image in images :
119
- if image ['size' ] == 'medium' and '#text' in image :
120
- thumb_url = image ['#text' ]
121
- break
122
-
123
- return thumb_url
124
-
125
111
def get_artwork_from_cache (self , ArtistID = None , AlbumID = None ):
126
112
"""
127
113
Pass a musicbrainz id to this function (either ArtistID or AlbumID)
@@ -211,39 +197,67 @@ def get_image_links(self, ArtistID=None, AlbumID=None):
211
197
if ArtistID :
212
198
213
199
self .id_type = 'artist'
214
- data = lastfm .request_lastfm ("artist.getinfo" , mbid = ArtistID , api_key = LASTFM_API_KEY )
200
+ data = Artist .get (id = ArtistID )
201
+ logger .debug ('Fanart.tv ArtistID: %s' , ArtistID )
215
202
216
203
if not data :
204
+ logger .debug ('Fanart.tv ArtistID not found' )
217
205
return
218
206
219
- try :
220
- image_url = data ['artist' ]['image' ][- 1 ]['#text' ]
221
- except (KeyError , IndexError ):
222
- logger .debug ('No artist image found' )
223
- image_url = None
207
+ artist_url = None
208
+ thumb_url = None
209
+ image_url = None
210
+
211
+ if data .thumbs :
212
+ for thumbs in data .thumbs [0 :1 ]:
213
+ artist_url = str (thumbs .url )
214
+
215
+ if artist_url :
216
+ thumb_url = artist_url .replace ('fanart/' , 'preview/' )
217
+ image_url = thumb_url
218
+
219
+ if not image_url :
220
+ logger .debug ('Fanart.tv no artist image found' )
221
+ else :
222
+ logger .debug ('Fanart.tv artist url: %s' , image_url )
224
223
225
- thumb_url = self ._get_thumb_url (data )
226
224
if not thumb_url :
227
- logger .debug ('No artist thumbnail image found' )
225
+ logger .debug ('Fanart.tv no artist thumbnail image found' )
226
+ else :
227
+ logger .debug ('Fanart.tv artist thumb url: %s' , thumb_url )
228
228
229
229
else :
230
230
231
+ album_url = None
232
+ thumb_url = None
233
+ image_url = None
234
+
231
235
self .id_type = 'album'
232
- data = lastfm .request_lastfm ("album.getinfo" , mbid = AlbumID , api_key = LASTFM_API_KEY )
236
+ data = Artist .get (id = "ArtistID" )
237
+ logger .debug ('Fanart.tv AlbumID: %s' , AlbumID )
233
238
234
239
if not data :
240
+ logger .debug ('Fanart.tv artist lookup failed' )
235
241
return
236
242
237
- try :
238
- image_url = data ['album' ]['image' ][- 1 ]['#text' ]
239
- except (KeyError , IndexError ):
240
- logger .debug ('No album image found on last.fm' )
241
- image_url = None
243
+ if data .albums :
244
+ for x in data .albums :
245
+ if x .mbid == AlbumID :
246
+ album_url = str (x .covers [0 ])
242
247
243
- thumb_url = self ._get_thumb_url (data )
248
+ if album_url :
249
+ thumb_url = album_url .replace ('fanart/' , 'preview/' )
250
+ image_url = thumb_url
251
+
252
+ if not image_url :
253
+ logger .debug ('Fanart.tv no album image found' )
254
+ else :
255
+ logger .debug ('Fanart.tv album url: %s' , image_url )
244
256
245
257
if not thumb_url :
246
- logger .debug ('No album thumbnail image found on last.fm' )
258
+ logger .debug ('Fanart.tv no album thumbnail image found' )
259
+ else :
260
+ logger .debug ('Fanart.tv album thumb url: %s' , thumb_url )
247
261
248
262
return {'artwork' : image_url , 'thumbnail' : thumb_url }
249
263
@@ -284,10 +298,34 @@ def _update_cache(self):
284
298
285
299
myDB = db .DBConnection ()
286
300
287
- # Since lastfm uses release ids rather than release group ids for albums, we have to do a artist + album search for albums
288
- # Exception is when adding albums manually, then we should use release id
289
301
if self .id_type == 'artist' :
290
302
303
+ data = Artist .get (id = self .id )
304
+
305
+ logger .debug ('Fanart.tv ArtistID is: %s' , self .id )
306
+
307
+ artist_url = None
308
+ thumb_url = None
309
+ image_url = None
310
+
311
+ if data .thumbs :
312
+ for thumbs in data .thumbs [0 :1 ]:
313
+ artist_url = str (thumbs .url )
314
+
315
+ if artist_url :
316
+ thumb_url = artist_url .replace ('fanart/' , 'preview/' )
317
+ image_url = thumb_url
318
+
319
+ if not image_url :
320
+ logger .debug ('Fanart.tv no artist image found' )
321
+ else :
322
+ logger .debug ('Fanart.tv artist url: %s' , image_url )
323
+
324
+ if not thumb_url :
325
+ logger .debug ('Fanart.tv no artist thumbnail image found' )
326
+ else :
327
+ logger .debug ('Fanart.tv artist thumb url: %s' , thumb_url )
328
+
291
329
data = lastfm .request_lastfm ("artist.getinfo" , mbid = self .id , api_key = LASTFM_API_KEY )
292
330
293
331
# Try with name if not found
@@ -299,7 +337,7 @@ def _update_cache(self):
299
337
api_key = LASTFM_API_KEY )
300
338
301
339
if not data :
302
- return
340
+ logger . debug ( 'Last.fm connection cannot be made' )
303
341
304
342
try :
305
343
self .info_summary = data ['artist' ]['bio' ]['summary' ]
@@ -311,80 +349,44 @@ def _update_cache(self):
311
349
except KeyError :
312
350
logger .debug ('No artist bio found' )
313
351
self .info_content = None
314
- try :
315
- image_url = data ['artist' ]['image' ][- 1 ]['#text' ]
316
- except KeyError :
317
- logger .debug ('No artist image found' )
318
- image_url = None
319
-
320
- thumb_url = self ._get_thumb_url (data )
321
- if not thumb_url :
322
- logger .debug ('No artist thumbnail image found' )
323
352
324
353
else :
325
- dbalbum = myDB .action (
326
- 'SELECT ArtistName, AlbumTitle, ReleaseID, Type FROM albums WHERE AlbumID=?' ,
327
- [self .id ]).fetchone ()
328
- if dbalbum ['ReleaseID' ] != self .id :
329
- data = lastfm .request_lastfm ("album.getinfo" , mbid = dbalbum ['ReleaseID' ],
330
- api_key = LASTFM_API_KEY )
331
- if not data :
332
- data = lastfm .request_lastfm ("album.getinfo" ,
333
- artist = helpers .clean_musicbrainz_name (dbalbum ['ArtistName' ]),
334
- album = helpers .clean_musicbrainz_name (dbalbum ['AlbumTitle' ]),
335
- api_key = LASTFM_API_KEY )
336
- else :
337
- if dbalbum ['Type' ] != "part of" :
338
- data = lastfm .request_lastfm ("album.getinfo" ,
339
- artist = helpers .clean_musicbrainz_name (dbalbum ['ArtistName' ]),
340
- album = helpers .clean_musicbrainz_name (dbalbum ['AlbumTitle' ]),
341
- api_key = LASTFM_API_KEY )
342
- else :
343
354
344
- # Series, use actual artist for the release-group
345
- artist = mb .getArtistForReleaseGroup (self .id )
346
- if artist :
347
- data = lastfm .request_lastfm ("album.getinfo" ,
348
- artist = helpers .clean_musicbrainz_name (artist ),
349
- album = helpers .clean_musicbrainz_name (dbalbum ['AlbumTitle' ]),
350
- api_key = LASTFM_API_KEY )
355
+ # get ArtistID from AlbumID lookup - ArtistID not passed into this function otherwise
356
+ myDB = db .DBConnection ()
357
+ ArtistID = myDB .action ('SELECT ArtistID FROM albums WHERE ReleaseID=?' , [self .id ]).fetchone ()[0 ]
351
358
352
- if not data :
353
- return
359
+ logger . debug ( 'Fanart.tv AlbumID: %s' , self . id )
360
+ logger . debug ( 'Fanart.tv ArtistID: %s' , ArtistID )
354
361
355
- try :
356
- self .info_summary = data ['album' ]['wiki' ]['summary' ]
357
- except KeyError :
358
- logger .debug ('No album summary found' )
359
- self .info_summary = None
360
- try :
361
- self .info_content = data ['album' ]['wiki' ]['content' ]
362
- except KeyError :
363
- logger .debug ('No album infomation found' )
364
- self .info_content = None
365
- try :
366
- image_url = data ['album' ]['image' ][- 1 ]['#text' ]
367
- except KeyError :
368
- logger .debug ('No album image link found' )
369
- image_url = None
362
+ data = Artist .get (id = ArtistID )
370
363
371
- thumb_url = self ._get_thumb_url (data )
364
+ album_url = None
365
+ thumb_url = None
366
+ image_url = None
372
367
373
- if not thumb_url :
374
- logger .debug ('No album thumbnail image found' )
368
+ if not data :
369
+ logger .debug ('Fanart.tv artist lookup failed' )
370
+ return
375
371
376
- # Save the content & summary to the database no matter what if we've
377
- # opened up the url
378
- if self .id_type == 'artist' :
379
- controlValueDict = {"ArtistID" : self .id }
380
- else :
381
- controlValueDict = {"ReleaseGroupID" : self .id }
372
+ if data .albums :
373
+ for x in data .albums :
374
+ if x .mbid == self .id :
375
+ album_url = str (x .covers [0 ])
382
376
383
- newValueDict = { "Summary" : self . info_summary ,
384
- "Content" : self . info_content ,
385
- "LastUpdated" : helpers . today ()}
377
+ if album_url :
378
+ thumb_url = album_url . replace ( 'fanart/' , 'preview/' )
379
+ image_url = thumb_url
386
380
387
- myDB .upsert ("descriptions" , newValueDict , controlValueDict )
381
+ if not image_url :
382
+ logger .debug ('Fanart.tv no album image found' )
383
+ else :
384
+ logger .debug ('Fanart.tv album url: %s' , image_url )
385
+
386
+ if not thumb_url :
387
+ logger .debug ('Fanart.tv no album thumbnail image found' )
388
+ else :
389
+ logger .debug ('Fanart.tv album thumb url: %s' , thumb_url )
388
390
389
391
# Save the image URL to the database
390
392
if image_url :
@@ -403,8 +405,14 @@ def _update_cache(self):
403
405
404
406
# Should we grab the artwork here if we're just grabbing thumbs or
405
407
# info? Probably not since the files can be quite big
408
+
409
+ # With fanart.tv only one url is used for both thumb_url and image_url - so only making one request
410
+ # If seperate ones are desired in the future, the artwork vars below will need to be uncommented
411
+
412
+ artwork = request .request_content (image_url , timeout = 20 )
413
+
406
414
if image_url and self .query_type == 'artwork' :
407
- artwork = request .request_content (image_url , timeout = 20 )
415
+ # artwork = request.request_content(image_url, timeout=20)
408
416
409
417
if artwork :
410
418
# Make sure the artwork dir exists:
@@ -443,7 +451,7 @@ def _update_cache(self):
443
451
# as it's missing/outdated.
444
452
if thumb_url and self .query_type in ['thumb' , 'artwork' ] and not (
445
453
self .thumb_files and self ._is_current (self .thumb_files [0 ])):
446
- artwork = request .request_content (thumb_url , timeout = 20 )
454
+ # artwork = request.request_content(thumb_url, timeout=20)
447
455
448
456
if artwork :
449
457
# Make sure the artwork dir exists:
@@ -478,6 +486,59 @@ def _update_cache(self):
478
486
self .thumb_errors = True
479
487
self .thumb_url = image_url
480
488
489
+ dbalbum = myDB .action (
490
+ 'SELECT ArtistName, AlbumTitle, ReleaseID, Type FROM albums WHERE AlbumID=?' ,
491
+ [self .id ]).fetchone ()
492
+ if dbalbum ['ReleaseID' ] != self .id :
493
+ data = lastfm .request_lastfm ("album.getinfo" , mbid = dbalbum ['ReleaseID' ],
494
+ api_key = LASTFM_API_KEY )
495
+ if not data :
496
+ data = lastfm .request_lastfm ("album.getinfo" ,
497
+ artist = helpers .clean_musicbrainz_name (dbalbum ['ArtistName' ]),
498
+ album = helpers .clean_musicbrainz_name (dbalbum ['AlbumTitle' ]),
499
+ api_key = LASTFM_API_KEY )
500
+ else :
501
+ if dbalbum ['Type' ] != "part of" :
502
+ data = lastfm .request_lastfm ("album.getinfo" ,
503
+ artist = helpers .clean_musicbrainz_name (dbalbum ['ArtistName' ]),
504
+ album = helpers .clean_musicbrainz_name (dbalbum ['AlbumTitle' ]),
505
+ api_key = LASTFM_API_KEY )
506
+ else :
507
+
508
+ # Series, use actual artist for the release-group
509
+ artist = mb .getArtistForReleaseGroup (self .id )
510
+ if artist :
511
+ data = lastfm .request_lastfm ("album.getinfo" ,
512
+ artist = helpers .clean_musicbrainz_name (artist ),
513
+ album = helpers .clean_musicbrainz_name (dbalbum ['AlbumTitle' ]),
514
+ api_key = LASTFM_API_KEY )
515
+
516
+ if not data :
517
+ logger .debug ('Last.fm connection cannot be made' )
518
+
519
+ try :
520
+ self .info_summary = data ['album' ]['wiki' ]['summary' ]
521
+ except KeyError :
522
+ logger .debug ('No album summary found' )
523
+ self .info_summary = None
524
+ try :
525
+ self .info_content = data ['album' ]['wiki' ]['content' ]
526
+ except KeyError :
527
+ logger .debug ('No album infomation found' )
528
+ self .info_content = None
529
+
530
+ # Save the content & summary to the database no matter what if we've
531
+ # opened up the url
532
+ if self .id_type == 'artist' :
533
+ controlValueDict = {"ArtistID" : self .id }
534
+ else :
535
+ controlValueDict = {"ReleaseGroupID" : self .id }
536
+
537
+ newValueDict = {"Summary" : self .info_summary ,
538
+ "Content" : self .info_content ,
539
+ "LastUpdated" : helpers .today ()}
540
+
541
+ myDB .upsert ("descriptions" , newValueDict , controlValueDict )
481
542
482
543
def getArtwork (ArtistID = None , AlbumID = None ):
483
544
c = Cache ()
@@ -486,7 +547,7 @@ def getArtwork(ArtistID=None, AlbumID=None):
486
547
if not artwork_path :
487
548
return None
488
549
489
- if artwork_path .startswith ('http://' ):
550
+ if artwork_path .startswith ('http://' ) or artwork_path . startswith ( 'https://' ) :
490
551
return artwork_path
491
552
else :
492
553
artwork_file = os .path .basename (artwork_path )
@@ -500,7 +561,7 @@ def getThumb(ArtistID=None, AlbumID=None):
500
561
if not artwork_path :
501
562
return None
502
563
503
- if artwork_path .startswith ('http://' ):
564
+ if artwork_path .startswith ('http://' ) or artwork_path . startswith ( 'https://' ) :
504
565
return artwork_path
505
566
else :
506
567
thumbnail_file = os .path .basename (artwork_path )
0 commit comments