Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Added clips timeline and clip upload support. #1742

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
612 changes: 600 additions & 12 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"class-transformer": "^0.3.1",
"debug": "^4.1.1",
"image-size": "^0.7.3",
"jsdom": "^23.0.1",
"json-bigint": "^1.0.0",
"lodash": "^4.17.20",
"luxon": "^1.12.1",
Expand All @@ -68,6 +69,7 @@
},
"devDependencies": {
"@types/bluebird": "^3.5.26",
"@types/jsdom": "^21.1.6",
"@types/lodash": "^4.14.123",
"@types/luxon": "^1.12.0",
"@types/node": "^10.14.5",
Expand Down
6 changes: 6 additions & 0 deletions src/core/feed.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
IgtvChannelFeed,
LikedFeed,
TopicalExploreFeed,
ClipsFeed,
} from '../feeds';
import { DirectInboxFeedResponseThreadsItem } from '../responses';
import { plainToClassFromExist } from 'class-transformer';
Expand Down Expand Up @@ -324,4 +325,9 @@ export class FeedFactory {
): TopicalExploreFeed {
return plainToClassFromExist(new TopicalExploreFeed(this.client), options);
}

public clipsFeed(): ClipsFeed {
const feed = new ClipsFeed(this.client);
return feed;
}
}
8 changes: 6 additions & 2 deletions src/core/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,19 @@ export class Request {
return resolveWithFullResponse ? response : response.body;
}

