<s> import hashlib , simplejson , os , collections <EOL> from totalimpact import db <EOL> from totalimpact . providers import provider <EOL> from totalimpact . providers . provider import Provider , ProviderContentMalformedError <EOL> from totalimpact . provider_batch_data import ProviderBatchData <EOL> import logging <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> batch_data = None <EOL> class Pmc ( Provider ) : <EOL> example_id = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> provenance_url_template = None <EOL> url = "<STR_LIT>" <EOL> descr = "<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> "<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 ( Pmc , self ) . __init__ ( ) <EOL> def is_relevant_alias ( self , alias ) : <EOL> ( namespace , nid ) = alias <EOL> return ( "<STR_LIT>" == namespace ) <EOL> def build_batch_data_dict ( self ) : <EOL> logger . info ( u"<STR_LIT>" ) <EOL> batch_data = collections . defaultdict ( list ) <EOL> matches = ProviderBatchData . query . filter_by ( provider = "<STR_LIT>" ) . all ( ) <EOL> for provider_batch_data_obj in matches : <EOL> for nid in provider_batch_data_obj . aliases [ "<STR_LIT>" ] : <EOL> pmid_alias = ( "<STR_LIT>" , nid ) <EOL> batch_data [ pmid_alias ] += [ { "<STR_LIT>" : provider_batch_data_obj . raw , <EOL> "<STR_LIT>" : provider_batch_data_obj . max_event_date } ] <EOL> logger . info ( u"<STR_LIT>" . format ( n = len ( batch_data ) ) ) <EOL> return batch_data <EOL> def has_applicable_batch_data ( self , namespace , nid ) : <EOL> has_applicable_batch_data = False <EOL> matches = ProviderBatchData . query . filter_by ( provider = "<STR_LIT>" ) . all ( ) <EOL> for provider_batch_data_obj in matches : <EOL> if nid in provider_batch_data_obj . aliases [ namespace ] : <EOL> has_applicable_batch_data = True <EOL> return has_applicable_batch_data <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> if "<STR_LIT>" not in page : <EOL> raise ProviderContentMalformedError <EOL> ( doc , lookup_function ) = provider . _get_doc_from_xml ( page ) <EOL> if not doc : <EOL> return { } <EOL> try : <EOL> articles = doc . getElementsByTagName ( "<STR_LIT>" ) <EOL> for article in articles : <EOL> print article <EOL> metrics_dict = { } <EOL> meta_data = article . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> pmid = meta_data . getAttribute ( "<STR_LIT>" ) <EOL> if id == pmid : <EOL> metrics = article . getElementsByTagName ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> pdf_downloads = int ( metrics . getAttribute ( "<STR_LIT>" ) ) <EOL> if pdf_downloads : <EOL> metrics_dict . update ( { '<STR_LIT>' : pdf_downloads } ) <EOL> abstract_views = int ( metrics . getAttribute ( "<STR_LIT>" ) ) <EOL> if abstract_views : <EOL> metrics_dict . update ( { '<STR_LIT>' : abstract_views } ) <EOL> fulltext_views = int ( metrics . getAttribute ( "<STR_LIT>" ) ) <EOL> if fulltext_views : <EOL> metrics_dict . update ( { '<STR_LIT>' : fulltext_views } ) <EOL> unique_ip_views = int ( metrics . getAttribute ( "<STR_LIT>" ) ) <EOL> if unique_ip_views : <EOL> metrics_dict . update ( { '<STR_LIT>' : unique_ip_views } ) <EOL> figure_views = int ( metrics . getAttribute ( "<STR_LIT>" ) ) <EOL> if figure_views : <EOL> metrics_dict . update ( { '<STR_LIT>' : figure_views } ) <EOL> suppdata_views = int ( metrics . getAttribute ( "<STR_LIT>" ) ) <EOL> if suppdata_views : <EOL> metrics_dict . update ( { '<STR_LIT>' : suppdata_views } ) <EOL> return metrics_dict <EOL> except ( KeyError , IndexError , TypeError ) : <EOL> pass <EOL> return { } <EOL> def _get_metrics_and_drilldown ( self , pages , pmid ) : <EOL> metrics_dict = { } <EOL> for page in pages : <EOL> one_month_metrics_dict = self . _extract_metrics ( page , id = pmid ) <EOL> print one_month_metrics_dict <EOL> for metric in one_month_metrics_dict : <EOL> try : <EOL> metrics_dict [ metric ] += one_month_metrics_dict [ metric ] <EOL> except KeyError : <EOL> metrics_dict [ metric ] = one_month_metrics_dict [ metric ] <EOL> metrics_and_drilldown = { } <EOL> for metric_name in metrics_dict : <EOL> drilldown_url = "<STR_LIT>" <EOL> metrics_and_drilldown [ metric_name ] = ( metrics_dict [ metric_name ] , drilldown_url ) <EOL> return metrics_and_drilldown <EOL> def metrics ( self , <EOL> aliases , <EOL> provider_url_template = None , <EOL> cache_enabled = True ) : <EOL> global batch_data <EOL> if not batch_data : <EOL> batch_data = self . build_batch_data_dict ( ) <EOL> pass <EOL> metrics_and_drilldown = { } <EOL> from totalimpact import item <EOL> aliases_dict = item . alias_dict_from_tuples ( aliases ) <EOL> try : <EOL> pmid = aliases_dict [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> except KeyError : <EOL> return { } <EOL> pmid_alias = ( "<STR_LIT>" , pmid ) <EOL> page = "<STR_LIT>" <EOL> if pmid_alias in batch_data : <EOL> pages = [ page [ "<STR_LIT>" ] for page in batch_data [ pmid_alias ] ] <EOL> if page : <EOL> metrics_and_drilldown = self . _get_metrics_and_drilldown ( pages , pmid ) <EOL> return metrics_and_drilldown </s>
