fix: Instagram carousel exception(#6353)

This commit is contained in:
oppilate
2020-12-10 14:17:56 +00:00
committed by GitHub
parent dffd55eac5
commit 02bdf8dadb
2 changed files with 30 additions and 16 deletions
+21 -9
View File
@@ -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(`<p>${item.caption.text}</p>`);
const summary = item.caption ? item.caption.text : '';
const $ = cheerio.load(`<p>${summary}</p>`);
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(`<img src="${i.url}" height="${i.height}" width="${i.width}">`);
const img = i.candidates[0];
content.append(`<img src="${img.url}" height="${img.height}" width="${img.width}">`);
}
// Metadata
const url = path.join('https://www.intagram.com', item.code);
const url = path.join('https://www.intagram.com', 'p', item.code);
const pubDate = new Date(item.taken_at).toUTCString();
const title = item.caption.text.split('\n')[0];
const title = summary.split('\n')[0];
return Promise.resolve({
title,
@@ -60,7 +72,7 @@ module.exports = async (ctx) => {
pubDate,
author: item.creator,
link: url,
summary: item.caption.text,
summary,
description: content.html(),
icon: 'https://www.instagram.com/static/images/ico/xxhdpi_launcher.png/99cf3909d459.png',
logo,
+9 -7
View File
@@ -1,12 +1,12 @@
const { IgApiClient } = require('instagram-private-api');
const logger = require('@/utils/logger');
let ig;
if (process.env.IG_USERNAME && process.env.IG_PASSWORD) {
(async () => {
try {
ig = new IgApiClient();
const ig = new IgApiClient();
async () => await login(ig); // deepscan-disable-line UNUSED_EXPR
async function login(ig) {
if (process.env.IG_USERNAME && process.env.IG_PASSWORD) {
try {
ig.state.generateDevice(process.env.IG_USERNAME);
await ig.simulate.preLoginFlow();
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
@@ -14,7 +14,9 @@ if (process.env.IG_USERNAME && process.env.IG_PASSWORD) {
} catch (error) {
logger.error('Instagram login fail: ' + error);
}
})();
} else {
throw Error('Instagram username and password are required to be set in the environment.');
}
}
module.exports = ig;
module.exports = { ig, login };