public async send<T = any>(userOptions: Options, onlyCheckHttpStatus?: boolean): Promise<IgResponse<T>> {
public async send<T = any>(
userOptions: Options,
onlyCheckHttpStatus?: boolean,
transform?: boolean,
): Promise<IgResponse<T>> {
const options = defaultsDeep(
userOptions,
{
baseUrl: 'https://i.instagram.com/',
resolveWithFullResponse: true,
proxy: this.client.state.proxyUrl,
simple: false,
transform: Request.requestTransform,
transform: transform == false ? undefined : Request.requestTransform,
jar: this.client.state.cookieJar,
strictSSL: false,
gzip: true,
Expand Down
3 changes: 2 additions & 1 deletion src/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class State {
build: string;
uuid: string;
phoneId: string;
fb_dtsg: string;
/**
* Google Play Advertising ID.
*
Expand Down Expand Up @@ -226,7 +227,7 @@ export class State {
const obj = typeof state === 'string' ? JSON.parse(state) : state;
if (typeof obj !== 'object') {
State.stateDebug(`State deserialization failed, obj is of type ${typeof obj} (object expected)`);
throw new TypeError('State isn\'t an object or serialized JSON');
throw new TypeError("State isn't an object or serialized JSON");
}
State.stateDebug(`Deserializing ${Object.keys(obj).join(', ')}`);
if (obj.constants) {
Expand Down
68 changes: 68 additions & 0 deletions src/feeds/clips.feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Expose } from 'class-transformer';
import { Feed } from '../core/feed';
import { ClipsFeedResponse, ClipsFeedVariables, TimelineFeedResponseMedia_or_ad } from '../responses';

export class ClipsFeed extends Feed<ClipsFeedResponse, TimelineFeedResponseMedia_or_ad> {
tag: string;
@Expose()
private feedVariables: ClipsFeedVariables;

set state(body) {
this.moreAvailable = body.data.xdt_api__v1__clips__home__connection_v2.page_info.has_next_page;
this.feedVariables = {
after: body.data.xdt_api__v1__clips__home__connection_v2.page_info.end_cursor,
before: null,
data: {
container_module: 'clips_tab_desktop_page',
seen_reels: body.data.xdt_api__v1__clips__home__connection_v2.edges.map(i => {
return { id: i.node.media.id };
}),
},
first: 10,
last: null,
};
}

async request() {
let form = {
fb_dtsg: this.client.state.fb_dtsg,
fb_api_caller_class: 'RelayModern',
fb_api_req_friendly_name: 'PolarisClipsTabDesktopContainerQuery',
server_timestamps: true,
doc_id: '6846808792076706',
};

if (this.feedVariables) {
form = Object.assign(form, {
variables: JSON.stringify(this.feedVariables),
});
} else {
form = Object.assign(form, {
variables: JSON.stringify({ data: { container_module: 'clips_tab_desktop_page' } }),
});
}

const { body } = await this.client.request.send<ClipsFeedResponse>(
{
url: '/api/graphql/',
method: 'POST',
headers: {
Host: 'www.instagram.com',
'Content-Type': 'application/x-www-form-urlencoded',
'X-FB-Friendly-Name': 'PolarisClipsTabDesktopContainerQuery',
'User-Agent': null,
},
form,
},
true,
);

this.state = body;
return body;
}

async items() {
const response = await this.request();
return response.data.xdt_api__v1__clips__home__connection_v2.edges.filter(i => i.node.media).map(i => i.node.media);
}
}
1 change: 1 addition & 0 deletions src/feeds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ export * from './igtv.browse.feed';
export * from './igtv.channel.feed';
export * from './liked.feed';
export * from './topical-explore.feed';
export * from './clips.feed';
46 changes: 37 additions & 9 deletions src/repositories/account.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import { IgSignupBlockError } from '../errors/ig-signup-block.error';
import Bluebird = require('bluebird');
import debug from 'debug';
import * as crypto from 'crypto';
import { JSDOM } from 'jsdom';

export class AccountRepository extends Repository {
private static accountDebug = debug('ig:account');
public async login(username: string, password: string): Promise<AccountRepositoryLoginResponseLogged_in_user> {
if (!this.client.state.passwordEncryptionPubKey) {
await this.client.qe.syncLoginExperiments();
}
const {encrypted, time} = this.encryptPassword(password);
const { encrypted, time } = this.encryptPassword(password);
const response = await Bluebird.try(() =>
this.client.request.send<AccountRepositoryLoginResponseRootObject>({
method: 'POST',
Expand Down Expand Up @@ -64,6 +65,8 @@ export class AccountRepository extends Repository {
}
}
});

this.client.state.fb_dtsg = await this.getDtsg();
return response.body.logged_in_user;
}

Expand All @@ -76,14 +79,37 @@ export class AccountRepository extends Repository {
return `2${sum}`;
}

public encryptPassword(password: string): { time: string, encrypted: string } {
private async getDtsg() {
const { body } = await this.client.request.send(
{
url: 'null',
method: 'GET',
headers: {},
},
true,
false, // disable transform to json because we are getting html
);

const dom = new JSDOM(body);
const unparsedParams = dom.window.document.querySelector('#__eqmc').innerHTML;

const params = JSON.parse(unparsedParams);
const fb_dtsg = params.f;

return fb_dtsg;
}

public encryptPassword(password: string): { time: string; encrypted: string } {
const randKey = crypto.randomBytes(32);
const iv = crypto.randomBytes(12);
const rsaEncrypted = crypto.publicEncrypt({
key: Buffer.from(this.client.state.passwordEncryptionPubKey, 'base64').toString(),
// @ts-ignore
padding: crypto.constants.RSA_PKCS1_PADDING,
}, randKey);
const rsaEncrypted = crypto.publicEncrypt(
{
key: Buffer.from(this.client.state.passwordEncryptionPubKey, 'base64').toString(),
// @ts-ignore
padding: crypto.constants.RSA_PKCS1_PADDING,
},
randKey,
);
const cipher = crypto.createCipheriv('aes-256-gcm', randKey, iv);
const time = Math.floor(Date.now() / 1000).toString();
cipher.setAAD(Buffer.from(time));
Expand All @@ -97,8 +123,10 @@ export class AccountRepository extends Repository {
Buffer.from([1, this.client.state.passwordEncryptionKeyId]),
iv,
sizeBuffer,
rsaEncrypted, authTag, aesEncrypted])
.toString('base64'),
rsaEncrypted,
authTag,
aesEncrypted,
]).toString('base64'),
};
}

Expand Down
21 changes: 9 additions & 12 deletions src/repositories/media.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class MediaRepository extends Repository {
return body;
}

public async configureVideo(options: MediaConfigureTimelineVideoOptions) {
public async configureVideo(options: MediaConfigureTimelineVideoOptions, isClipVideo: boolean) {
const now = DateTime.local().toFormat('yyyy:mm:dd HH:mm:ss');

const form = this.applyConfigureDefaults<MediaConfigureTimelineVideoOptions>(options, {
Expand All @@ -388,7 +388,7 @@ export class MediaRepository extends Repository {
}

const { body } = await this.client.request.send<MediaRepositoryConfigureResponseRootObject>({
url: '/api/v1/media/configure/',
url: '/api/v1/media/configure' + (isClipVideo ? '_to_clips/' : '/'),
method: 'POST',
qs: {
video: '1',
Expand Down Expand Up @@ -682,10 +682,10 @@ export class MediaRepository extends Repository {
* save a media, or save it to collection if you pass the collection ids in array
* @param {string} mediaId - The mediaId of the post
* @param {string[]} [collection_ids] - Optional, The array of collection ids if you want to save the media to a specific collection
* Example:
* Example:
* save("2524149952724070925_1829855275") save media
* save("2524149952724070925_1829855275", ["17865977635619975"]) save media to 1 collection
* save("2524149952724070925_1829855275", ["17865977635619975", "17845997638619928"]) save media to 2 collection
* save("2524149952724070925_1829855275", ["17865977635619975", "17845997638619928"]) save media to 2 collection
*/
public async save(mediaId: string, collection_ids?: string[]) {
const { body } = await this.client.request.send({
Expand Down Expand Up @@ -795,27 +795,24 @@ export class MediaRepository extends Repository {
});
return body;
}

private async storyCountdownAction(
countdownId: string | number,
action: string,
): Promise<StatusResponse> {

private async storyCountdownAction(countdownId: string | number, action: string): Promise<StatusResponse> {
const { body } = await this.client.request.send({
url: `/api/v1/media/${countdownId}/${action}/`,
method: 'POST',
form: this.client.request.sign({
_csrftoken: this.client.state.cookieCsrfToken,
_uid: this.client.state.cookieUserId,
_uuid: this.client.state.uuid
_uuid: this.client.state.uuid,
}),
});
return body;
}

public async storyCountdownFollow(countdownId: string | number) {
return this.storyCountdownAction(countdownId, 'follow_story_countdown');
}

public async storyCountdownUnfollow(countdownId: string | number) {
return this.storyCountdownAction(countdownId, 'unfollow_story_countdown');
}
Expand Down
3 changes: 3 additions & 0 deletions src/repositories/upload.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ export class UploadRepository extends Repository {
if (options.isDirectVoice) {
ruploadParams.is_direct_voice = '1';
}
if (options.isClipVideo) {
ruploadParams.is_clips_video = '1';
}
return ruploadParams;
}
}
39 changes: 39 additions & 0 deletions src/responses/clips.feed.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { TimelineFeedResponseMedia_or_ad } from './timeline.feed.response';

export interface ClipsFeedResponse {
data: {
xdt_api__v1__clips__home__connection_v2: {
edges: ClipsFeedResponseFeedItemsItem[];
page_info: {
end_cursor: string;
has_next_page: boolean;
has_previous_page: boolean;
start_cursor: string | null;
};
};
};
extensions: {
is_final: boolean;
};
}

export interface ClipsFeedResponseFeedItemsItem {
node: {
media: TimelineFeedResponseMedia_or_ad;
__typename: string;
};
cursor: string;
}

export interface ClipsFeedVariables {
after: string;
before: null;
data: {
container_module: 'clips_tab_desktop_page';
seen_reels: {
id: string;
}[];
};
first: number;
last: null;
}
1 change: 1 addition & 0 deletions src/responses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ export * from './igtv.channel.feed.response';
export * from './igtv.search.response';
export * from './liked.feed.response';
export * from './topical-explore.feed.response';
export * from './clips.feed.response';
3 changes: 2 additions & 1 deletion src/services/publish.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ export class PublishService extends Repository {
await Bluebird.try(() =>
this.regularVideo({
video: options.video,
isClipVideo: options.isClipVideo,
uploadId,
...videoInfo,
}),
Expand Down Expand Up @@ -198,7 +199,7 @@ export class PublishService extends Repository {

for (let i = 0; i < 6; i++) {
try {
return await this.client.media.configureVideo(configureOptions);
return await this.client.media.configureVideo(configureOptions, options.isClipVideo);
} catch (e) {
if (i >= 5 || e.response.statusCode >= 400) {
throw new IgConfigureVideoError(e.response, configureOptions);
Expand Down
2 changes: 2 additions & 0 deletions src/types/media.configure-video.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface MediaConfigureVideoOptions {
posting_longitude?: string;
media_latitude?: string;
media_longitude?: string;

isClipVideo?: boolean;
}

export interface MediaConfigureTimelineVideoOptions extends MediaConfigureVideoOptions {
Expand Down
1 change: 1 addition & 0 deletions src/types/posting.video.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface PostingVideoOptions {
usertags?: PostingUsertags;
location?: PostingLocation;
transcodeDelay?: number;
isClipVideo?: boolean;
}

export interface PostingStoryVideoOptions extends PostingStoryOptions {
Expand Down
1 change: 1 addition & 0 deletions src/types/upload.video.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface UploadVideoOptions {
waterfallId?: string;
uploadName?: string;
offset?: number;
isClipVideo?: boolean;
}

export interface UploadVideoSegmentInitOptions {
Expand Down