-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): add Surfshark Blog (#13367)
- Loading branch information
Showing
6 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
'/blog/:category?': ['nczitzk'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = (router) => { | ||
router.get('/blog/:category*', require('./blog')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{{ if image }} | ||
<figure> | ||
<img src="{{ image.src }}" alt="{{ image.alt }}"> | ||
</figure> | ||
{{ /if }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters