<s> from totalimpact . providers import provider <EOL> from totalimpact . providers . provider import Provider , ProviderContentMalformedError <EOL> import re <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class Figshare ( Provider ) : <EOL> example_id = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> url = "<STR_LIT>" <EOL> descr = "<STR_LIT>" <EOL> biblio_url_template = "<STR_LIT>" <EOL> aliases_url_template = "<STR_LIT>" <EOL> metrics_url_template = "<STR_LIT>" <EOL> provenance_url_template = "<STR_LIT>" <EOL> member_items_url_template = "<STR_LIT>" <EOL> static_meta_dict = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> } <EOL> def __init__ ( self ) : <EOL> super ( Figshare , self ) . __init__ ( ) <EOL> def is_relevant_alias ( self , alias ) : <EOL> ( namespace , nid ) = alias <EOL> is_figshare_doi = ( namespace == "<STR_LIT>" ) and ( "<STR_LIT>" in nid . lower ( ) ) <EOL> return is_figshare_doi <EOL> @ property <EOL> def provides_members ( self ) : <EOL> return True <EOL> def get_figshare_userid_from_author_url ( self , url ) : <EOL> match = re . findall ( "<STR_LIT>" , url ) <EOL> try : <EOL> userid = match [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> userid = None <EOL> return userid <EOL> def _extract_aliases ( self , page , id = None ) : <EOL> dict_of_keylists = { "<STR_LIT:url>" : [ "<STR_LIT>" ] } <EOL> item = self . _extract_figshare_record ( page , id ) <EOL> aliases_dict = provider . _extract_from_data_dict ( item , dict_of_keylists ) <EOL> if aliases_dict : <EOL> aliases_list = [ ( namespace , nid ) for ( namespace , nid ) in aliases_dict . iteritems ( ) ] <EOL> else : <EOL> aliases_list = [ ] <EOL> return aliases_list <EOL> def _extract_biblio ( self , page , id = None ) : <EOL> dict_of_keylists = { <EOL> '<STR_LIT:title>' : [ '<STR_LIT:title>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> item = self . _extract_figshare_record ( page , id ) <EOL> biblio_dict = provider . _extract_from_data_dict ( item , dict_of_keylists ) <EOL> biblio_dict [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> try : <EOL> biblio_dict [ "<STR_LIT>" ] = int ( biblio_dict [ "<STR_LIT>" ] [ - <NUM_LIT:4> : ] ) <EOL> except ( KeyError , TypeError ) : <EOL> pass <EOL> if "<STR_LIT>" in biblio_dict : <EOL> genre = biblio_dict [ "<STR_LIT>" ] . lower ( ) <EOL> if genre in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> genre = biblio_dict [ "<STR_LIT>" ] <EOL> elif genre == "<STR_LIT>" : <EOL> genre = "<STR_LIT>" <EOL> elif genre == "<STR_LIT>" : <EOL> genre = "<STR_LIT>" <EOL> elif genre == "<STR_LIT>" : <EOL> genre = "<STR_LIT>" <EOL> else : <EOL> genre = "<STR_LIT>" <EOL> biblio_dict [ "<STR_LIT>" ] = genre <EOL> if biblio_dict [ "<STR_LIT>" ] == "<STR_LIT>" : <EOL> biblio_dict [ "<STR_LIT>" ] = self . _get_templated_url ( self . provenance_url_template , id , "<STR_LIT>" ) <EOL> return biblio_dict <EOL> def _extract_figshare_record ( self , page , id ) : <EOL> data = provider . _load_json ( page ) <EOL> if not data : <EOL> return { } <EOL> item = data [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if str ( item [ "<STR_LIT>" ] ) in id : <EOL> return item <EOL> else : <EOL> return { } <EOL> def _extract_metrics ( self , page , status_code = <NUM_LIT:200> , id = None ) : <EOL> if status_code != <NUM_LIT:200> : <EOL> if status_code == <NUM_LIT> : <EOL> return { } <EOL> else : <EOL> raise ( self . _get_error ( status_code ) ) <EOL> dict_of_keylists = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> item = self . _extract_figshare_record ( page , id ) <EOL> metrics_dict = provider . _extract_from_data_dict ( item , dict_of_keylists ) <EOL> return metrics_dict <EOL> def _extract_members ( self , page , query_string = None ) : <EOL> data = provider . _load_json ( page ) <EOL> dois = [ item [ "<STR_LIT>" ] . replace ( "<STR_LIT>" , "<STR_LIT>" ) for item in data [ "<STR_LIT>" ] ] <EOL> doi_aliases = [ ( "<STR_LIT>" , doi ) for doi in dois ] <EOL> return ( doi_aliases ) <EOL> def member_items ( self , <EOL> account_name , <EOL> provider_url_template = None , <EOL> cache_enabled = True ) : <EOL> if not self . provides_members : <EOL> raise NotImplementedError ( ) <EOL> self . logger . debug ( u"<STR_LIT>" % ( self . provider_name , account_name ) ) <EOL> if not provider_url_template : <EOL> provider_url_template = self . member_items_url_template <EOL> members = [ ] <EOL> figshare_userid = self . get_figshare_userid_from_author_url ( account_name ) <EOL> if not figshare_userid : <EOL> raise ProviderContentMalformedError ( "<STR_LIT>" ) <EOL> next_page = <NUM_LIT:1> <EOL> while next_page : <EOL> url = provider_url_template % ( figshare_userid , next_page ) <EOL> response = self . http_get ( url , cache_enabled = cache_enabled ) <EOL> if response . status_code != <NUM_LIT:200> : <EOL> self . logger . info ( u"<STR_LIT>" <EOL> % ( self . provider_name , response . status_code ) ) <EOL> if response . status_code == <NUM_LIT> : <EOL> raise ProviderItemNotFoundError <EOL> elif response . status_code == <NUM_LIT> : <EOL> pass <EOL> else : <EOL> self . _get_error ( response . status_code , response ) <EOL> number_of_items_per_page = <NUM_LIT:10> <EOL> try : <EOL> page = response . text <EOL> data = provider . _load_json ( page ) <EOL> if data [ "<STR_LIT>" ] > next_page * number_of_items_per_page : <EOL> next_page += <NUM_LIT:1> <EOL> else : <EOL> next_page = None <EOL> members += self . _extract_members ( page , account_name ) <EOL> except ( AttributeError , TypeError ) : <EOL> next_page = None <EOL> return ( members ) </s>
