yawnxyz-unpaywall.web.val.run
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Hono } from "npm:hono@3";
import { cors } from "npm:hono/cors";
import { fetch } from "https://esm.town/v/std/fetch";
const app = new Hono();
const YOUR_EMAIL = "jan@phage.directory";
// Unpaywall DOI access function
async function unpaywallDOI(doi) {
const url = `https://api.unpaywall.org/v2/${doi}?email=${YOUR_EMAIL}`;
const options = {
method: 'GET',
headers: {
'accept': 'application/json',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
}
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// Unpaywall search function
async function unpaywallSearch(query, additionalParams = {}) {
const url = new URL('https://api.unpaywall.org/v2/search');
url.searchParams.append('query', query);
url.searchParams.append('email', YOUR_EMAIL);
Object.entries(additionalParams).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
url.searchParams.append(key, value);
}
});
const options = {
method: 'GET',
headers: {
'accept': 'application/json',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36'
}
};
try {
console.log('search url:', url)
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error('Error:', error);
throw error;
}
}
// GET endpoint for DOI
// https://yawnxyz-unpaywall.web.val.run/doi/10.1038/nature12373
app.get('/doi/*', async (c) => {
// const doi = c.req.param('doi');
const url = new URL(c.req.url);
const doi = url.pathname.slice(1);
if (!doi) {
return c.json({ error: 'DOI parameter is required' }, 400);
}
try {
console.log({ mode: 'get/unpaywallDOI', doi });
const results = await unpaywallDOI(doi);
return c.json(results);
} catch (error) {
console.error('API error:', error);
return c.json({ error: 'An error occurred while processing your request' }, 500);
}
});
// GET endpoint for search
app.get('/search', async (c) => {
const query = c.req.query('query') || c.req.query('q');
const is_oa = c.req.query('is_oa');
const page = c.req.query('page');
if (!query) {
return c.json({ error: 'Query parameter is required' }, 400);
}
try {
console.log({ mode: 'get/unpaywallSearch', query, is_oa, page });
const additionalParams = {};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 15, 2024