From 02bdf8dadbd503d6f846adf7e931d691a74ef7d8 Mon Sep 17 00:00:00 2001 From: oppilate <64395752+oppilate@users.noreply.github.com> Date: Thu, 10 Dec 2020 14:17:56 +0000 Subject: [PATCH] fix: Instagram carousel exception(#6353) --- lib/routes/instagram/index.js | 30 +++++++++++++++++++++--------- lib/routes/instagram/utils.js | 16 +++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/routes/instagram/index.js b/lib/routes/instagram/index.js index 47a2a66c6..bfd2354ca 100644 --- a/lib/routes/instagram/index.js +++ b/lib/routes/instagram/index.js @@ -1,6 +1,7 @@ const cheerio = require('cheerio'); const path = require('path'); -const ig = require('./utils'); +const { IgLoginRequiredError } = require('instagram-private-api'); +const { ig, login } = require('./utils'); module.exports = async (ctx) => { // https://github.com/dilame/instagram-private-api#feeds @@ -13,8 +14,15 @@ module.exports = async (ctx) => { if (!availableCategories.includes(category)) { throw Error('Such feed is not supported.'); } - if (process.env.IG_USERNAME === undefined || process.env.IG_PASSWORD === undefined) { - throw Error('Instagram username and password are required to be set in the environment.'); + + try { + await ig.account.currentUser(); + } catch (e) { + if (e instanceof IgLoginRequiredError) { + await login(ig); + } else { + throw Error('Unknown error'); + } } // Unique key for a feed category @@ -42,17 +50,21 @@ module.exports = async (ctx) => { const items = await Promise.all( itemsPromise.map(async (item) => { // Content - const $ = cheerio.load(`
${item.caption.text}
`); + const summary = item.caption ? item.caption.text : ''; + const $ = cheerio.load(`${summary}
`); const content = $.root(); - const images = item.image_versions2.candidates; + + // Image or carousel media + const images = item.image_versions2 ? [item.image_versions2] : item.carousel_media.map((i) => i.image_versions2); for (const i of images) { - content.append(`