<s> from __future__ import unicode_literals <EOL> import re <EOL> try : <EOL> from urllib . parse import urlparse <EOL> except ImportError : <EOL> from urlparse import urlparse <EOL> import spotify <EOL> from spotify import ffi , lib , serialized , utils <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> class Link ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session , uri = None , sp_link = None , add_ref = True ) : <EOL> assert uri or sp_link , '<STR_LIT>' <EOL> self . _session = session <EOL> if uri is not None : <EOL> sp_link = lib . sp_link_create_from_string ( <EOL> utils . to_char ( Link . _normalize_uri ( uri ) ) ) <EOL> add_ref = False <EOL> if sp_link == ffi . NULL : <EOL> raise ValueError ( <EOL> '<STR_LIT>' % uri ) <EOL> if add_ref : <EOL> lib . sp_link_add_ref ( sp_link ) <EOL> self . _sp_link = ffi . gc ( sp_link , lib . sp_link_release ) <EOL> @ staticmethod <EOL> def _normalize_uri ( uri ) : <EOL> if uri . startswith ( '<STR_LIT>' ) : <EOL> return uri <EOL> parsed = urlparse ( uri ) <EOL> if parsed . netloc not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return uri <EOL> return '<STR_LIT>' % parsed . path . strip ( ) . replace ( '<STR_LIT:/>' , '<STR_LIT::>' ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % self . uri <EOL> def __str__ ( self ) : <EOL> return self . uri <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _sp_link == other . _sp_link <EOL> else : <EOL> return False <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _sp_link ) <EOL> @ property <EOL> def uri ( self ) : <EOL> """<STR_LIT>""" <EOL> return utils . get_with_growing_buffer ( <EOL> lib . sp_link_as_string , self . _sp_link ) <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( <EOL> self . uri [ len ( '<STR_LIT>' ) : ] . replace ( '<STR_LIT::>' , '<STR_LIT:/>' ) ) <EOL> @ property <EOL> def type ( self ) : <EOL> """<STR_LIT>""" <EOL> return LinkType ( lib . sp_link_type ( self . _sp_link ) ) <EOL> @ serialized <EOL> def as_track ( self ) : <EOL> """<STR_LIT>""" <EOL> sp_track = lib . sp_link_as_track ( self . _sp_link ) <EOL> if sp_track == ffi . NULL : <EOL> return None <EOL> return spotify . Track ( self . _session , sp_track = sp_track , add_ref = True ) <EOL> def as_track_offset ( self ) : <EOL> """<STR_LIT>""" <EOL> offset = ffi . new ( '<STR_LIT>' ) <EOL> sp_track = lib . sp_link_as_track_and_offset ( self . _sp_link , offset ) <EOL> if sp_track == ffi . NULL : <EOL> return None <EOL> return offset [ <NUM_LIT:0> ] <EOL> @ serialized <EOL> def as_album ( self ) : <EOL> """<STR_LIT>""" <EOL> sp_album = lib . sp_link_as_album ( self . _sp_link ) <EOL> if sp_album == ffi . NULL : <EOL> return None <EOL> return spotify . Album ( self . _session , sp_album = sp_album , add_ref = True ) <EOL> @ serialized <EOL> def as_artist ( self ) : <EOL> """<STR_LIT>""" <EOL> sp_artist = lib . sp_link_as_artist ( self . _sp_link ) <EOL> if sp_artist == ffi . NULL : <EOL> return None <EOL> return spotify . Artist ( self . _session , sp_artist = sp_artist , add_ref = True ) <EOL> def as_playlist ( self ) : <EOL> """<STR_LIT>""" <EOL> sp_playlist = self . _as_sp_playlist ( ) <EOL> if sp_playlist is None : <EOL> return None <EOL> return spotify . Playlist . _cached ( <EOL> self . _session , sp_playlist , add_ref = False ) <EOL> def _as_sp_playlist ( self ) : <EOL> sp_playlist = None <EOL> if self . type == LinkType . PLAYLIST : <EOL> sp_playlist = lib . sp_playlist_create ( <EOL> self . _session . _sp_session , self . _sp_link ) <EOL> elif self . type == LinkType . STARRED : <EOL> matches = re . match ( r'<STR_LIT>' , self . uri ) <EOL> if matches : <EOL> username = matches . group ( <NUM_LIT:1> ) <EOL> sp_playlist = lib . sp_session_starred_for_user_create ( <EOL> self . _session . _sp_session , utils . to_bytes ( username ) ) <EOL> if sp_playlist is None or sp_playlist == ffi . NULL : <EOL> return None <EOL> return sp_playlist <EOL> @ serialized <EOL> def as_user ( self ) : <EOL> """<STR_LIT>""" <EOL> sp_user = lib . sp_link_as_user ( self . _sp_link ) <EOL> if sp_user == ffi . NULL : <EOL> return None <EOL> return spotify . User ( self . _session , sp_user = sp_user , add_ref = True ) <EOL> def as_image ( self , callback = None ) : <EOL> """<STR_LIT>""" <EOL> if self . type is not LinkType . IMAGE : <EOL> return None <EOL> sp_image = lib . sp_image_create_from_link ( <EOL> self . _session . _sp_session , self . _sp_link ) <EOL> if sp_image == ffi . NULL : <EOL> return None <EOL> return spotify . Image ( <EOL> self . _session , sp_image = sp_image , add_ref = False , callback = callback ) <EOL> @ utils . make_enum ( '<STR_LIT>' ) <EOL> class LinkType ( utils . IntEnum ) : <EOL> pass </s>
