def queryAltmetric(pmid): """ Check the altmetric journal percentile score of the publication """ a = Altmetric() try: resp = a.pmid(pmid) if resp is None: logger.debug("PMID %s. Not found" % pmid) return -1 else: if 'context' in resp: metric = resp['context']['journal'][ 'pct'] # Percentage attention for this journal logger.debug("PMID %s. Metric %s" % (pmid, metric)) return metric logger.debug("PMID %s. Percentage attention not found" % pmid) return -2 except AltmetricHTTPException as e: if e.status_code == 403: logger.error( "You aren't authorized for this call: {}".format(pmid)) elif e.status_code == 420: logger.error( 'You are being rate limited, currently {}'.format(pmid)) elif e.status_code == 502: logger.error( 'The API version you are using is currently down for maintenance.' ) elif e.status_code == 404: logger.error('Invalid API function') logger.error(e.msg) logger.warn("PMID %s. Exception %s" % (pmid, e.msg)) return -3
def bst_openaire_altmetric(): """ """ recids = search_pattern(p="0->Z", f="0247_a") a = Altmetric() for recid in recids: try: # Check if we already have an Altmetric id sysno_inst = get_fieldvalues(recid, "035__9") if ['Altmetric'] in sysno_inst: continue doi_val = get_fieldvalues(recid, "0247_a")[0] json_res = a.doi(doi_val) rec = {} record_add_field(rec, "001", controlfield_value=str(recid)) if json_res: record_add_field(rec, '035', subfields=[('a', str(json_res['altmetric_id'])), ('9', 'Altmetric')]) bibupload(rec, opt_mode='correct') except AltmetricHTTPException, e: register_exception(prefix='Altmetric error (status code %s): %s' % (e.status_code, str(e)), alert_admin=False)
def openaire_altmetric_update(recids, upload=True): """ Retrieve Altmetric information for a record. """ logger.debug("Checking Altmetric for recids %s" % recids) a = Altmetric() records = [] for recid in recids: logger.debug("Checking Altmetric for recid %s" % recid) try: # Check if we already have an Altmetric id sysno_inst = get_fieldvalues(recid, "035__9") if ['Altmetric'] in sysno_inst: continue doi_val = get_fieldvalues(recid, "0247_a")[0] logger.debug("Found DOI %s" % doi_val) json_res = a.doi(doi_val) logger.debug("Altmetric response: %s" % json_res) rec = {} record_add_field(rec, "001", controlfield_value=str(recid)) if json_res: record_add_field(rec, '035', subfields=[('a', str(json_res['altmetric_id'])), ('9', 'Altmetric')]) records.append(rec) except AltmetricHTTPException as e: logger.warning( 'Altmetric error for recid %s with DOI %s (status code %s): %s' % (recid, doi_val, e.status_code, str(e))) register_exception(prefix='Altmetric error (status code %s): %s' % (e.status_code, str(e)), alert_admin=False) except IndexError: logger.debug("No DOI found") pass if upload and records: if len(records) == 1: bibupload(record=records[0], file_prefix="altmetric") else: bibupload(collection=records, file_prefix="altmetric") return records
def getAltScore(inputDF): """This function reads in an already existing dataframe, typically the one obtained from the searchPubMed function. It returns said dataframe with an additional column, giving the Altmetric score for a given article DOI in the table according to Altmetrics. Parameters: inputDF(Pandas.DataFrame): this is a Pandas dataframe with PubMed ID as indices, and a column labelled "DOI" with all the DOIs for the associated PubMed ID. Returns: Pandas.DataFrame, identical to input with addition of column "Altmetric Score," giving the Altmetric score, calculated by Altmetrics, for a given article. Example: queryDF = getAltScore(queryDF)""" #First gather the DOI for each article in the dataframe. doiList = inputDF["DOI"].tolist() listAltScore = [] count = 1 print("Getting Altmetrics Score") #Now for every DOI, use CrossRef to find the associated Altmetrics score. for entry in doiList: print(count) try: if entry != "": a = Altmetric() thisDOI = a.doi(entry) extractScore = thisDOI["score"] listAltScore.append(extractScore) else: listAltScore.append(None) except: listAltScore.append(None) count = count + 1 #Now add a column of Altmetrics scores to the dataframe. inputDF["Altmetric Score"] = listAltScore print("Altmetrics Search Complete!") return inputDF
def main(): try: args = get_args() twitter_api = setup_tweeting() preprints = load_database() shuffle(preprints) altmetric_api = Altmetric(apikey=secrets.altmetric_key) for preprint in preprints: if preprint.status == 'tweeted': continue return_code, res = preprint.query_altmetric(altmetric_api) if return_code: my_logger.error(res) else: if res >= 90: if not args.dry: preprint.tweet(twitter_api) else: preprint.dry_print() sleep(5) clean_database(preprints) my_logger.info('Finished.\n') except Exception as e: my_logger.error(e, exc_info=True) print("Terminating...") clean_database(preprints) raise
def openaire_altmetric_update(recids, upload=True): """ Retrieve Altmetric information for a record. """ logger.debug("Checking Altmetric for recids %s" % recids) a = Altmetric() records = [] for recid in recids: logger.debug("Checking Altmetric for recid %s" % recid) try: # Check if we already have an Altmetric id sysno_inst = get_fieldvalues(recid, "035__9") if ["Altmetric"] in sysno_inst: continue doi_val = get_fieldvalues(recid, "0247_a")[0] logger.debug("Found DOI %s" % doi_val) json_res = a.doi(doi_val) logger.debug("Altmetric response: %s" % json_res) rec = {} record_add_field(rec, "001", controlfield_value=str(recid)) if json_res: record_add_field(rec, "035", subfields=[("a", str(json_res["altmetric_id"])), ("9", "Altmetric")]) records.append(rec) except AltmetricHTTPException as e: logger.warning( "Altmetric error for recid %s with DOI %s (status code %s): %s" % (recid, doi_val, e.status_code, str(e)) ) register_exception( prefix="Altmetric error (status code %s): %s" % (e.status_code, str(e)), alert_admin=False ) except IndexError: logger.debug("No DOI found") pass if upload and records: if len(records) == 1: bibupload(record=records[0], file_prefix="altmetric") else: bibupload(collection=records, file_prefix="altmetric") return records
def openaire_altmetric_update(recids, upload=True): """ Retrieve Altmetric information for a record. """ logger.debug("Checking Altmetric for recids %s" % recids) a = Altmetric() records = [] for recid in recids: logger.debug("Checking Altmetric for recid %s" % recid) try: # Check if we already have an Altmetric id sysno_inst = get_fieldvalues(recid, "035__9") if ['Altmetric'] in sysno_inst: continue doi_val = get_fieldvalues(recid, "0247_a")[0] logger.debug("Found DOI %s" % doi_val) json_res = a.doi(doi_val) logger.debug("Altmetric response: %s" % json_res) rec = {} record_add_field(rec, "001", controlfield_value=str(recid)) if json_res: record_add_field(rec, '035', subfields=[ ('a', str(json_res['altmetric_id'])), ('9', 'Altmetric') ]) records.append(rec) except AltmetricHTTPException, e: logger.warning( 'Altmetric error for recid %s with DOI %s (status code %s): %s' % (recid, doi_val, e.status_code, str(e)) ) register_exception( prefix='Altmetric error (status code %s): %s' % ( e.status_code, str(e)), alert_admin=False ) except IndexError: logger.debug("No DOI found") pass
s = ',' # separator if os.path.isfile(outputFileName): outputFile_r = open(outputFileName, 'r') for line in outputFile_r: listpmid_alreadyDone.append(line.split(',')[0]) outputFile_r.close() outputFile = open(outputFileName, 'a') else: outputFile = open(outputFileName, 'w') outputFile.write('pmid, score, numReaders, cited (altmetric), scopus\n') am_metadata = Altmetric() c = 0 for line in setLinks: if c > numLines2Skip: parsedData = line.split('\n')[0].split(',') pmid = parsedData[2] if not (pmid in listpmid_alreadyDone): paperMetadata = am_metadata.pmid(pmid) if paperMetadata != None:
def update(self): rsp = None a = Altmetric(settings.ALTMETRIC_API_KEY) ids = self.paper.get_ids() # Fetch altmetric data based on paper supported id try: if 'doi' in ids: rsp = a.doi(ids.get('doi', '')) if not rsp and 'arx' in ids: rsp = a.arxiv(ids.get('arx')) if not rsp and 'pmi' in ids: rsp = a.pmid(ids.get('pmi')) except AltmetricHTTPException: raise # Parse json if rsp: self.score = rsp.get('score') self.altmetric_id = rsp.get('altmetric_id') self.altmetric_jid = rsp.get('altmetric_jid', '') self.score_1d = rsp.get('history').get('1d') self.score_2d = rsp.get('history').get('2d') self.score_3d = rsp.get('history').get('3d') self.score_4d = rsp.get('history').get('4d') self.score_5d = rsp.get('history').get('5d') self.score_6d = rsp.get('history').get('6d') self.score_1w = rsp.get('history').get('1w') self.score_1m = rsp.get('history').get('1m') self.score_3m = rsp.get('history').get('3m') self.score_6m = rsp.get('history').get('6m') self.score_1y = rsp.get('history').get('1y') self.cited_by_posts_count = rsp.get('cited_by_posts_count', 0) self.cited_by_delicious_count = rsp.get('cited_by_delicious_count', 0) self.cited_by_fbwalls_count = rsp.get('cited_by_fbwalss_count', 0) self.cited_by_feeds_count = rsp.get('cited_by_feeds_count', 0) self.cited_by_forum_count = rsp.get('cited_by_forum_count', 0) self.cited_by_gplus_count = rsp.get('cited_by_gplus_count', 0) self.cited_by_linkedin_count = rsp.get('cited_by_linkedin_count', 0) self.cited_by_msm_count = rsp.get('cited_by_msm_count', 0) self.cited_by_peer_review_sites_count = rsp.get( 'cited_by_peer_review_sites_count', 0) self.cited_by_pinners_count = rsp.get('cited_by_pinners_count', 0) self.cited_by_policies_count = rsp.get('cited_by_policies_count', 0) self.cited_by_qs_count = rsp.get('cited_by_qs_count', 0) self.cited_by_rdts_count = rsp.get('cited_by_rdts_count', 0) self.cited_by_rh_count = rsp.get('cited_by_rh_count', 0) self.cited_by_tweeters_count = rsp.get('cited_by_tweeters_count', 0) self.cited_by_videos_count = rsp.get('cited_by_videos_count', 0) self.cited_by_weibo_count = rsp.get('cited_by_weibo_count', 0) self.cited_by_wikipedia_count = rsp.get('cited_by_wikipedia_count', 0) if isinstance(rsp.get('readers'), dict): self.readers_citeulike = rsp.get('readers').get('citeulike') self.readers_mendeley = rsp.get('readers').get('mendeley') self.image = rsp.get('images').get('small') match = re.findall(r'types=(P?[\w]+)', self.image) if match: self.type = match[0] # save self.save()
metrics_columns = [ "pmid", "doi", "pub_year", "altmetric_id", "altmetric_queried", "paperbuzz_queried" ] altmetric_fields = config['altmetric_fields'] paperbuzz_fields = config['paperbuzz_fields'] am_metrics = sorted(altmetric_fields.keys()) paperbuzz_metrics = sorted(paperbuzz_fields.keys()) metrics_columns = metrics_columns + am_metrics + paperbuzz_metrics # Initialize APIs ALTMETRIC_KEY = config['keys']['altmetric'] altmetric = Altmetric(ALTMETRIC_KEY) paperbuzz = PaperbuzzAPI(config['contact']['email']) logging.info('Iterating over input files') for name, input_file in config['input_files'].items(): logging.info('Processing {}: {}'.format(name, input_file)) # Load input file input_df = pd.read_csv(data_dir / input_file) output_dir = base_dir / name if not os.path.exists(str(output_dir)): os.makedirs(str(output_dir)) # # Query Paperbuzz API # logging.info('Querying Paperbuzz API')
# Output columns for results METRICS_COLUMNS = ["pmid", "doi", "resp", "err", "ts"] # Init logging logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(message)s") # Init logging.info('Initialise configuration') data_dir = Path("../data/") config = load_config("../config.yml") output_dir = select_basedir(data_dir) # Initialize APIs ALTMETRIC_KEY = config['keys']['altmetric'] altmetric = Altmetric(ALTMETRIC_KEY) paperbuzz = PaperbuzzAPI(config['contact']['email']) logging.info('Iterating over each query') for idx, query in enumerate(config['queries'].keys()): input_df = pd.read_csv(output_dir / query / "articles.csv") input_df = input_df[input_df.error.isna()] # Query Paperbuzz API logging.info('Collecting Paperbuzz metrics for {}'.format(query)) with open(str(output_dir / query / "paperbuzz.csv"), "w") as f: csv_writer = csv.writer(f) csv_writer.writerow(METRICS_COLUMNS) for pmid, doi in tqdm(zip(input_df.pmid, input_df.doi), total=len(input_df)): now = datetime.now()