Skip to content

Instantly share code, notes, and snippets.

@helix84
Forked from rivaldi8/ImpactServlet.py
Created December 18, 2015 13:20
Show Gist options
  • Save helix84/6e204d8296b731751fde to your computer and use it in GitHub Desktop.
Save helix84/6e204d8296b731751fde to your computer and use it in GitHub Desktop.
/**
* Functions to add citation count or other statistics from several
* services (like Scopus) to the impact section of an item view.
*/
var IMPACT_SERVLET_URL = "/impact/ImpactServlet.py";
function addWebOfScienceCitationCount() {
var doi = $("#wos").data("doi");
$.getJSON(IMPACT_SERVLET_URL + "?service=wos&doi=" + doi)
.done(function(data) {
var citations = data["citationCount"];
var linkBack = data["linkBack"];
if (citations && linkBack) {
$("#wos a").attr("href", linkBack);
$("#wos .citation-count").html(citations);
$("#wos").removeClass("hidden");
}
})
.fail(function(jqxhr, textStatus, error) {
console.log("Error retrieving Web of Science citation count: " + error);
});
}
function addScopusCitationCount() {
var doi = $("#scopus").data("doi");
$.getJSON(IMPACT_SERVLET_URL + "?service=scopus&doi=" + doi)
.done(function(data) {
if (data["citation-count-response"]["document"]["@status"] === "not_found")
return;
var citations = data["citation-count-response"]["document"]["citation-count"];
var linkBack = getScopusLinkBack(data);
if (citations > 0) {
$("#scopus a").attr("href", linkBack);
$("#scopus .citation-count").html(citations);
$("#scopus").removeClass("hidden");
}
})
.fail(function(jqxhr, textStatus, error) {
console.log("Error retrieving Scopus citation count: " + error);
});
}
function getScopusLinkBack(data) {
var linkList = data["citation-count-response"]["document"]["link"];
var linkBackObject = $.grep(linkList, function(linkObject) {
return linkObject["@rel"] === "scopus-citedby";
})[0];
return linkBackObject["@href"];
}
function addCitationCounts() {
addWebOfScienceCitationCount();
addScopusCitationCount();
}
$(document).ready(addCitationCounts);
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
import json
import xml.etree.ElementTree as ElementTree
from javax.servlet.http import HttpServlet
class ImpactServlet(HttpServlet):
def doGet(self, request, response):
self.doPost(request, response)
def doPost(self, request, response):
serviceToQuery = request.getParameter("service")
if serviceToQuery == "scopus":
service = Scopus(request)
elif serviceToQuery == "wos":
service = WebOfScience(request)
else:
response.sendError(response.SC_NOT_IMPLEMENTED,
"Incorrect service '{0}'.".format(serviceToQuery))
return
response.setContentType("application/json")
response.setCharacterEncoding("UTF-8")
response.getWriter().println(service.query())
class ImpactService(object):
def __init__(self, request):
raise NotImplementedError()
def query(self):
raise NotImplementedError()
class Scopus(ImpactService):
QUERY_URL = "http://api.elsevier.com/content/abstract/citation-count?httpAccept=application/json&apiKey={0}&doi={1}"
API_KEY = "YOUR-API-KEY"
def __init__(self, request):
self.doi = request.getParameter("doi")
def query(self):
response = requests.get(self.QUERY_URL.format(self.API_KEY, self.doi))
return response.text
class WebOfScience(ImpactService):
QUERY_URL = "http://gateway.webofknowledge.com/gateway/Gateway.cgi"
QUERY_DOC = """<?xml version="1.0" encoding="UTF-8"?>
<request xmlns="http://www.isinet.com/xrpc42">
<fn name="LinksAMR.retrieve">
<list>
<map>
<val name="username">your-username</val>
<val name="password">your-password</val>
</map>
<map>
<list name="WOS">
<val>timesCited</val>
<val>citingArticlesURL</val>
</list>
</map>
<map>
<map name="cite_1">
<val name="doi">{0}</val>
</map>
</map>
</list>
</fn>
</request>
"""
TIMES_CITED_XPATH = ".//wos:map[@name='cite_1']/wos:map[@name='WOS']/wos:val[@name='timesCited']"
LINKBACK_XPATH = ".//wos:map[@name='cite_1']/wos:map[@name='WOS']/wos:val[@name='citingArticlesURL']"
def __init__(self, request):
self.doi = request.getParameter("doi")
def query(self):
wosResponse = requests.post(self.QUERY_URL, data=self.QUERY_DOC.format(self.doi))
return self.buildJsonResponse(wosResponse.text)
def buildJsonResponse(self, wosXmlResponse):
root = ElementTree.fromstring(wosXmlResponse)
namespaces = {"wos": "http://www.isinet.com/xrpc42"}
timesCited = root.findtext(self.TIMES_CITED_XPATH, namespaces=namespaces)
linkBack = root.findtext(self.LINKBACK_XPATH, namespaces=namespaces)
responseData = {}
responseData["citationCount"] = timesCited
responseData["linkBack"] = linkBack
return json.dumps(responseData)
<xsl:template name="impact-wos">
<script src="{$theme-path}scripts/impact.js" defer="defer">&#160;</script>
<p id="wos" class="hidden">
<xsl:attribute name="data-doi">
<xsl:value-of select="$identifier_doi" />
</xsl:attribute>
<a href="to be filled by impact.js">
<img src="{concat($theme-path,'/images/wos.png')}"
i18n:attr="alt"
alt="xmlui.mirage2.itemSummaryView.ImpactSection.WosLogoAlt" />&#x2002;
<span class="citation-count"></span>&#x000A0;
<i18n:text>xmlui.mirage2.itemSummaryView.ImpactSection.WosLink</i18n:text>
</a>
</p>
</xsl:template>
<xsl:template name="impact-scopus">
<script src="{$theme-path}scripts/impact.js" defer="defer">&#160;</script>
<p id="scopus" class="hidden">
<xsl:attribute name="data-doi">
<xsl:value-of select="$identifier_doi" />
</xsl:attribute>
<a href="to be filled by impact.js">
<img src="{concat($theme-path,'/images/scopus.png')}"
i18n:attr="alt"
alt="xmlui.mirage2.itemSummaryView.ImpactSection.ScopusLogoAlt" />&#x2002;
<span class="citation-count"></span>&#x000A0;
<i18n:text>xmlui.mirage2.itemSummaryView.ImpactSection.ScopusLink</i18n:text>
</a>
</p>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment