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
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();
// PubMed search function (modified to accept more parameters)
async function pubmedSearch(query, userId, additionalParams = {}) {
const url = 'https://www.pubmedisearch.com/api/fetch_articles';
const options = {
method: 'POST',
headers: {
'accept': '*/*',
'accept-language': 'en-US,en;q=0.9,sv;q=0.8,pt;q=0.7,es;q=0.6,fr;q=0.5,af;q=0.4,da;q=0.3',
'content-type': 'application/json',
'cookie': `AMP_MKTG_f155593c51=JTdCJTIycmVmZXJyZXIlMjIlM0ElMjJodHRwcyUzQSUyRiUyRm5ld3MueWNvbWJpbmF0b3IuY29tJTJGJTIyJTJDJTIycmVmZXJyaW5nX2RvbWFpbiUyMiUzQSUyMm5ld3MueWNvbWJpbmF0b3IuY29tJTIyJTdE; userId=${userId}; AMP_f155593c51=JTdCJTIyZGV2aWNlSWQlMjIl
'origin': 'https://www.pubmedisearch.com',
'referer': 'https://www.pubmedisearch.com/',
'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'
},
body: JSON.stringify({
query: query,
userId: userId,
...additionalParams
})
};
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;
}
}
// GET endpoint
app.get('/', async (c) => {
const query = c.req.query('query');
const userId = c.req.query('userId') || 'a756948c-dd73-449a-a215-ea4feefd40a9';
if (!query) {
return c.json({ error: 'Query parameter is required' }, 400);
}
try {
console.log({ mode: 'get/pubmedsearch', query, userId });
const results = await pubmedSearch(query, userId);
return c.json(results);
} catch (error) {
console.error('API error:', error);
return c.json({ error: 'An error occurred while processing your request' }, 500);
}
});
// POST endpoint
app.post('/', async (c) => {
const { query, userId = 'a756948c-dd73-449a-a215-ea4feefd40a9', ...additionalParams } = await c.req.json();
if (!query) {
return c.json({ error: 'Query parameter is required' }, 400);
}
try {
console.log({ mode: 'post/pubmedsearch', query, userId });
const results = await pubmedSearch(query, userId, additionalParams);
return c.json(results);
} catch (error) {
console.error('API error:', error);
return c.json({ error: 'An error occurred while processing your request' }, 500);
}
});
export default app.fetch;
export { pubmedSearch };
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 11, 2024