Commit 5bc17829 authored by Vincent's avatar Vincent

Merge branch '5-don-t-rely-on-the-deprecated-rank' into 'master'

Resolve "Don't rely on the `deprecated` rank"

Closes #5

See merge request !3
parents 876c1bfe 9523f3a8
Pipeline #37671562 passed with stage
in 2 minutes and 14 seconds
......@@ -140,16 +140,40 @@ async function getUrls() {
const sciHubId = 'Q21980377';
const officialWebsiteProperty = 'P856';
const sparql = `
SELECT ?mainUrl ?otherUrls WHERE {
{ wd:${sciHubId} wdt:${officialWebsiteProperty} ?mainUrl. }
UNION
{ wd:${sciHubId} p:${officialWebsiteProperty} [wikibase:rank wikibase:NormalRank; ps:${officialWebsiteProperty} ?otherUrls]. }
SELECT ?urls WHERE {
{ wd:${sciHubId} p:${officialWebsiteProperty} [wikibase:rank wikibase:PreferredRank; ps:${officialWebsiteProperty} ?urls]. }
}
`;
const response = await fetch(`https://query.wikidata.org/sparql?format=json&query=${encodeURIComponent(sparql)}`);
const data = await response.json();
const urls = data.results.bindings.map(result => result.mainUrl ? result.mainUrl.value : result.otherUrls.value);
const urls = data.results.bindings.map(result => result.urls.value);
return urls;
}
\ No newline at end of file
return urls.sort(compareUrls);
}
function compareUrls(url1, url2) {
// URL beginning with `https://`, and ending in a non-digit character (of the TLD) and a potential trailing slash
const httpsDomainRE = /^https:\/\/(.*)\D\/?$/;
// URL beginning with `http://`, and ending in a non-digit character (of the TLD) and a potential trailing slash
const httpDomainRE = /^http:\/\/(.*)\D\/?$/;
// Regular URL's pointing to an HTTPS address come first
if (httpsDomainRE.test(url1)) {
return -1;
} else if(httpsDomainRE.test(url2)) {
return 1;
// IP addresses using HTTPS come second
} else if(url1.substr(0, 5) === 'https') {
return -1;
} else if(url2.substr(0, 5) === 'https') {
return 1;
// Only then come regular domains hosted through naked HTTP
} else if(httpDomainRE.test(url1)) {
return -1;
} else if(httpDomainRE.test(url2)) {
return 1;
}
// And if both are an IP address through naked HTTP, we don't care which comes first:
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment