Unicodesnowman has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/176887

Change subject: Add requestFromPMID (and aliases for PMCID, Manuscript ID), 
which fetches DOI and grabs response of that
......................................................................

Add requestFromPMID (and aliases for PMCID, Manuscript ID), which fetches DOI 
and grabs response of that

Change-Id: I1852f9820d9a0e1af0f30003d84846b435095e16
---
M lib/requests.js
1 file changed, 73 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/citoid 
refs/changes/87/176887/1

diff --git a/lib/requests.js b/lib/requests.js
index dfde7d2..932330a 100644
--- a/lib/requests.js
+++ b/lib/requests.js
@@ -12,6 +12,7 @@
 var scrape = require('./scrape.js').scrape;
 var zoteroRequest = require('./zotero.js').zoteroRequest;
 var util = require('util');
+var request = require('request');
 
 /**
  * runner function to use for a url
@@ -99,6 +100,69 @@
        });
 };
 
+/**
+ * parses a PMID, PMCID (must begin with PMC), manuscript ID, finds the DOI, 
and runs requestFromDOI
+ * @param  {[type]}   requestedPMID want metadata from this PMID / PMCID / 
manuscript ID
+ * @param  {[type]}   opts          zoteroRequest options object
+ * @param  {Function} callback      callback (error, statusCode, body)
+ */
+
+var requestFromPMID = function (requestedPMID, opts, callback){
+    var pmidLink = 
'http://www.ncbi.nlm.nih.gov/pmc/utils/idconv/v1.0/?tool=citoid&email=citoid@mediawiki&format=json&ids='+requestedPMID;
+    requestJSON(pmidLink, {}, function(error, obj){
+               if(error){
+                       callback(error, null, "");
+               }
+               else if(obj.status !== "ok"){
+                       log.error("PubMed returned unrecognized status code: " 
+ obj.status);
+                       callback("PubMed returned unrecognized status", 200, 
"");
+               }
+               else {
+                       if(obj.records.length > 0){
+                               var doi = obj.records[0].doi;
+                               console.log("Got DOI " + doi);
+                               requestFromDOI(doi, opts, callback);
+                       } else {
+                               log.error("No records retrieved from PubMed for 
" + requestedPMID);
+                               callback("No records rerieved from PubMed", 
200, "");
+                       }
+               }
+
+       });
+};
+
+/**
+ * fetch and parse a JSON endpoint, returns the JS object.
+ * @param  {[type]}   url      JSON endpoint to fetch and parse
+ * @param  {[type]}   opts     request options object
+ * @param  {Function} callback callback (error, object)
+ */
+
+var requestJSON = function (url, opts, callback){
+    opts.url = url;
+    request(opts, function(error, response, body){
+        log.info("JSON query made for: " + url);
+        if ( error ) {
+            log.error(error);
+            return callback(true, "");
+        }
+        else if ( response.statusCode !== 200 ) {
+            log.error("JSON query returned HTTP status code " + 
response.statusCode);
+            return callback(true, "");
+        }
+        else {
+            try {
+                var jsonObj = JSON.parse(body);
+                return callback(false, jsonObj);
+            } catch ( error ) {
+                log.error("Error when parsing JSON response: " + error);
+                return callback(true, "");
+            }
+        }
+    });
+};
+
+
 /*Test methods in main */
 if (require.main === module) {
        var opts = {
@@ -110,9 +174,16 @@
        requestFromURL("http://example.com";, opts, function (error, statusCode, 
body){
                console.log(body);
        });
+
+       requestFromPMID("23193287", opts, function(error, statusCode, body){
+               console.log(body);
+       });
 }
 
 module.exports = {
        requestFromURL: requestFromURL,
-       requestFromDOI: requestFromDOI
-};
\ No newline at end of file
+       requestFromDOI: requestFromDOI,
+       requestFromPMID: requestFromPMID,
+       requestFromPMCID: requestFromPMID, // alias
+       requestFromManuscript: requestFromPMID // alias
+};

-- 
To view, visit https://gerrit.wikimedia.org/r/176887
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1852f9820d9a0e1af0f30003d84846b435095e16
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/citoid
Gerrit-Branch: master
Gerrit-Owner: Unicodesnowman <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to