Skip to content

Commit

Permalink
feat: Add semantic API for searching venue
Browse files Browse the repository at this point in the history
Signed-off-by: SPC <github@spcsky.com>
  • Loading branch information
specialpointcentral committed Feb 13, 2024
1 parent c144226 commit 8acd809
Showing 1 changed file with 55 additions and 26 deletions.
81 changes: 55 additions & 26 deletions js/semanticscholar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ const semanticscholar = {};

semanticscholar.rankSpanList = [];

let count = 0;
let maxCount = 5;

semanticscholar.run = function () {
let url = window.location.pathname;
setInterval(function () {
$(window).bind("popstate", function () {
$(window).on('load', function() {
let url = window.location.pathname;
let intervalId = setInterval(function () {
count++;
if (count === maxCount) {
clearInterval(intervalId);
}
semanticscholar.appendRanks();
});
semanticscholar.appendRanks();
}, 700);
}, 700);
});
};

semanticscholar.appendRanks = function () {
Expand All @@ -24,31 +30,54 @@ semanticscholar.appendRanks = function () {
let element = $(this);
let source = element[0].innerText;
if (source.length != 0 && !element.next().hasClass("ccf-rank")) {
for (let getRankSpan of semanticscholar.rankSpanList) {
if (source.includes('(')) {
source = source.substring(source.indexOf('(') + 1, source.indexOf(')'));
}
if (source.includes('\'')) {
source = source.substring(0, source.indexOf('\'')).trim();
}
element.after(getRankSpan(source, 'abbr'));
let paperNode = element.parents('.cl-paper-row').first();
// <sha> - a Semantic Scholar ID
let paperId = paperNode.attr('data-paper-id');
if (paperId == undefined) {
// In paper detail page, i.e. https://www.semanticscholar.org/paper/...
paperNode = element.parents("[data-test-id='paper-detail-page-header']").first();
paperNode = paperNode.find("[data-test-id='corpus-id']").first();
// CorpusId:<id> - a Semantic Scholar numerical ID
paperId = "CorpusId:" + paperNode.text().match(/\d+/)[0];
}
let appendRankBar = semanticscholar.appendRankBar;
semanticscholar.fetchData(paperId, element, appendRankBar);
}
});
};

semanticscholar.appendRank = function (selector) {
let element = $(selector);
let headline = window.location.pathname;
if (headline.length != 0) {
for (let getRankSpan of dblp.rankSpanList) {
let urls = headline.substring(
headline.indexOf("/db/") + 3,
headline.lastIndexOf("/")
);
url = ccf.rankDb[urls];
element.after(getRankSpan(url, "url"));
let semanticAPI = `https://api.semanticscholar.org/graph/v1/paper/{paper_id}?fields=venue`;

semanticscholar.fetchData = function (paperId, element, callback) {
let query_url = semanticAPI.replace('{paper_id}', paperId);
let cached = apiCache.getItem(query_url);
if(cached) {
venue = cached.venue;
callback(element, venue);
return;
}
// none cached
$.get(query_url)
.done(function (data) {
apiCache.setItem(query_url, data);
venue = data.venue;
callback(element, venue);
})
.fail(function () {
console.log("Request failed.");
});
}

semanticscholar.appendRankBar = function (selector, source) {
if (selector.next().hasClass("ccf-rank")) return;

for (let getRankSpan of semanticscholar.rankSpanList) {
if (source.includes('(')) {
source = source.substring(source.indexOf('(') + 1, source.indexOf(')'));
}
if (source.includes('\'')) {
source = source.substring(0, source.indexOf('\'')).trim();
}
selector.after(getRankSpan(source, 'abbr'));
}
};

0 comments on commit 8acd809

Please sign in to comment.