feat: Create the Letterboxd Following Diary endpoint. (#3858)

This commit is contained in:
Logan Rockmore
2020-02-01 22:02:15 -08:00
committed by GitHub
parent 70d40284c9
commit 80f1819316
5 changed files with 56 additions and 4 deletions

View File

@@ -79,6 +79,10 @@ Provides a better reading experience (full text articles) over the official one.
<RouteEn author="loganrockmore" example="/letterboxd/user/diary/demiadejuyigbe" path="/letterboxd/user/diary/:username" :paramsDesc="['username']" />
### Following diary
<RouteEn author="loganrockmore" example="/letterboxd/user/followingdiary/demiadejuyigbe" path="/letterboxd/user/followingdiary/:username" :paramsDesc="['username']" />
## Nautilus
### Topics

View File

@@ -2177,6 +2177,7 @@ router.get('/mqube/top', require('./routes/mqube/top'));
// Letterboxd
router.get('/letterboxd/user/diary/:username', require('./routes/letterboxd/userdiary'));
router.get('/letterboxd/user/followingdiary/:username', require('./routes/letterboxd/followingdiary'));
// 网易大神
router.get('/netease/ds/:id', require('./routes/netease/ds'));

View File

@@ -0,0 +1,8 @@
const utils = require('./utils');
module.exports = async (ctx) => {
const url = `https://letterboxd.com/${ctx.params.username}/following/`;
const title = `Letterboxd - following diary - ${ctx.params.username}`;
ctx.state.data = await utils.getFollowingData(ctx, ctx.params.username, url, title);
};

View File

@@ -4,5 +4,5 @@ module.exports = async (ctx) => {
const url = `https://letterboxd.com/${ctx.params.username}/films/diary/by/added/`;
const title = `Letterboxd - diary - ${ctx.params.username}`;
ctx.state.data = await utils.getData(ctx, url, title);
ctx.state.data = await utils.getData(ctx, ctx.params.username, url, title);
};

View File

@@ -43,7 +43,7 @@ async function ProcessFeed(list, username, caches) {
const rewatch = $('.td-rewatch.icon-status-off').length <= 0;
const hasReview = $('.td-review.icon-status-off').length <= 0;
let descriptionText = `<b>${filmTitle}</b><br />Watched: ${displayDate}<br />Rating: ${rating}`;
let descriptionText = `<b>${filmTitle}</b><br />watched by ${username}<br />Date: ${displayDate}<br />Rating: ${rating}`;
if (liked) {
descriptionText = descriptionText.concat('<br />Liked');
@@ -75,7 +75,7 @@ async function ProcessFeed(list, username, caches) {
);
}
const getData = async (ctx, url, title) => {
const getData = async (ctx, username, url, title) => {
const response = await got({
method: 'get',
url: url,
@@ -87,7 +87,7 @@ const getData = async (ctx, url, title) => {
const $ = cheerio.load(response.data);
const list = $('.diary-entry-row').get();
const result = await ProcessFeed(list, ctx.params.username, ctx.cache);
const result = await ProcessFeed(list, username, ctx.cache);
return {
title: title,
@@ -97,6 +97,45 @@ const getData = async (ctx, url, title) => {
};
};
const getFollowingData = async (ctx, username, url, title) => {
const response = await got({
method: 'get',
url: url,
headers: {
Referer: url,
},
});
const $ = cheerio.load(response.data);
const list = $('.person-table td.table-person').get();
const users = list.map(function(user) {
const $ = cheerio.load(user);
return $('a.name').attr('href');
});
const usersResult = await Promise.all(
users.map(async (user) => {
const userWithoutSlashes = user.replace(/^\//, '').replace(/\/$/, '');
const url = `https://letterboxd.com/${userWithoutSlashes}/films/diary/by/added/`;
const data = getData(ctx, userWithoutSlashes, url, title);
return data;
})
);
const usersItems = usersResult.map((result) => result.item);
const flattenedItems = [].concat.apply([], usersItems);
return {
title: title,
link: url,
description: $('meta[name="description"]').attr('content'),
item: flattenedItems,
};
};
module.exports = {
getData,
getFollowingData,
};