<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> import requests <EOL> import operator <EOL> from beets import plugins , ui <EOL> ACOUSTIC_BASE = "<STR_LIT>" <EOL> LEVELS = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> class AcousticPlugin ( plugins . BeetsPlugin ) : <EOL> def __init__ ( self ) : <EOL> super ( AcousticPlugin , self ) . __init__ ( ) <EOL> self . config . add ( { '<STR_LIT>' : True } ) <EOL> if self . config [ '<STR_LIT>' ] : <EOL> self . register_listener ( '<STR_LIT>' , <EOL> self . import_task_files ) <EOL> def commands ( self ) : <EOL> cmd = ui . Subcommand ( '<STR_LIT>' , <EOL> help = u"<STR_LIT>" ) <EOL> def func ( lib , opts , args ) : <EOL> items = lib . items ( ui . decargs ( args ) ) <EOL> fetch_info ( self . _log , items , ui . should_write ( ) ) <EOL> cmd . func = func <EOL> return [ cmd ] <EOL> def import_task_files ( self , session , task ) : <EOL> """<STR_LIT>""" <EOL> items = task . imported_items ( ) <EOL> fetch_info ( self . _log , items , False ) <EOL> def fetch_info ( log , items , write ) : <EOL> """<STR_LIT>""" <EOL> def get_value ( * map_path ) : <EOL> try : <EOL> return reduce ( operator . getitem , map_path , data ) <EOL> except KeyError : <EOL> log . debug ( u'<STR_LIT>' , map_path ) <EOL> for item in items : <EOL> if item . mb_trackid : <EOL> log . info ( u'<STR_LIT>' , item ) <EOL> urls = [ generate_url ( item . mb_trackid , path ) for path in LEVELS ] <EOL> log . debug ( u'<STR_LIT>' , '<STR_LIT:U+0020>' . join ( urls ) ) <EOL> try : <EOL> res = [ requests . get ( url ) for url in urls ] <EOL> except requests . RequestException as exc : <EOL> log . info ( u'<STR_LIT>' , exc ) <EOL> continue <EOL> if any ( r . status_code == <NUM_LIT> for r in res ) : <EOL> log . info ( u'<STR_LIT>' , item . mb_trackid ) <EOL> continue <EOL> try : <EOL> data = res [ <NUM_LIT:0> ] . json ( ) <EOL> data . update ( res [ <NUM_LIT:1> ] . json ( ) ) <EOL> except ValueError : <EOL> log . debug ( u'<STR_LIT>' , [ r . text for r in res ] ) <EOL> item . danceable = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" , <EOL> ) <EOL> item . gender = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:value>" , <EOL> ) <EOL> item . genre_rosamerica = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:value>" <EOL> ) <EOL> item . mood_acoustic = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . mood_aggressive = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . mood_electronic = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . mood_happy = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . mood_party = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . mood_relaxed = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . mood_sad = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . rhythm = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:value>" <EOL> ) <EOL> item . tonal = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" <EOL> ) <EOL> item . voice_instrumental = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:value>" <EOL> ) <EOL> item . average_loudness = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> item . chords_changes_rate = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> item . chords_key = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> item . chords_number_rate = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> item . chords_scale = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> item . initial_key = '<STR_LIT>' . format ( <EOL> get_value ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> get_value ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> ) <EOL> item . key_strength = get_value ( <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ) <EOL> item . store ( ) <EOL> if write : <EOL> item . try_write ( ) <EOL> def generate_url ( mbid , level ) : <EOL> """<STR_LIT>""" <EOL> return ACOUSTIC_BASE + mbid + level </s>
