Skip to content

Commit

Permalink
add parameters and use templates
Browse files Browse the repository at this point in the history
  • Loading branch information
etShaw-zh committed Jan 4, 2025
1 parent 9e2b2cf commit 97635b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
55 changes: 20 additions & 35 deletions lib/routes/sciencedirect/call-for-paper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
import got from '@/utils/got';
import { load } from 'cheerio';
import { art } from '@/utils/render';
import path from 'node:path';
const __dirname = getCurrentPath(import.meta.url);

export const route: Route = {
path: '/call-for-paper/:subject',
categories: ['journal'],
example: '/sciencedirect/call-for-paper/education',
parameters: {},
parameters: {
subject: '学科分类,例如“education”',
},
radar: [
{
source: ['sciencedirect.com'],
Expand All @@ -18,26 +25,10 @@ export const route: Route = {
description: '`sciencedirect.com/browse/calls-for-papers?subject=education` -> `/sciencedirect/call-for-paper/education`',
};

/**
* This handler fetches the calls-for-papers page on ScienceDirect,
* extracts the embedded JSON data from the <script> element with data-iso-key="_0",
* and generates RSS items from it.
*/
async function handler(ctx) {
// Example: subject=education
const { subject = '' } = ctx.req.param(); // or ctx.req.param() in some frameworks
// Construct the URL and the Header
const { subject = '' } = ctx.req.param();
const apiUrl = `https://www.sciencedirect.com/browse/calls-for-papers?subject=${subject}`;
const headers = {
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,...',
'accept-language': 'zh,zh-TW;q=0.9,en-US;q=0.8,en;q=0.7,zh-CN;q=0.6',
'cache-control': 'no-cache',
pragma: 'no-cache',
'upgrade-insecure-requests': '1',
};
// Fetch the page
const response = await got(apiUrl, { headers });
// Load into Cheerio
const response = await got(apiUrl);
const $ = load(response.body);

// 1) Grab the JSON from the script tag with data-iso-key="_0".
Expand All @@ -49,8 +40,8 @@ async function handler(ctx) {
// 2) Parse the JSON string
let data;
try {
data = JSON.parse(JSON.parse(scriptJSON ));
} catch (error) {
data = JSON.parse(JSON.parse(scriptJSON));
} catch (error: any) {
throw new Error(`Failed to parse embedded script JSON: ${error.message}`);
}

Expand All @@ -62,26 +53,20 @@ async function handler(ctx) {

// 4) Build a list of items to return in the feed
const items = cfpList.map((cfp) => {
// Construct a link to the CfP detail page
const link = `https://www.sciencedirect.com/special-issue/${cfp.contentId}/${cfp.url}`;

// Build a description from the CfP fields
const descriptionParts = [];
if (cfp.summary) {
descriptionParts.push(`<p><strong>Summary:</strong> ${cfp.summary}</p>`);
}
if (cfp.submissionDeadline) {
descriptionParts.push(`<p><strong>Submission Deadline:</strong> ${cfp.submissionDeadline}</p>`);
}
if (cfp.journal) {
descriptionParts.push(`<p><strong>Journal:</strong> ${cfp.journal.displayName} (IF: ${cfp.journal.impactFactor}, CiteScore: ${cfp.journal.citeScore})</p>`);
}
const description = art(path.join(__dirname, 'templates/description.art'), {
summary: cfp.summary,
submissionDeadline: cfp.submissionDeadline,
displayName: cfp.journal.displayName,
impactFactor: cfp.journal.impactFactor,
citeScore: cfp.journal.citeScore,
});

return {
title: cfp.title,
author: cfp.journal ? `${cfp.journal.displayName} (IF: ${cfp.journal.impactFactor})` : '',
link,
description: descriptionParts.join(''),
description,
pubDate: cfp.submissionDeadline || '',
};
});
Expand Down
5 changes: 5 additions & 0 deletions lib/routes/sciencedirect/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<p><strong>Summary:</strong> {{summary}} </p>
<p><strong>Submission Deadline:</strong> {{submissionDeadline}}</p>
<p><strong>Journal:</strong> {{displayName}} (IF: {{impactFactor}}, CiteScore: {{citeScore}})</p>
</div>

0 comments on commit 97635b7

Please sign in to comment.