From 06c725c086d867807dc0fccd1dbbd48c9a15d7ff Mon Sep 17 00:00:00 2001 From: Ethan Shen <42264778+nczitzk@users.noreply.github.com> Date: Sat, 23 Sep 2023 01:56:24 +0800 Subject: [PATCH] feat(route): add Surfshark Blog (#13367) --- lib/v2/surfshark/blog.js | 91 ++++++++++++++++++++++ lib/v2/surfshark/maintainer.js | 3 + lib/v2/surfshark/radar.js | 90 +++++++++++++++++++++ lib/v2/surfshark/router.js | 3 + lib/v2/surfshark/templates/description.art | 5 ++ website/docs/routes/blog.md | 24 ++++++ 6 files changed, 216 insertions(+) create mode 100644 lib/v2/surfshark/blog.js create mode 100644 lib/v2/surfshark/maintainer.js create mode 100644 lib/v2/surfshark/radar.js create mode 100644 lib/v2/surfshark/router.js create mode 100644 lib/v2/surfshark/templates/description.art diff --git a/lib/v2/surfshark/blog.js b/lib/v2/surfshark/blog.js new file mode 100644 index 000000000..674d3ba49 --- /dev/null +++ b/lib/v2/surfshark/blog.js @@ -0,0 +1,91 @@ +const got = require('@/utils/got'); +const cheerio = require('cheerio'); +const { parseDate } = require('@/utils/parse-date'); +const { art } = require('@/utils/render'); +const path = require('path'); + +module.exports = async (ctx) => { + const { category } = ctx.params; + const limit = ctx.query.limit ? parseInt(ctx.query.limit, 10) : 15; + + const rootUrl = 'https://surfshark.com'; + const currentUrl = new URL(`blog${category ? `/${category}` : ''}`, rootUrl).href; + + const { data: response } = await got(currentUrl); + + const $ = cheerio.load(response); + + let items = $('div.article-info') + .slice(0, limit) + .toArray() + .map((item) => { + item = $(item); + + const a = item.find('a'); + + return { + title: a.prop('title'), + link: a.prop('href'), + author: item.find('div.author, div.name').text().split('in')[0].trim(), + category: item + .find('div.author, div.name') + .find('a') + .toArray() + .map((c) => $(c).text()), + pubDate: parseDate(item.find('div.date, div.time').text().split('·')[0].trim(), 'YYYY, MMMM D'), + }; + }); + + items = await Promise.all( + items.map((item) => + ctx.cache.tryGet(item.link, async () => { + const { data: detailResponse } = await got(item.link); + + const content = cheerio.load(detailResponse); + + content('div.post-main-img').each(function () { + const image = content(this).find('img'); + + content(this).replaceWith( + art(path.join(__dirname, 'templates/description.art'), { + image: { + src: image + .prop('srcset') + .match(/(https?:.*?\.\w+\s)/g) + .pop() + .trim(), + alt: image.prop('alt'), + }, + }) + ); + }); + + item.title = content('div.blog-post-title').text(); + item.description = content('div.post-content').html(); + item.author = content('meta[name="author"]').prop('content'); + item.category = content('div.name') + .find('a') + .toArray() + .map((c) => content(c).text()); + item.pubDate = parseDate(content('meta[property="article:published_time"]').prop('content')); + + return item; + }) + ) + ); + + const icon = new URL($('link[rel="apple-touch-icon"]').prop('href'), rootUrl).href; + + ctx.state.data = { + item: items, + title: $('title').text(), + link: currentUrl, + description: $('meta[property="og:description"]').prop('content'), + language: $('html').prop('lang'), + image: $('meta[property="og:image"]').prop('content'), + icon, + logo: icon, + subtitle: $('meta[property="og:title"]').prop('content'), + author: $('meta[property="og:site_name"]').prop('content'), + }; +}; diff --git a/lib/v2/surfshark/maintainer.js b/lib/v2/surfshark/maintainer.js new file mode 100644 index 000000000..d8eab4f11 --- /dev/null +++ b/lib/v2/surfshark/maintainer.js @@ -0,0 +1,3 @@ +module.exports = { + '/blog/:category?': ['nczitzk'], +}; diff --git a/lib/v2/surfshark/radar.js b/lib/v2/surfshark/radar.js new file mode 100644 index 000000000..4108f82db --- /dev/null +++ b/lib/v2/surfshark/radar.js @@ -0,0 +1,90 @@ +module.exports = { + 'surfshark.com': { + _name: 'Surfshark', + '.': [ + { + title: 'Blog', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/blog/:category*'], + target: (params, url) => { + url = new URL(url); + const path = params.category ?? url.href.match(/\/blog\/(.*?)/)[1]; + + return `/surfshark/blog${path ? `/${path}` : ''}`; + }, + }, + { + title: 'Blog - Cybersecurity', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/cybersecurity'], + target: '/surfshark/blog/cybersecurity', + }, + { + title: 'Blog - All things VPN', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/all-things-vpn'], + target: '/surfshark/blog/all-things-vpn', + }, + { + title: 'Blog - Internet censorship', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/internet-censorship'], + target: '/surfshark/blog/internet-censorship', + }, + { + title: 'Blog - Entertainment', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/entertainment'], + target: '/surfshark/blog/entertainment', + }, + { + title: 'Blog - News', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/news'], + target: '/surfshark/blog/news', + }, + { + title: 'Blog - Internet Security', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/cybersecurity/internet-security', '/blog/cybersecurity'], + target: '/surfshark/blog/cybersecurity/internet-security', + }, + { + title: 'Blog - Mobile Security', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/cybersecurity/mobile-security', '/blog/cybersecurity'], + target: '/surfshark/blog/cybersecurity/mobile-security', + }, + { + title: 'Blog - Identity Protection', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/cybersecurity/identity-protection', '/blog/cybersecurity'], + target: '/surfshark/blog/cybersecurity/identity-protection', + }, + { + title: 'Blog - Phishing', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/cybersecurity/phishing', '/blog/cybersecurity'], + target: '/surfshark/blog/cybersecurity/phishing', + }, + { + title: 'Blog - Must-knows', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/all-things-vpn/must-knows', '/blog/all-things-vpn'], + target: '/surfshark/blog/all-things-vpn/must-knows', + }, + { + title: 'Blog - Technology', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/all-things-vpn/technology', '/blog/all-things-vpn'], + target: '/surfshark/blog/all-things-vpn/technology', + }, + { + title: 'Blog - Tips & Advice', + docs: 'https://docs.rsshub.app/routes/blog#surfshark-blog', + source: ['/blog/all-things-vpn/tips-and-advice', '/blog/all-things-vpn'], + target: '/surfshark/blog/all-things-vpn/tips-and-advice', + }, + ], + }, +}; diff --git a/lib/v2/surfshark/router.js b/lib/v2/surfshark/router.js new file mode 100644 index 000000000..fff6a211f --- /dev/null +++ b/lib/v2/surfshark/router.js @@ -0,0 +1,3 @@ +module.exports = (router) => { + router.get('/blog/:category*', require('./blog')); +}; diff --git a/lib/v2/surfshark/templates/description.art b/lib/v2/surfshark/templates/description.art new file mode 100644 index 000000000..16a5adf2b --- /dev/null +++ b/lib/v2/surfshark/templates/description.art @@ -0,0 +1,5 @@ +{{ if image }} +
+ {{ image.alt }} +
+{{ /if }} \ No newline at end of file diff --git a/website/docs/routes/blog.md b/website/docs/routes/blog.md index 308991e56..921bb87af 100644 --- a/website/docs/routes/blog.md +++ b/website/docs/routes/blog.md @@ -264,6 +264,30 @@ Limit the number of entries to be retrieved by adding `?limit=x` to the end of t +## Surfshark {#surfshark} + +### Blog {#surfshark-blog} + + + +| Home | Cybersecurity | All things VPN | Internet censorship | Entertainment | News | +| ---- | ------------- | -------------- | ------------------- | ------------- | ---- | +| | cybersecurity | all-things-vpn | internet-censorship | entertainment | news | + +#### Cybersecurity {#surfshark-blog-cybersecurity} + +| Internet Security | Mobile Security | Identity Protection | Phishing | +| ------------------------------- | ----------------------------- | --------------------------------- | ---------------------- | +| cybersecurity/internet-security | cybersecurity/mobile-security | cybersecurity/identity-protection | cybersecurity/phishing | + +#### All things VPN {#surfshark-blog-all-things-vpn} + +| Must-knows | Technology | Tips & Advice | +| -------------- | -------------- | ------------------- | +| vpn/must-knows | vpn/technology | vpn/tips-and-advice | + + + ## Uber 优步 {#uber-you-bu} ### Engineering {#uber-you-bu-engineering}