mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-02-02 16:05:19 -05:00
api/popular: cross-reference with RSS feed to get post titles
This commit is contained in:
@@ -4,14 +4,19 @@ const faunadb = require("faunadb"),
|
|||||||
q = faunadb.query;
|
q = faunadb.query;
|
||||||
const numeral = require("numeral");
|
const numeral = require("numeral");
|
||||||
const pluralize = require("pluralize");
|
const pluralize = require("pluralize");
|
||||||
|
const rssParser = require("rss-parser");
|
||||||
require("dotenv").config();
|
require("dotenv").config();
|
||||||
|
|
||||||
exports.handler = async () => {
|
exports.handler = async () => {
|
||||||
try {
|
try {
|
||||||
|
const parser = new rssParser({
|
||||||
|
timeout: 3000,
|
||||||
|
});
|
||||||
const client = new faunadb.Client({
|
const client = new faunadb.Client({
|
||||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const feed = await parser.parseURL("https://jarv.is/feed.xml");
|
||||||
const result = await client.query(
|
const result = await client.query(
|
||||||
q.Map(
|
q.Map(
|
||||||
q.Paginate(q.Documents(q.Collection("hits"))),
|
q.Paginate(q.Documents(q.Collection("hits"))),
|
||||||
@@ -19,14 +24,27 @@ exports.handler = async () => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
const posts = result.data.sort((a, b) => {
|
let posts = result.data;
|
||||||
return a.hits > b.hits ? -1 : 1;
|
|
||||||
});
|
|
||||||
|
|
||||||
posts.forEach((p) => {
|
posts.map((p) => {
|
||||||
|
// match URLs from RSS feed with db to populate some metadata
|
||||||
|
let match = feed.items.find((x) => x.link === "https://jarv.is/" + p.slug + "/");
|
||||||
|
if (match) {
|
||||||
|
p.title = match.title;
|
||||||
|
p.url = match.link;
|
||||||
|
p.date = match.isoDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
// add comma-separated numbers and proper pluralization on the backend
|
||||||
p.pretty_hits = numeral(p.hits).format("0,0");
|
p.pretty_hits = numeral(p.hits).format("0,0");
|
||||||
p.pretty_unit = pluralize("hit", p.hits);
|
p.pretty_unit = pluralize("hit", p.hits);
|
||||||
p.url = "https://jarv.is/" + p.slug + "/";
|
|
||||||
|
return p;
|
||||||
|
});
|
||||||
|
|
||||||
|
// sort by hits (descending)
|
||||||
|
posts.sort((a, b) => {
|
||||||
|
return a.hits > b.hits ? -1 : 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -43,6 +43,7 @@
|
|||||||
"modern-normalize": "1.1.0",
|
"modern-normalize": "1.1.0",
|
||||||
"numeral": "^2.0.6",
|
"numeral": "^2.0.6",
|
||||||
"pluralize": "^8.0.0",
|
"pluralize": "^8.0.0",
|
||||||
|
"rss-parser": "^3.12.0",
|
||||||
"twemoji": "13.1.0",
|
"twemoji": "13.1.0",
|
||||||
"twemoji-emojis": "13.1.0"
|
"twemoji-emojis": "13.1.0"
|
||||||
},
|
},
|
||||||
|
|||||||
25
yarn.lock
25
yarn.lock
@@ -2973,7 +2973,7 @@ entities@^1.1.1:
|
|||||||
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
|
resolved "https://registry.yarnpkg.com/entities/-/entities-1.1.2.tgz#bdfa735299664dfafd34529ed4f8522a275fea56"
|
||||||
integrity sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==
|
integrity sha512-f2LZMYl1Fzu7YSBKg+RoROelpOaNrcGmE9AZubeDfrCEia483oW4MI4VyFd5VNHIgQ/7qm1I0wUHK1eJnn2y2w==
|
||||||
|
|
||||||
entities@^2.0.0:
|
entities@^2.0.0, entities@^2.0.3:
|
||||||
version "2.2.0"
|
version "2.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
|
resolved "https://registry.yarnpkg.com/entities/-/entities-2.2.0.tgz#098dc90ebb83d8dffa089d55256b351d34c4da55"
|
||||||
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==
|
integrity sha512-p92if5Nz619I0w+akJrLZH0MX0Pb5DX39XOwQTtXSdQQOaYH03S1uIQp4mhOZtAXrxq4ViO67YTiLBo2638o9A==
|
||||||
@@ -7206,6 +7206,14 @@ ripemd160@^2.0.0, ripemd160@^2.0.1:
|
|||||||
hash-base "^3.0.0"
|
hash-base "^3.0.0"
|
||||||
inherits "^2.0.1"
|
inherits "^2.0.1"
|
||||||
|
|
||||||
|
rss-parser@^3.12.0:
|
||||||
|
version "3.12.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/rss-parser/-/rss-parser-3.12.0.tgz#b8888699ea46304a74363fbd8144671b2997984c"
|
||||||
|
integrity sha512-aqD3E8iavcCdkhVxNDIdg1nkBI17jgqF+9OqPS1orwNaOgySdpvq6B+DoONLhzjzwV8mWg37sb60e4bmLK117A==
|
||||||
|
dependencies:
|
||||||
|
entities "^2.0.3"
|
||||||
|
xml2js "^0.4.19"
|
||||||
|
|
||||||
run-parallel@^1.1.9:
|
run-parallel@^1.1.9:
|
||||||
version "1.2.0"
|
version "1.2.0"
|
||||||
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.2.0.tgz#66d1368da7bdf921eb9d95bd1a9229e7f21a43ee"
|
resolved "https://registry.yarnpkg.com/run-parallel/-/run-parallel-1.2.0.tgz#66d1368da7bdf921eb9d95bd1a9229e7f21a43ee"
|
||||||
@@ -7249,7 +7257,7 @@ safe-regex@^1.1.0:
|
|||||||
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
|
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
|
||||||
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
|
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==
|
||||||
|
|
||||||
sax@~1.2.4:
|
sax@>=0.6.0, sax@~1.2.4:
|
||||||
version "1.2.4"
|
version "1.2.4"
|
||||||
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
|
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
|
||||||
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
|
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
|
||||||
@@ -8733,6 +8741,19 @@ write-file-atomic@^3.0.3:
|
|||||||
signal-exit "^3.0.2"
|
signal-exit "^3.0.2"
|
||||||
typedarray-to-buffer "^3.1.5"
|
typedarray-to-buffer "^3.1.5"
|
||||||
|
|
||||||
|
xml2js@^0.4.19:
|
||||||
|
version "0.4.23"
|
||||||
|
resolved "https://registry.yarnpkg.com/xml2js/-/xml2js-0.4.23.tgz#a0c69516752421eb2ac758ee4d4ccf58843eac66"
|
||||||
|
integrity sha512-ySPiMjM0+pLDftHgXY4By0uswI3SPKLDw/i3UXbnO8M/p28zqexCUoPmQFrYD+/1BzhGJSs2i1ERWKJAtiLrug==
|
||||||
|
dependencies:
|
||||||
|
sax ">=0.6.0"
|
||||||
|
xmlbuilder "~11.0.0"
|
||||||
|
|
||||||
|
xmlbuilder@~11.0.0:
|
||||||
|
version "11.0.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/xmlbuilder/-/xmlbuilder-11.0.1.tgz#be9bae1c8a046e76b31127726347d0ad7002beb3"
|
||||||
|
integrity sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==
|
||||||
|
|
||||||
"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0, xtend@~4.0.1:
|
"xtend@>=4.0.0 <4.1.0-0", xtend@^4.0.0, xtend@~4.0.1:
|
||||||
version "4.0.2"
|
version "4.0.2"
|
||||||
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
|
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54"
|
||||||
|
|||||||
Reference in New Issue
Block a user