From 68456e5b0f9265dc16b0c455ea4889d13ea699af Mon Sep 17 00:00:00 2001
From: Mg Pig
Date: Sun, 24 Mar 2024 18:04:05 +0800
Subject: [PATCH] feat(route): Add appstorrent (#14908)
* feat(route): Add appstorrent
* feat(route): appstorrent apply suggestions from code review
Co-authored-by: Tony
* fix(route): fix and organize appstorrent imports
---------
---
lib/routes/appstorrent/namespace.ts | 6 ++
lib/routes/appstorrent/programs.ts | 91 +++++++++++++++++++
.../appstorrent/templates/description.art | 22 +++++
3 files changed, 119 insertions(+)
create mode 100644 lib/routes/appstorrent/namespace.ts
create mode 100644 lib/routes/appstorrent/programs.ts
create mode 100644 lib/routes/appstorrent/templates/description.art
diff --git a/lib/routes/appstorrent/namespace.ts b/lib/routes/appstorrent/namespace.ts
new file mode 100644
index 000000000..3b5b12a00
--- /dev/null
+++ b/lib/routes/appstorrent/namespace.ts
@@ -0,0 +1,6 @@
+import type { Namespace } from '@/types';
+
+export const namespace: Namespace = {
+ name: 'AppsTorrent',
+ url: 'appstorrent.ru',
+};
diff --git a/lib/routes/appstorrent/programs.ts b/lib/routes/appstorrent/programs.ts
new file mode 100644
index 000000000..ad9d40b86
--- /dev/null
+++ b/lib/routes/appstorrent/programs.ts
@@ -0,0 +1,91 @@
+import { Data, DataItem, Route } from '@/types';
+import cache from '@/utils/cache';
+import got, { Options } from '@/utils/got';
+import { getCurrentPath } from '@/utils/helpers';
+import { parseDate } from '@/utils/parse-date';
+import { art } from '@/utils/render';
+import { load } from 'cheerio';
+import dayjs from 'dayjs';
+import { Context } from 'hono';
+import * as path from 'node:path';
+const __dirname = getCurrentPath(import.meta.url);
+
+export const route: Route = {
+ path: '/programs',
+ categories: ['program-update'],
+ example: '/appstorrent/programs',
+ name: 'Programs',
+ maintainers: ['xzzpig'],
+ handler,
+ url: 'appstorrent.ru/programs/',
+};
+
+async function handler(ctx?: Context): Promise {
+ const limit = ctx?.req.query('limit') ? Number.parseInt(ctx?.req.query('limit') ?? '20') : 20;
+ const baseUrl = 'https://appstorrent.ru';
+ const currentUrl = `${baseUrl}/programs/`;
+ const gotOptions: Options = {
+ http2: true,
+ };
+
+ const response = await got(currentUrl, gotOptions);
+ const $ = load(response.data as any);
+
+ const selector = 'article.soft-item:not(.locked)';
+ const list = $(selector)
+ .slice(0, limit)
+ .toArray()
+ .map((item) => {
+ const $item = $(item);
+ return {
+ title: $item.find('.subtitle').text().trim(),
+ link: $item.find('.subtitle a').attr('href')!,
+ category: [$item.find('.info .category').text().trim()],
+ version: $item.find('.version').text(),
+ architecture: $item.find('.architecture').text().trim(),
+ size: $item.find('.size').text().trim(),
+ };
+ });
+
+ const items: DataItem[] = await Promise.all(
+ list.map(
+ (item) =>
+ cache.tryGet(item.link, async () => {
+ const response = await got(item.link, gotOptions);
+ const $ = load(response.data as any);
+
+ const pubDate = parseDate($('.tech-info .date-news a').attr('href')?.replace('https://appstorrent.ru/', '') ?? '');
+
+ return {
+ title: item.title,
+ link: item.link,
+ category: item.category,
+ pubDate,
+ description: art(path.join(__dirname, 'templates/description.art'), {
+ cover: baseUrl + $('.main-title img').attr('src')?.trim(),
+ title: item.title,
+ pubDate: dayjs(pubDate).format('YYYY-MM-DD'),
+ version: item.version,
+ architecture: item.architecture,
+ compatibility: $('div.right > div.info > div.right-container > div:nth-child(5) > div > span:nth-child(2) > a').text(),
+ size: item.size,
+ activation: $('div.right > div.info > div.right-container > div:nth-child(4) > div > span:nth-child(2) > a').text(),
+ description: $('.content .body-content').first().text(),
+ changelog: $('.content .body-content').last().text(),
+ screenshots: $('.screenshots img')
+ .toArray()
+ .map((img) => $(img).attr('src'))
+ .map((src) => baseUrl + src),
+ }),
+ } as DataItem;
+ }) as Promise
+ )
+ );
+
+ return {
+ title: $('title').text(),
+ link: currentUrl.toString(),
+ allowEmpty: true,
+ item: items,
+ };
+}
diff --git a/lib/routes/appstorrent/templates/description.art b/lib/routes/appstorrent/templates/description.art
new file mode 100644
index 000000000..2e86e1682
--- /dev/null
+++ b/lib/routes/appstorrent/templates/description.art
@@ -0,0 +1,22 @@
+
+
+
{{ title }}
+Public Date: {{pubDate}}
+Version: {{version}}
+Architecture: {{architecture}}
+Compactibility: {{compatibility}}
+Size: {{size}}
+Activation: {{activation}}
+
+Description:
+
+{{ description }}
+
+Change Log:
+
+{{ changelog }}
+
+Screenshots
+{{each screenshots}}
+
+{{/each}}
\ No newline at end of file