From 548770dd9149819bc0798ba96f4e298f96ee53b3 Mon Sep 17 00:00:00 2001
From: Ethan Shen <42264778+nczitzk@users.noreply.github.com>
Date: Thu, 21 Sep 2023 00:59:05 +0800
Subject: [PATCH 1/8] =?UTF-8?q?feat(route):=20add=20=E6=96=B0=E7=89=87?=
=?UTF-8?q?=E5=9C=BA=20(#13345)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat(route): add 新片场
* fix typo
---
lib/v2/xinpianchang/index.js | 19 +++
lib/v2/xinpianchang/maintainer.js | 4 +
lib/v2/xinpianchang/radar.js | 28 +++++
lib/v2/xinpianchang/rank.js | 33 ++++++
lib/v2/xinpianchang/router.js | 5 +
lib/v2/xinpianchang/templates/description.art | 13 +++
lib/v2/xinpianchang/util.js | 110 ++++++++++++++++++
website/docs/routes/new-media.md | 31 +++++
8 files changed, 243 insertions(+)
create mode 100644 lib/v2/xinpianchang/index.js
create mode 100644 lib/v2/xinpianchang/maintainer.js
create mode 100644 lib/v2/xinpianchang/radar.js
create mode 100644 lib/v2/xinpianchang/rank.js
create mode 100644 lib/v2/xinpianchang/router.js
create mode 100644 lib/v2/xinpianchang/templates/description.art
create mode 100644 lib/v2/xinpianchang/util.js
diff --git a/lib/v2/xinpianchang/index.js b/lib/v2/xinpianchang/index.js
new file mode 100644
index 00000000000000..36f1cb045363ce
--- /dev/null
+++ b/lib/v2/xinpianchang/index.js
@@ -0,0 +1,19 @@
+const { rootUrl, getData, processItems } = require('./util');
+
+module.exports = async (ctx) => {
+ const { params = 'article-0-0-all-all-0-0-score' } = ctx.params;
+ const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 60;
+
+ const currentUrl = new URL(`discover/${params}`, rootUrl).href;
+
+ const { data, response } = await getData(currentUrl, ctx.cache.tryGet);
+
+ let items = JSON.parse(response.match(/"list":(\[.*?\]),"total"/)[1]);
+
+ items = await processItems(items.slice(0, limit), ctx.cache.tryGet);
+
+ ctx.state.data = {
+ ...data,
+ item: items,
+ };
+};
diff --git a/lib/v2/xinpianchang/maintainer.js b/lib/v2/xinpianchang/maintainer.js
new file mode 100644
index 00000000000000..0ddc7131e22dc2
--- /dev/null
+++ b/lib/v2/xinpianchang/maintainer.js
@@ -0,0 +1,4 @@
+module.exports = {
+ '/discover/:params?': ['nczitzk'],
+ '/rank/:category?': ['nczitzk'],
+};
diff --git a/lib/v2/xinpianchang/radar.js b/lib/v2/xinpianchang/radar.js
new file mode 100644
index 00000000000000..45cea9df995c81
--- /dev/null
+++ b/lib/v2/xinpianchang/radar.js
@@ -0,0 +1,28 @@
+module.exports = {
+ 'xinpianchang.com': {
+ _name: '新片场',
+ '.': [
+ {
+ title: '发现',
+ docs: 'https://docs.rsshub.app/routes/new-media#xin-pian-chang-fa-xian',
+ source: ['/discover/:params'],
+ target: (params, url) => {
+ url = new URL(url);
+ const path = params.params ?? url.href.match(/discover\/(article.*?)/)[1];
+
+ return `/xinpianchang/discover${path ? `/${path}` : ''}`;
+ },
+ },
+ {
+ title: '排行榜',
+ docs: 'https://docs.rsshub.app/routes/new-media#xin-pian-chang-pai-hang-bang',
+ source: ['/rank/:params'],
+ target: (params, url) => {
+ const path = params.params.match(/article-(\w+)-\d+-\d+/)[1] ?? url.href.match(/rank\/article-(\w+)-\d+-\d+/)[1];
+
+ return `/xinpianchang/rank${path ? `/${path}` : ''}`;
+ },
+ },
+ ],
+ },
+};
diff --git a/lib/v2/xinpianchang/rank.js b/lib/v2/xinpianchang/rank.js
new file mode 100644
index 00000000000000..60140a87fff47b
--- /dev/null
+++ b/lib/v2/xinpianchang/rank.js
@@ -0,0 +1,33 @@
+const got = require('@/utils/got');
+
+const { rootUrl, getData, processItems } = require('./util');
+
+module.exports = async (ctx) => {
+ const { category = 'all' } = ctx.params;
+ const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 60;
+
+ const apiRankUrl = new URL(`api/xpc/v2/rank/${category}`, rootUrl).href;
+
+ const { data: apiResponse } = await got(apiRankUrl);
+
+ const current = apiResponse.data.list[0];
+ const currentUrl = current.web_link;
+ const currentName = `${current.code}-${current.year}-${current.index}`;
+
+ const { data, response: currentResponse } = await getData(currentUrl, ctx.cache.tryGet);
+
+ const buildId = currentResponse.match(/\/static\/(\w+)\/_buildManifest\.js/)[1];
+
+ const apiUrl = new URL(`_next/data/${buildId}/rank/article/${currentName}.json`, rootUrl).href;
+
+ const { data: response } = await got(apiUrl);
+
+ let items = response.pageProps.rankList;
+
+ items = await processItems(items.slice(0, limit), ctx.cache.tryGet);
+
+ ctx.state.data = {
+ ...data,
+ item: items,
+ };
+};
diff --git a/lib/v2/xinpianchang/router.js b/lib/v2/xinpianchang/router.js
new file mode 100644
index 00000000000000..4084aa94f5b74c
--- /dev/null
+++ b/lib/v2/xinpianchang/router.js
@@ -0,0 +1,5 @@
+module.exports = (router) => {
+ router.get('/discover/:params?', require('./'));
+ router.get('/rank/:category?', require('./rank'));
+ router.get('/:params?', require('./'));
+};
diff --git a/lib/v2/xinpianchang/templates/description.art b/lib/v2/xinpianchang/templates/description.art
new file mode 100644
index 00000000000000..3a8645dbadd04b
--- /dev/null
+++ b/lib/v2/xinpianchang/templates/description.art
@@ -0,0 +1,13 @@
+{{ if content }}
+
{{ content }}
+{{ /if }}
+
+{{ if enclousure }}
+
+{{ /if }}
\ No newline at end of file
diff --git a/lib/v2/xinpianchang/util.js b/lib/v2/xinpianchang/util.js
new file mode 100644
index 00000000000000..c3f5125fc455fe
--- /dev/null
+++ b/lib/v2/xinpianchang/util.js
@@ -0,0 +1,110 @@
+const got = require('@/utils/got');
+const cheerio = require('cheerio');
+const { parseDate } = require('@/utils/parse-date');
+const { art } = require('@/utils/render');
+const path = require('path');
+
+const appKey = '61a2f329348b3bf77';
+
+const domain = 'xinpianchang.com';
+const rootUrl = `https://www.${domain}`;
+const rootApiUrl = `https://mod-api.${domain}`;
+
+/**
+ * Retrieves information from a given URL using a provided tryGet function.
+ *
+ * @param {string} url - The URL to fetch information from.
+ * @param {Function} tryGet - The tryGet function that handles the retrieval process.
+ * @returns {Promise