<s> import os , re <EOL> from totalimpact . providers import provider <EOL> from totalimpact . providers . provider import Provider , ProviderContentMalformedError , ProviderRateLimitError <EOL> from totalimpact . providers . twitter_paging import TwitterPager <EOL> from birdy . twitter import AppClient <EOL> from birdy . twitter import TwitterApiError , TwitterRateLimitError , TwitterClientError <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> class Twitter ( Provider ) : <EOL> example_id = ( "<STR_LIT:url>" , "<STR_LIT>" ) <EOL> url = "<STR_LIT>" <EOL> descr = "<STR_LIT>" <EOL> member_items_url_template = "<STR_LIT>" <EOL> provenance_url_templates = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <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 ( Twitter , self ) . __init__ ( ) <EOL> self . client = AppClient ( os . getenv ( "<STR_LIT>" ) , <EOL> os . getenv ( "<STR_LIT>" ) , <EOL> os . getenv ( "<STR_LIT>" ) ) <EOL> @ property <EOL> def provides_biblio ( self ) : <EOL> return True <EOL> @ property <EOL> def provides_metrics ( self ) : <EOL> return True <EOL> def is_relevant_alias ( self , alias ) : <EOL> ( namespace , nid ) = alias <EOL> try : <EOL> nid = nid . lower ( ) <EOL> except AttributeError : <EOL> return False <EOL> if ( namespace == "<STR_LIT:url>" ) : <EOL> if ( "<STR_LIT>" in nid ) and ( "<STR_LIT>" not in nid ) : <EOL> return True <EOL> return False <EOL> def screen_name ( self , nid ) : <EOL> match = re . findall ( "<STR_LIT>" , nid ) <EOL> return match [ <NUM_LIT:0> ] <EOL> def member_items ( self , <EOL> query_string , <EOL> provider_url_template = None , <EOL> cache_enabled = True ) : <EOL> twitter_username = query_string . replace ( "<STR_LIT:@>" , "<STR_LIT>" ) <EOL> url = self . _get_templated_url ( self . member_items_url_template , twitter_username , "<STR_LIT>" ) <EOL> members = [ ( "<STR_LIT:url>" , url ) ] <EOL> return ( members ) <EOL> def get_account_data ( self , aliases , number_tweets = <NUM_LIT:20> , max_pages = <NUM_LIT:1> ) : <EOL> nid = self . get_best_id ( aliases ) <EOL> if not nid : <EOL> return None <EOL> response_list = [ ] <EOL> def save_to_list ( r ) : <EOL> if r . data : <EOL> response_list . extend ( r . data ) <EOL> pager = TwitterPager ( os . getenv ( "<STR_LIT>" ) , <EOL> os . getenv ( "<STR_LIT>" ) , <EOL> os . getenv ( "<STR_LIT>" ) , <EOL> default_max_pages = max_pages ) <EOL> try : <EOL> r = pager . paginated_search ( <EOL> page_handler = save_to_list , <EOL> screen_name = self . screen_name ( nid ) , <EOL> count = number_tweets , <EOL> contributor_details = True , <EOL> include_rts = True , <EOL> exclude_replies = False , <EOL> trim_user = False <EOL> ) <EOL> except TwitterApiError : <EOL> logger . error ( "<STR_LIT>" ) <EOL> return None <EOL> except TwitterClientError : <EOL> logger . error ( "<STR_LIT>" ) <EOL> return None <EOL> return response_list <EOL> def biblio ( self , <EOL> aliases , <EOL> provider_url_template = None , <EOL> cache_enabled = True ) : <EOL> biblio_dict = { } <EOL> biblio_dict [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> data_list = self . get_account_data ( aliases , number_tweets = <NUM_LIT:1> ) <EOL> if not data_list : <EOL> return biblio_dict <EOL> data = data_list [ <NUM_LIT:0> ] [ "<STR_LIT:user>" ] <EOL> twitter_username = data [ "<STR_LIT>" ] <EOL> twitter_username_with_at = u"<STR_LIT>" . format ( <EOL> twitter_username = twitter_username ) <EOL> biblio_dict [ "<STR_LIT:title>" ] = twitter_username_with_at <EOL> biblio_dict [ "<STR_LIT>" ] = data [ "<STR_LIT:name>" ] <EOL> biblio_dict [ "<STR_LIT:description>" ] = data [ "<STR_LIT:description>" ] <EOL> biblio_dict [ "<STR_LIT>" ] = data [ "<STR_LIT>" ] <EOL> biblio_dict [ "<STR_LIT:url>" ] = u"<STR_LIT>" . format ( <EOL> twitter_username = twitter_username ) <EOL> biblio_dict [ "<STR_LIT>" ] = True <EOL> biblio_dict [ "<STR_LIT>" ] = twitter_username_with_at <EOL> biblio_dict [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> return biblio_dict <EOL> def metrics ( self , <EOL> aliases , <EOL> provider_url_template = None , <EOL> cache_enabled = True ) : <EOL> data_list = self . get_account_data ( aliases , number_tweets = <NUM_LIT:1> ) <EOL> if not data_list : <EOL> return { } <EOL> data = data_list [ <NUM_LIT:0> ] [ "<STR_LIT:user>" ] <EOL> dict_of_keylists = { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> metrics_dict = { } <EOL> for field in dict_of_keylists : <EOL> metric_value = data [ dict_of_keylists [ field ] [ <NUM_LIT:0> ] ] <EOL> if metric_value : <EOL> metrics_dict [ field ] = metric_value <EOL> metrics_and_drilldown = { } <EOL> for metric_name in metrics_dict : <EOL> drilldown_url = self . provenance_url ( metric_name , aliases ) <EOL> metrics_and_drilldown [ metric_name ] = ( metrics_dict [ metric_name ] , drilldown_url ) <EOL> return metrics_and_drilldown <EOL> def provenance_url ( self , metric_name , aliases ) : <EOL> nid = self . get_best_id ( aliases ) <EOL> if not nid : <EOL> return None <EOL> screen_name = self . screen_name ( nid ) <EOL> provenance_url = self . _get_templated_url ( self . provenance_url_templates [ metric_name ] , screen_name , "<STR_LIT>" ) <EOL> return provenance_url </s>
