rsshub
Version:
Make RSS Great Again!
146 lines (143 loc) • 4.91 kB
JavaScript
import "./esm-shims-CzJ_djXG.mjs";
import { t as config } from "./config-C37vj7VH.mjs";
import { t as ViewType } from "./types-D84BRIt4.mjs";
import { t as logger_default } from "./logger-Czu8UMNd.mjs";
import "./parse-date-BrP7mxXf.mjs";
import { t as cache_default } from "./cache-Bo__VnGm.mjs";
import "./render-BQo6B4tL.mjs";
import { t as invalid_parameter_default } from "./invalid-parameter-rr4AgGpp.mjs";
import { t as config_not_found_default } from "./config-not-found-Dyp3RlZZ.mjs";
import { t as renderItems } from "./common-utils-Cjv924V2.mjs";
import { IgApiClient } from "instagram-private-api";
//#region lib/routes/instagram/private-api/utils.ts
const ig = new IgApiClient();
async function login(ig$1, cache) {
if (!config.instagram || !config.instagram.username || !config.instagram.password) throw new config_not_found_default("Instagram RSS is disabled due to the lack of <a href=\"https://docs.rsshub.app/deploy/config#route-specific-configurations\">relevant config</a>");
const LOGIN_CACHE_KEY = "instagram:login";
const { username, password } = config.instagram;
const state = await cache.get(LOGIN_CACHE_KEY);
if (state) ig$1.state.deserialize(state);
else {
ig$1.state.generateDevice(username);
await ig$1.account.login(username, password);
process.nextTick(() => ig$1.simulate.postLoginFlow());
logger_default.debug("Instagram login success.");
}
ig$1.request.end$.subscribe(async () => {
const loginState = await ig$1.state.serialize();
delete loginState.constants;
await cache.set(LOGIN_CACHE_KEY, loginState, 12e5);
});
}
//#endregion
//#region lib/routes/instagram/private-api/index.ts
async function loadContent(category, nameOrId, tryGet) {
let feedTitle, feedLink, feedDescription, feedLogo;
let itemsRaw;
switch (category) {
case "user": {
let userInfo, username, id;
if (Number.isNaN(nameOrId)) {
username = nameOrId;
id = await tryGet(`instagram:getIdByUsername:${username}`, () => ig.user.getIdByUsername(username), 31536e3);
userInfo = await tryGet(`instagram:userInfo:${id}`, () => ig.user.info(id));
} else {
id = nameOrId;
userInfo = await tryGet(`instagram:userInfo:${id}`, () => ig.user.info(id));
username = userInfo.username;
}
feedDescription = userInfo.biography;
feedLogo = userInfo.profile_pic_url_hd ?? userInfo.hd_profile_pic_url_info?.url ?? userInfo.profile_pic_url;
feedTitle = `${userInfo.full_name} (@${username}) - Instagram`;
feedLink = `https://www.instagram.com/${username}`;
itemsRaw = await tryGet(`instagram:feed:${id}`, () => ig.feed.user(id).items(), config.cache.routeExpire, false);
break;
}
case "tags": {
const tag = nameOrId;
feedTitle = `#${tag} - Instagram`;
feedLink = `https://www.instagram.com/explore/tags/${tag}`;
itemsRaw = await tryGet(`instagram:tags:${tag}`, () => ig.feed.tags(tag, "recent").items(), config.cache.routeExpire, false);
break;
}
default: break;
}
return {
feedTitle,
feedLink,
feedDescription,
feedLogo,
itemsRaw
};
}
const route = {
path: "/:category/:key",
categories: ["social-media"],
view: ViewType.SocialMedia,
example: "/instagram/user/stefaniejoosten",
parameters: {
category: {
description: "Feed category",
default: "user",
options: [{
label: "User",
value: "user"
}, {
label: "Tags",
value: "tags"
}]
},
key: "Username / Hashtag name"
},
features: {
requireConfig: [
{
name: "IG_PROXY",
optional: true,
description: ""
},
{
name: "IG_USERNAME",
description: "Instagram username"
},
{
name: "IG_PASSWORD",
description: "Instagram password, due to [Instagram Private API](https://github.com/dilame/instagram-private-api) restrictions, you have to setup your credentials on the server. 2FA is not supported."
}
],
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false
},
name: "User Profile / Hashtag - Private API",
maintainers: ["oppilate", "DIYgod"],
handler
};
async function handler(ctx) {
const availableCategories = ["user", "tags"];
const { category, key } = ctx.req.param();
if (!availableCategories.includes(category)) throw new invalid_parameter_default("Such feed is not supported.");
if (config.instagram && config.instagram.proxy) ig.state.proxyUrl = config.instagram.proxy;
await login(ig, cache_default);
let data;
try {
data = await loadContent(category, key, cache_default.tryGet);
} catch (error) {
logger_default.error(`Instagram error: ${error}`);
throw error;
}
return {
title: data.feedTitle,
link: data.feedLink,
description: data.feedDescription,
item: renderItems(data.itemsRaw),
icon: "https://www.instagram.com/static/images/ico/xxhdpi_launcher.png/99cf3909d459.png",
logo: data.feedLogo,
image: data.feedLogo,
allowEmpty: true
};
}
//#endregion
export { route };