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(`
`);
+ const img = i.candidates[0];
+ content.append(`
`);
}
// 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,
diff --git a/lib/routes/instagram/utils.js b/lib/routes/instagram/utils.js
index da9b4b1f9..f14dbdf73 100644
--- a/lib/routes/instagram/utils.js
+++ b/lib/routes/instagram/utils.js
@@ -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 };