fix(route): The New York Times 新闻简报图片 (#10077)

* fix(route): The New York Times 新闻简报图片

* fix: pattern for urls of images
This commit is contained in:
Ethan Shen
2022-06-30 22:34:19 +08:00
committed by GitHub
parent 837b5dfe0d
commit 88cce18124
2 changed files with 3 additions and 23 deletions
+3 -22
View File
@@ -13,7 +13,7 @@ module.exports = async (ctx) => {
url: currentUrl,
});
const listData = JSON.parse(response.data.match(/<script>window\.__preloadedData = {"initialState":(.*),"config"/)[1]);
const listData = JSON.parse(response.data.match(/"initialState":(.*),"config"/)[1]);
let items = [];
Object.keys(listData).forEach((key) => {
@@ -35,36 +35,17 @@ module.exports = async (ctx) => {
});
const content = cheerio.load(detailResponse.data);
const itemData = JSON.parse(detailResponse.data.match(/<script>window\.__preloadedData = {"initialState":(.*),"config"/)[1]);
// to remove ads
content('.StoryBodyCompanionColumn').last().find('p').last().remove();
const images = [];
Object.keys(itemData).forEach((key) => {
if (/^Image:/.test(key) && itemData[key].__typename === 'Image') {
let cropKey = '';
const image = itemData[key];
Object.keys(image).forEach((c) => {
if (/^crops/.test(c)) {
cropKey = c;
return;
}
});
images.push({
caption: image.legacyHtmlCaption,
url: itemData[itemData[image[cropKey][0].id].renditions[0].id].url,
});
}
});
const images = detailResponse.data.match(/"url":"[^{}]+","name":"articleLarge"/g).map((e) => JSON.parse(`{${e}}`).url);
let i = 0;
content('figure').each(function () {
content(this).html(
art(path.join(__dirname, 'templates/image.art'), {
url: images[i].url,
caption: images[i++].caption,
url: images[i++],
})
);
});
-1
View File
@@ -1,4 +1,3 @@
<figure>
<img src="{{ url }}">
<figcaption>{{ caption }}</figcaption>
</figure>