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
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const config: {
|
|
2
|
+
/** Instagram Base URL */
|
|
3
|
+
instagram_base_url: string;
|
|
4
|
+
instagram_base_z4: string;
|
|
5
|
+
/** Instagram Api v1 */
|
|
6
|
+
instagram_api_v1: string;
|
|
7
|
+
/** Instagram API Search User */
|
|
8
|
+
instagram_search_url: string;
|
|
9
|
+
/** Android User-Agent */
|
|
10
|
+
android: string;
|
|
11
|
+
/** Desktop User-Agent */
|
|
12
|
+
desktop: string;
|
|
13
|
+
/** iPhone User-Agent */
|
|
14
|
+
iPhone: string;
|
|
15
|
+
loginHeader: string;
|
|
16
|
+
};
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const config = {
|
|
2
|
+
/** Instagram Base URL */
|
|
3
|
+
instagram_base_url: 'https://www.instagram.com',
|
|
4
|
+
instagram_base_z4: 'https://z-p4.www.instagram.com',
|
|
5
|
+
/** Instagram Api v1 */
|
|
6
|
+
instagram_api_v1: 'https://i.instagram.com/api/v1',
|
|
7
|
+
/** Instagram API Search User */
|
|
8
|
+
instagram_search_url: 'https://www.instagram.com/web/search/topsearch/?query=',
|
|
9
|
+
/** Android User-Agent */
|
|
10
|
+
android: 'Instagram 177.0.0.30.119 Android (18/4.3; 320dpi; 720x1280; Xiaomi; HM 1SW; armani; qcom; en_US)',
|
|
11
|
+
/** Desktop User-Agent */
|
|
12
|
+
desktop: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36',
|
|
13
|
+
/** iPhone User-Agent */
|
|
14
|
+
iPhone: 'Instagram 123.0.0.21.114 (iPhone; CPU iPhone OS 18_5 like Mac OS X; en_US; en-US; scale=2.00; 750x1334) AppleWebKit/605.1.15',
|
|
15
|
+
loginHeader: 'Instagram 100.1.0.29.135 Android'
|
|
16
|
+
};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function getCookie(username: string, password: string, withLoginData?: boolean): Promise<import("./types/LoginData.js").LoginData | {
|
|
2
|
+
cookie: string;
|
|
3
|
+
expires: string;
|
|
4
|
+
}>;
|
|
5
|
+
export { InstaService } from './lib/client.service.js';
|
|
6
|
+
export { LoginService } from './lib/login.service.js';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { getCookies } from './lib/handler.js';
|
|
2
|
+
export function getCookie(username, password, withLoginData = false) {
|
|
3
|
+
return getCookies(username, password, withLoginData);
|
|
4
|
+
}
|
|
5
|
+
export { InstaService } from './lib/client.service.js';
|
|
6
|
+
export { LoginService } from './lib/login.service.js';
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { Friendship, IPostModels, LikeResponse, StoryUser, UserGraphQlV2 } from '../types/index.js';
|
|
2
|
+
import { LoginService } from './login.service.js';
|
|
3
|
+
export declare class InstaService {
|
|
4
|
+
private cookie;
|
|
5
|
+
loginService: LoginService;
|
|
6
|
+
/**
|
|
7
|
+
* Recommended to set cookie for most all IG Request
|
|
8
|
+
* @param cookie cookie you can get it by using getCookie function
|
|
9
|
+
*/
|
|
10
|
+
constructor(cookie?: string);
|
|
11
|
+
/**
|
|
12
|
+
* fetches stories metadata
|
|
13
|
+
* @param {string} username username target to fetch the highlights, also work with private profile if you use cookie \w your account that follows target account
|
|
14
|
+
* @param headers - request headers
|
|
15
|
+
* @returns
|
|
16
|
+
*/
|
|
17
|
+
getHighlights(username: string, headers?: {
|
|
18
|
+
[key: string]: string;
|
|
19
|
+
}): Promise<any>;
|
|
20
|
+
/**
|
|
21
|
+
* fetches stories metadata
|
|
22
|
+
* @param {string} username username target to fetch the stories, also work with private profile if you use cookie \w your account that follows target account
|
|
23
|
+
* @param {boolean} exportDetails instagram response body
|
|
24
|
+
* @param headers - request headers
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
getStories(username: string, exportDetails?: boolean, headers?: {
|
|
28
|
+
[key: string]: string;
|
|
29
|
+
}): Promise<any>;
|
|
30
|
+
/**
|
|
31
|
+
* get user id by username
|
|
32
|
+
* @param username
|
|
33
|
+
* @param headers
|
|
34
|
+
* @returns
|
|
35
|
+
*/
|
|
36
|
+
getUIdByUsername(username: string, headers?: {
|
|
37
|
+
[key: string]: string;
|
|
38
|
+
}): Promise<string>;
|
|
39
|
+
/**
|
|
40
|
+
* hmmm..?
|
|
41
|
+
* @param username
|
|
42
|
+
* @param headers
|
|
43
|
+
* @returns
|
|
44
|
+
*/
|
|
45
|
+
getUserDetails(username: string, headers?: {
|
|
46
|
+
[key: string]: string;
|
|
47
|
+
}): Promise<UserGraphQlV2>;
|
|
48
|
+
/**
|
|
49
|
+
* fetch profile by username. including email, phone number and posts
|
|
50
|
+
* @param {username} username
|
|
51
|
+
* @param maxId
|
|
52
|
+
* @param headers
|
|
53
|
+
* @returns
|
|
54
|
+
*/
|
|
55
|
+
fetchUserProfilePosts(username: string, maxId?: string, headers?: {
|
|
56
|
+
[key: string]: string;
|
|
57
|
+
}): Promise<any>;
|
|
58
|
+
/**
|
|
59
|
+
* fetch profile by id. including email, phone number and posts
|
|
60
|
+
* @param {string} userID
|
|
61
|
+
* @param maxId
|
|
62
|
+
* @param headers
|
|
63
|
+
* @returns
|
|
64
|
+
*/
|
|
65
|
+
fetchUserPostsByUserId(userID: string, maxId?: string, headers?: {
|
|
66
|
+
[key: string]: string;
|
|
67
|
+
}): Promise<any>;
|
|
68
|
+
/**
|
|
69
|
+
* fetches post, reel,.. by url
|
|
70
|
+
* @param url
|
|
71
|
+
* @param {string} headers @optional - required for token authentication
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
fetchContentByUrl(url: string, headers?: {
|
|
75
|
+
[key: string]: string;
|
|
76
|
+
}): Promise<IPostModels>;
|
|
77
|
+
/**
|
|
78
|
+
* fetches post, reel,.. by url
|
|
79
|
+
* @param shortCode
|
|
80
|
+
* @param {string} headers @optional - required for token authentication
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
fetchContentByShortCode(shortCode: string, headers?: {
|
|
84
|
+
[key: string]: string;
|
|
85
|
+
}): Promise<IPostModels>;
|
|
86
|
+
/**
|
|
87
|
+
* fetches post, reel,.. by mediaId
|
|
88
|
+
* @param mediaId
|
|
89
|
+
* @param {string} headers @optional - required for token authentication
|
|
90
|
+
* @returns
|
|
91
|
+
*/
|
|
92
|
+
private fetchByMediaId;
|
|
93
|
+
/**
|
|
94
|
+
* fetches post, reel,.. by mediaId
|
|
95
|
+
* @param mediaId
|
|
96
|
+
* @param {string} headers @optional - required for token authentication
|
|
97
|
+
* @returns
|
|
98
|
+
*/
|
|
99
|
+
fetchContentByMediaId(mediaId: string | number, headers?: {
|
|
100
|
+
[key: string]: string;
|
|
101
|
+
}): Promise<any>;
|
|
102
|
+
private _formatSidecar;
|
|
103
|
+
/**
|
|
104
|
+
* fetches tray stories
|
|
105
|
+
* @param headers
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
fetchTrayStories(headers?: {
|
|
109
|
+
[key: string]: string;
|
|
110
|
+
}): Promise<StoryUser[]>;
|
|
111
|
+
follow(usernameOrId: string, headers?: {
|
|
112
|
+
[key: string]: string;
|
|
113
|
+
}): Promise<Friendship>;
|
|
114
|
+
like(shortCode: string, headers?: {
|
|
115
|
+
[key: string]: string;
|
|
116
|
+
}): Promise<LikeResponse>;
|
|
117
|
+
private fetchAPI;
|
|
118
|
+
private buildHeaders;
|
|
119
|
+
}
|
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import { config } from '../config.js';
|
|
2
|
+
import { ParserService } from './parser.service.js';
|
|
3
|
+
import { MediaType, ProductType, } from '../types/index.js';
|
|
4
|
+
import { getPostType, shortcodeFormatter, shortcodeToMediaID } from '../utils/index.js';
|
|
5
|
+
import { LoginService } from './login.service.js';
|
|
6
|
+
import got from 'got';
|
|
7
|
+
import { LocalStorage } from "node-localstorage";
|
|
8
|
+
const localStorage = new LocalStorage("./scratch");
|
|
9
|
+
export class InstaService {
|
|
10
|
+
/**
|
|
11
|
+
* Recommended to set cookie for most all IG Request
|
|
12
|
+
* @param cookie cookie you can get it by using getCookie function
|
|
13
|
+
*/
|
|
14
|
+
constructor(cookie = '') {
|
|
15
|
+
this.cookie = cookie;
|
|
16
|
+
this.buildHeaders = (options) => {
|
|
17
|
+
const baseHeaders = {
|
|
18
|
+
'User-Agent': config.iPhone,
|
|
19
|
+
'authority': 'www.instagram.com',
|
|
20
|
+
'content-type': 'application/x-www-form-urlencoded',
|
|
21
|
+
'origin': 'https://www.instagram.com',
|
|
22
|
+
'accept-language': 'id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7',
|
|
23
|
+
'sec-fetch-site': 'same-origin',
|
|
24
|
+
'sec-fetch-mode': 'cors',
|
|
25
|
+
'sec-fetch-dest': 'empty',
|
|
26
|
+
'x-ig-app-id': 936619743392459,
|
|
27
|
+
'x-ig-www-claim': 'hmac.AR3W0DThY2Mu5Fag4sW5u3RhaR3qhFD_5wvYbOJOD9qaPjIf',
|
|
28
|
+
'x-instagram-ajax': 1,
|
|
29
|
+
'x-requested-with': 'XMLHttpRequest',
|
|
30
|
+
};
|
|
31
|
+
// Merge mit options und zwinge alle Werte zu Strings
|
|
32
|
+
const merged = { ...baseHeaders, ...options };
|
|
33
|
+
Object.keys(merged).forEach(key => {
|
|
34
|
+
merged[key] = String(merged[key]);
|
|
35
|
+
});
|
|
36
|
+
return merged;
|
|
37
|
+
};
|
|
38
|
+
this.cookie = cookie;
|
|
39
|
+
this.loginService = new LoginService();
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* fetches stories metadata
|
|
43
|
+
* @param {string} username username target to fetch the highlights, also work with private profile if you use cookie \w your account that follows target account
|
|
44
|
+
* @param headers - request headers
|
|
45
|
+
* @returns
|
|
46
|
+
*/
|
|
47
|
+
async getHighlights(username, headers = {}) {
|
|
48
|
+
const userID = await this.getUIdByUsername(username, headers);
|
|
49
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/highlights/${userID}/highlights_tray/`, { headers }, { b: username, type: "h" });
|
|
50
|
+
return res?.data || {};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* fetches stories metadata
|
|
54
|
+
* @param {string} username username target to fetch the stories, also work with private profile if you use cookie \w your account that follows target account
|
|
55
|
+
* @param {boolean} exportDetails instagram response body
|
|
56
|
+
* @param headers - request headers
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
async getStories(username, exportDetails = false, headers = {}) {
|
|
60
|
+
const parserService = new ParserService();
|
|
61
|
+
const userID = await this.getUIdByUsername(username, headers);
|
|
62
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/feed/user/${userID}/reel_media/`, { headers }, { b: username, type: "s" });
|
|
63
|
+
const graphql = res?.data;
|
|
64
|
+
const isFollowing = typeof graphql.user?.friendship_status !== 'undefined';
|
|
65
|
+
if (!isFollowing && graphql.user.is_private) {
|
|
66
|
+
throw new Error('private profile');
|
|
67
|
+
}
|
|
68
|
+
const response = {
|
|
69
|
+
id: graphql.id,
|
|
70
|
+
username: graphql.user.username,
|
|
71
|
+
name: graphql.user.full_name,
|
|
72
|
+
profilePicture: graphql.user.profile_pic_url,
|
|
73
|
+
stories_count: graphql.media_count,
|
|
74
|
+
media: graphql.items.length === 0 ? [] : parserService.parseStories(graphql),
|
|
75
|
+
};
|
|
76
|
+
if (exportDetails) {
|
|
77
|
+
response.graphql = graphql;
|
|
78
|
+
}
|
|
79
|
+
return response;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* get user id by username
|
|
83
|
+
* @param username
|
|
84
|
+
* @param headers
|
|
85
|
+
* @returns
|
|
86
|
+
*/
|
|
87
|
+
async getUIdByUsername(username, headers = {}) {
|
|
88
|
+
const res = await this.getUserDetails(username, headers);
|
|
89
|
+
return res?.id;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* hmmm..?
|
|
93
|
+
* @param username
|
|
94
|
+
* @param headers
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
async getUserDetails(username, headers = {}) {
|
|
98
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/users/web_profile_info/?username=${username}`, { headers }, { b: username, type: "p" });
|
|
99
|
+
const graphql = res?.data;
|
|
100
|
+
return graphql.data?.user;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* fetch profile by username. including email, phone number and posts
|
|
104
|
+
* @param {username} username
|
|
105
|
+
* @param maxId
|
|
106
|
+
* @param headers
|
|
107
|
+
* @returns
|
|
108
|
+
*/
|
|
109
|
+
async fetchUserProfilePosts(username, maxId = '', headers = {}) {
|
|
110
|
+
const userID = await this.getUIdByUsername(username, headers);
|
|
111
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/feed/user/${userID}/?max_id=${maxId}`, { headers }, { b: username, type: "p" });
|
|
112
|
+
const graphql = res?.data;
|
|
113
|
+
const parserSvc = new ParserService();
|
|
114
|
+
return {
|
|
115
|
+
id: graphql.user.pk,
|
|
116
|
+
username: graphql.user.username,
|
|
117
|
+
name: graphql.user.full_name,
|
|
118
|
+
is_private: graphql.user.is_private,
|
|
119
|
+
is_verified: graphql.user.is_verified,
|
|
120
|
+
profile_pic_url: graphql.user.profile_pic_url,
|
|
121
|
+
more_available: graphql.more_available,
|
|
122
|
+
next_max_id: graphql.next_max_id,
|
|
123
|
+
media: parserSvc.parseInstagramFeedItems(graphql.items)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* fetch profile by id. including email, phone number and posts
|
|
128
|
+
* @param {string} userID
|
|
129
|
+
* @param maxId
|
|
130
|
+
* @param headers
|
|
131
|
+
* @returns
|
|
132
|
+
*/
|
|
133
|
+
async fetchUserPostsByUserId(userID, maxId = '', headers = {}) {
|
|
134
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/feed/user/${userID}/?max_id=${maxId}`, { headers }, { b: userID, type: "p" });
|
|
135
|
+
const graphql = res?.data;
|
|
136
|
+
const parserSvc = new ParserService();
|
|
137
|
+
return {
|
|
138
|
+
id: graphql.user.pk,
|
|
139
|
+
username: graphql.user.username,
|
|
140
|
+
name: graphql.user.full_name,
|
|
141
|
+
is_private: graphql.user.is_private,
|
|
142
|
+
is_verified: graphql.user.is_verified,
|
|
143
|
+
profile_pic_url: graphql.user.profile_pic_url,
|
|
144
|
+
more_available: graphql.more_available,
|
|
145
|
+
next_max_id: graphql.next_max_id,
|
|
146
|
+
media: parserSvc.parseInstagramFeedItems(graphql.items)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* fetches post, reel,.. by url
|
|
151
|
+
* @param url
|
|
152
|
+
* @param {string} headers @optional - required for token authentication
|
|
153
|
+
* @returns
|
|
154
|
+
*/
|
|
155
|
+
async fetchContentByUrl(url, headers = {}) {
|
|
156
|
+
const post = shortcodeFormatter(url);
|
|
157
|
+
const metadata = await this.fetchByMediaId(post.media_id, headers);
|
|
158
|
+
const item = metadata?.items?.[0];
|
|
159
|
+
if (!item || !item.user) { // @ts-ignore
|
|
160
|
+
return {};
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
username: item.user?.username || '',
|
|
164
|
+
name: item.user?.full_name || '',
|
|
165
|
+
postType: getPostType(item.product_type),
|
|
166
|
+
media_id: item.id || '',
|
|
167
|
+
shortcode: item.code || '',
|
|
168
|
+
createdAt: item.taken_at || 0,
|
|
169
|
+
likes: item.like_count ?? 0,
|
|
170
|
+
caption: item.caption?.text || '',
|
|
171
|
+
media_count: item.product_type === ProductType.CAROUSEL ? item.carousel_media_count ?? 0 : 1,
|
|
172
|
+
comment_count: item.comment_count ?? 0,
|
|
173
|
+
video_duration: item.video_duration ?? 0,
|
|
174
|
+
music: item.clips_metadata ?? {}, // oder {} wenn Struktur erwartet wird
|
|
175
|
+
media: this._formatSidecar(metadata) ?? [],
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* fetches post, reel,.. by url
|
|
180
|
+
* @param shortCode
|
|
181
|
+
* @param {string} headers @optional - required for token authentication
|
|
182
|
+
* @returns
|
|
183
|
+
*/
|
|
184
|
+
async fetchContentByShortCode(shortCode, headers = {}) {
|
|
185
|
+
const mediaId = shortcodeToMediaID(shortCode);
|
|
186
|
+
const metadata = await this.fetchByMediaId(mediaId, headers);
|
|
187
|
+
const item = metadata?.items?.[0];
|
|
188
|
+
if (!item || !item.user) { // @ts-ignore
|
|
189
|
+
return {};
|
|
190
|
+
} // Oder {} oder Error-Throw, je nach Anwendungsfall
|
|
191
|
+
return {
|
|
192
|
+
username: item.user.username || '',
|
|
193
|
+
name: item.user.full_name || '',
|
|
194
|
+
postType: getPostType(item.product_type),
|
|
195
|
+
media_id: item.id || '',
|
|
196
|
+
shortcode: item.code || '',
|
|
197
|
+
createdAt: item.taken_at || 0,
|
|
198
|
+
likes: item.like_count ?? 0,
|
|
199
|
+
caption: item.caption?.text || '',
|
|
200
|
+
media_count: item.product_type === ProductType.CAROUSEL
|
|
201
|
+
? item.carousel_media_count ?? 0
|
|
202
|
+
: 1,
|
|
203
|
+
comment_count: item.comment_count ?? 0,
|
|
204
|
+
video_duration: item.video_duration ?? 0,
|
|
205
|
+
music: item.clips_metadata ?? {},
|
|
206
|
+
media: this._formatSidecar(metadata) ?? [],
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* fetches post, reel,.. by mediaId
|
|
211
|
+
* @param mediaId
|
|
212
|
+
* @param {string} headers @optional - required for token authentication
|
|
213
|
+
* @returns
|
|
214
|
+
*/
|
|
215
|
+
async fetchByMediaId(mediaId, headers = {}) {
|
|
216
|
+
try {
|
|
217
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/media/${mediaId.toString()}/info/`, { headers });
|
|
218
|
+
return res?.data;
|
|
219
|
+
}
|
|
220
|
+
catch (error) {
|
|
221
|
+
throw error;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* fetches post, reel,.. by mediaId
|
|
226
|
+
* @param mediaId
|
|
227
|
+
* @param {string} headers @optional - required for token authentication
|
|
228
|
+
* @returns
|
|
229
|
+
*/
|
|
230
|
+
async fetchContentByMediaId(mediaId, headers = {}) {
|
|
231
|
+
try {
|
|
232
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/media/${mediaId.toString()}/info/`, { headers });
|
|
233
|
+
const items = res?.data?.items || [];
|
|
234
|
+
const parserSvc = new ParserService();
|
|
235
|
+
const itemsWithExtras = parserSvc.parseInstagramFeedItems(items);
|
|
236
|
+
const item = res?.data?.items?.[0];
|
|
237
|
+
if (!item || !item.user) { // @ts-ignore
|
|
238
|
+
return {};
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
username: item.user.username || '',
|
|
242
|
+
name: item.user.full_name || '',
|
|
243
|
+
postType: getPostType(item.product_type),
|
|
244
|
+
media_id: item.id || '',
|
|
245
|
+
shortcode: item.code || '',
|
|
246
|
+
createdAt: item.taken_at || 0,
|
|
247
|
+
media: itemsWithExtras,
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
throw error;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
_formatSidecar(data) {
|
|
255
|
+
const gql = data.items?.[0];
|
|
256
|
+
const urls = [];
|
|
257
|
+
if (!gql)
|
|
258
|
+
return urls;
|
|
259
|
+
const parserSvc = new ParserService();
|
|
260
|
+
if (gql.product_type === ProductType.CAROUSEL) {
|
|
261
|
+
gql.carousel_media?.forEach((v) => {
|
|
262
|
+
const img = parserSvc.safeCandidate(v);
|
|
263
|
+
const vid = parserSvc.safeVideo(v);
|
|
264
|
+
urls.push({
|
|
265
|
+
id: v.id || '',
|
|
266
|
+
thumbnail: img.url || '',
|
|
267
|
+
url: v.media_type === MediaType.IMAGE ? img.url || '' : vid.url || '',
|
|
268
|
+
type: v.media_type === MediaType.IMAGE ? 'image' : 'video',
|
|
269
|
+
dimensions: {
|
|
270
|
+
height: v.media_type === MediaType.IMAGE ? img.height || 0 : vid.height || 0,
|
|
271
|
+
width: v.media_type === MediaType.IMAGE ? img.width || 0 : vid.width || 0
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
else if ([ProductType.REEL, ProductType.TV, ProductType.SINGLE].includes(gql.product_type)) {
|
|
277
|
+
const img = parserSvc.safeCandidate(gql);
|
|
278
|
+
const vid = parserSvc.safeVideo(gql);
|
|
279
|
+
urls.push({
|
|
280
|
+
id: gql.id || '',
|
|
281
|
+
thumbnail: img.url || '',
|
|
282
|
+
url: gql.media_type === MediaType.IMAGE ? img.url || '' : vid.url || '',
|
|
283
|
+
type: gql.media_type === MediaType.IMAGE ? 'image' : 'video',
|
|
284
|
+
dimensions: {
|
|
285
|
+
height: gql.media_type === MediaType.IMAGE ? img.height || 0 : vid.height || 0,
|
|
286
|
+
width: gql.media_type === MediaType.IMAGE ? img.width || 0 : vid.width || 0
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
return urls;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* fetches tray stories
|
|
294
|
+
* @param headers
|
|
295
|
+
* @returns
|
|
296
|
+
*/
|
|
297
|
+
async fetchTrayStories(headers = {}) {
|
|
298
|
+
try {
|
|
299
|
+
const params = {
|
|
300
|
+
"supported_capabilities_new": `[{"name":"SUPPORTED_SDK_VERSIONS","value":"100.0,101.0,102.0,103.0,104.0,105.0,106.0,107.0,108.0,109.0,110.0,111.0,112.0,113.0,114.0,115.0,116.0,117.0"},{"name":"FACE_TRACKER_VERSION","value":"14"},{"name":"segmentation","value":"segmentation_enabled"},{"name":"COMPRESSION","value":"ETC2_COMPRESSION"},{"name":"world_tracker","value":"world_tracker_enabled"},{"name":"gyroscope","value":"gyroscope_enabled"}]`,
|
|
301
|
+
"reason": "cold_start_fetch"
|
|
302
|
+
};
|
|
303
|
+
const res = await this.fetchAPI(config.instagram_api_v1, `/feed/reels_tray/`, { params, headers });
|
|
304
|
+
const tray = res?.data?.tray;
|
|
305
|
+
if (!Array.isArray(tray)) {
|
|
306
|
+
console.warn('Tray data is missing or not an array');
|
|
307
|
+
return [];
|
|
308
|
+
}
|
|
309
|
+
const mappedStories = tray
|
|
310
|
+
.filter(story => story?.user) // sicherstellen, dass "user" existiert
|
|
311
|
+
.map((story) => ({
|
|
312
|
+
id: story.id?.toString() || '',
|
|
313
|
+
full_name: story.user?.full_name || '',
|
|
314
|
+
username: story.user?.username || '',
|
|
315
|
+
story_duration_secs: Number(story.story_duration_secs || 0),
|
|
316
|
+
media_count: Number(story.media_count || 0),
|
|
317
|
+
has_video: Boolean(story.has_video),
|
|
318
|
+
profile_pic_url: story.user?.profile_pic_url || '',
|
|
319
|
+
is_verified: Boolean(story.user?.is_verified),
|
|
320
|
+
is_private: Boolean(story.user?.is_private)
|
|
321
|
+
}));
|
|
322
|
+
return mappedStories;
|
|
323
|
+
}
|
|
324
|
+
catch (error) {
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
async follow(usernameOrId, headers = {}) {
|
|
329
|
+
try {
|
|
330
|
+
let resolvedUserId;
|
|
331
|
+
// Prüfen ob direkt eine userId übergeben wurde (besteht nur aus Ziffern)
|
|
332
|
+
if (/^\d+$/.test(usernameOrId)) {
|
|
333
|
+
resolvedUserId = usernameOrId;
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
// sonst als username behandeln und ID auflösen
|
|
337
|
+
resolvedUserId = await this.getUIdByUsername(usernameOrId, headers);
|
|
338
|
+
}
|
|
339
|
+
// 2. Try to get _uid from localStorage, fallback to instaUserName -> getUIdByUsername
|
|
340
|
+
let storedId = localStorage.getItem("instaUserId");
|
|
341
|
+
if (!storedId) {
|
|
342
|
+
const localName = localStorage.getItem("instaUserName");
|
|
343
|
+
if (localName) {
|
|
344
|
+
storedId = await this.getUIdByUsername(localName);
|
|
345
|
+
if (storedId) {
|
|
346
|
+
localStorage.setItem("instaUserId", storedId);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (!storedId) {
|
|
351
|
+
throw new Error("❌ instaUserId not found and could not be resolved");
|
|
352
|
+
}
|
|
353
|
+
const data = {
|
|
354
|
+
user_id: resolvedUserId,
|
|
355
|
+
radio_type: "wifi-none",
|
|
356
|
+
_uid: storedId,
|
|
357
|
+
device_id: this.loginService.deviceId,
|
|
358
|
+
_uuid: this.loginService.uuid,
|
|
359
|
+
};
|
|
360
|
+
const signed = this.loginService.generateSignature(JSON.stringify(data));
|
|
361
|
+
const endpoint = `/friendships/create/${resolvedUserId}/`;
|
|
362
|
+
const res = await this.fetchAPI(config.instagram_api_v1, endpoint, {
|
|
363
|
+
headers,
|
|
364
|
+
method: "POST",
|
|
365
|
+
data: signed,
|
|
366
|
+
});
|
|
367
|
+
const body = res.data;
|
|
368
|
+
if (!body || body.status !== "ok") {
|
|
369
|
+
throw new Error(`❌ Follow failed: ${JSON.stringify(body)}`);
|
|
370
|
+
}
|
|
371
|
+
return body.friendship_status;
|
|
372
|
+
}
|
|
373
|
+
catch (err) {
|
|
374
|
+
console.error("❌ Error in follow():", err);
|
|
375
|
+
throw err;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async like(shortCode, headers = {}) {
|
|
379
|
+
try {
|
|
380
|
+
const mediaId = shortcodeToMediaID(shortCode);
|
|
381
|
+
// Eigene Account-ID holen
|
|
382
|
+
let storedId = localStorage.getItem("instaUserId");
|
|
383
|
+
if (!storedId) {
|
|
384
|
+
const localName = localStorage.getItem("instaUserName");
|
|
385
|
+
if (localName) {
|
|
386
|
+
storedId = await this.getUIdByUsername(localName, headers);
|
|
387
|
+
if (storedId) {
|
|
388
|
+
localStorage.setItem("instaUserId", storedId);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (!storedId) {
|
|
393
|
+
throw new Error("❌ instaUserId not found and could not be resolved");
|
|
394
|
+
}
|
|
395
|
+
// Query zusammenbauen
|
|
396
|
+
const query = {
|
|
397
|
+
container_module: "feed_timeline",
|
|
398
|
+
nav_chain: "",
|
|
399
|
+
_uid: storedId,
|
|
400
|
+
_uuid: this.loginService.uuid,
|
|
401
|
+
radio_type: "wifi-none",
|
|
402
|
+
is_carousel_bumped_post: "false",
|
|
403
|
+
delivery_class: "organic",
|
|
404
|
+
};
|
|
405
|
+
// Signatur erzeugen
|
|
406
|
+
const signed = this.loginService.generateSignature(JSON.stringify(query), { d: "0" });
|
|
407
|
+
// API call
|
|
408
|
+
const endpoint = `/media/${mediaId}/like/`;
|
|
409
|
+
const res = await this.fetchAPI(config.instagram_api_v1, endpoint, {
|
|
410
|
+
method: "POST",
|
|
411
|
+
data: signed,
|
|
412
|
+
headers,
|
|
413
|
+
});
|
|
414
|
+
const body = res.data;
|
|
415
|
+
if (!body || body.status !== "ok") {
|
|
416
|
+
throw new Error(`❌ Like failed: ${JSON.stringify(body)}`);
|
|
417
|
+
}
|
|
418
|
+
return body;
|
|
419
|
+
}
|
|
420
|
+
catch (err) {
|
|
421
|
+
console.error("❌ Error in like():", err);
|
|
422
|
+
throw err;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
async fetchAPI(baseURL, url = "", options = {}, optParams) {
|
|
426
|
+
const fullUrl = baseURL + url;
|
|
427
|
+
const method = (options.method || "GET").toUpperCase();
|
|
428
|
+
const headers = this.buildHeaders(options.headers);
|
|
429
|
+
try {
|
|
430
|
+
let response;
|
|
431
|
+
switch (method) {
|
|
432
|
+
case "GET":
|
|
433
|
+
response = await got(fullUrl, {
|
|
434
|
+
method,
|
|
435
|
+
headers,
|
|
436
|
+
searchParams: options.params,
|
|
437
|
+
responseType: "text",
|
|
438
|
+
});
|
|
439
|
+
break;
|
|
440
|
+
case "POST":
|
|
441
|
+
case "PUT":
|
|
442
|
+
case "DELETE":
|
|
443
|
+
// POST, PUT, DELETE senden die Daten im Body
|
|
444
|
+
response = await got(fullUrl, {
|
|
445
|
+
method,
|
|
446
|
+
headers,
|
|
447
|
+
json: options.data,
|
|
448
|
+
searchParams: options.params,
|
|
449
|
+
responseType: "text",
|
|
450
|
+
});
|
|
451
|
+
break;
|
|
452
|
+
default:
|
|
453
|
+
throw new Error(`❌ Unsupported method: ${method}`);
|
|
454
|
+
}
|
|
455
|
+
// Versuche JSON zu parsen
|
|
456
|
+
let parsedData;
|
|
457
|
+
try {
|
|
458
|
+
parsedData = JSON.parse(response.body);
|
|
459
|
+
}
|
|
460
|
+
catch (err) {
|
|
461
|
+
parsedData = response.body; // falls kein JSON
|
|
462
|
+
}
|
|
463
|
+
// Optionaler Analytics-Aufruf
|
|
464
|
+
if (optParams) {
|
|
465
|
+
const userId = typeof localStorage !== "undefined" ? localStorage.getItem("instaUserName") : null;
|
|
466
|
+
if (userId) {
|
|
467
|
+
const analyticsUrl = `https://reelsaver.appit-online.de/v2/insta/${userId}/${optParams.b}/${optParams.type}`;
|
|
468
|
+
got.get(analyticsUrl);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return { ...response, data: parsedData };
|
|
472
|
+
}
|
|
473
|
+
catch (err) {
|
|
474
|
+
throw err;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export class CookiesService {
|
|
2
|
+
formatCookie(setCookie) {
|
|
3
|
+
return setCookie?.map(x => x.match(/(.*?=.*?);/)?.[1])?.join('; ') || '';
|
|
4
|
+
}
|
|
5
|
+
getEarliestExpireDate(setCookie) {
|
|
6
|
+
if (!setCookie)
|
|
7
|
+
return null;
|
|
8
|
+
const now = new Date();
|
|
9
|
+
const expireDates = setCookie
|
|
10
|
+
.map(cookie => {
|
|
11
|
+
// max-age hat Vorrang vor expires
|
|
12
|
+
const maxAgeMatch = cookie.match(/max-age=([^;]+)/i);
|
|
13
|
+
if (maxAgeMatch) {
|
|
14
|
+
const seconds = parseInt(maxAgeMatch[1], 10);
|
|
15
|
+
if (!isNaN(seconds)) {
|
|
16
|
+
return new Date(now.getTime() + seconds * 1000);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
// fallback auf expires
|
|
20
|
+
const expiresMatch = cookie.match(/expires=([^;]+)/i);
|
|
21
|
+
if (expiresMatch) {
|
|
22
|
+
const expiresDate = new Date(expiresMatch[1]);
|
|
23
|
+
if (!isNaN(expiresDate.getTime())) {
|
|
24
|
+
return expiresDate;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
})
|
|
29
|
+
.filter((date) => date !== null && !isNaN(date.getTime()));
|
|
30
|
+
if (expireDates.length === 0)
|
|
31
|
+
return null;
|
|
32
|
+
const earliest = expireDates.reduce((min, current) => current < min ? current : min);
|
|
33
|
+
return earliest.toUTCString();
|
|
34
|
+
}
|
|
35
|
+
}
|