<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> import requests <EOL> from urlparse import urljoin <EOL> from urllib import urlencode <EOL> import xml . etree . ElementTree as ET <EOL> from beets import config <EOL> from beets . plugins import BeetsPlugin <EOL> def get_music_section ( host , port , token , library_name ) : <EOL> """<STR_LIT>""" <EOL> api_endpoint = append_token ( '<STR_LIT>' , token ) <EOL> url = urljoin ( '<STR_LIT>' . format ( host , port ) , api_endpoint ) <EOL> r = requests . get ( url ) <EOL> tree = ET . fromstring ( r . content ) <EOL> for child in tree . findall ( '<STR_LIT>' ) : <EOL> if child . get ( '<STR_LIT:title>' ) == library_name : <EOL> return child . get ( '<STR_LIT:key>' ) <EOL> def update_plex ( host , port , token , library_name ) : <EOL> """<STR_LIT>""" <EOL> section_key = get_music_section ( host , port , token , library_name ) <EOL> api_endpoint = '<STR_LIT>' . format ( section_key ) <EOL> api_endpoint = append_token ( api_endpoint , token ) <EOL> url = urljoin ( '<STR_LIT>' . format ( host , port ) , api_endpoint ) <EOL> r = requests . get ( url ) <EOL> return r <EOL> def append_token ( url , token ) : <EOL> """<STR_LIT>""" <EOL> if token : <EOL> url += '<STR_LIT:?>' + urlencode ( { '<STR_LIT>' : token } ) <EOL> return url <EOL> class PlexUpdate ( BeetsPlugin ) : <EOL> def __init__ ( self ) : <EOL> super ( PlexUpdate , self ) . __init__ ( ) <EOL> config [ '<STR_LIT>' ] . add ( { <EOL> u'<STR_LIT:host>' : u'<STR_LIT:localhost>' , <EOL> u'<STR_LIT:port>' : <NUM_LIT> , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' } ) <EOL> self . register_listener ( '<STR_LIT>' , self . listen_for_db_change ) <EOL> def listen_for_db_change ( self , lib , model ) : <EOL> """<STR_LIT>""" <EOL> self . register_listener ( '<STR_LIT>' , self . update ) <EOL> def update ( self , lib ) : <EOL> """<STR_LIT>""" <EOL> self . _log . info ( u'<STR_LIT>' ) <EOL> try : <EOL> update_plex ( <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT:host>' ] . get ( ) , <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT:port>' ] . get ( ) , <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( ) , <EOL> config [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( ) ) <EOL> self . _log . info ( u'<STR_LIT>' ) <EOL> except requests . exceptions . RequestException : <EOL> self . _log . warning ( u'<STR_LIT>' ) </s>
