Skip to content

Commit

Permalink
feat: fetchPostByShortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
Gimenz committed Apr 12, 2024
1 parent 1a9ac3a commit 5771299
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "insta-fetcher",
"version": "1.3.34",
"version": "1.3.35",
"description": "Simplified Instagram metadata scraping",
"main": "./dist/index.js",
"scripts": {
Expand Down
39 changes: 24 additions & 15 deletions src/helper/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export const highlight_ids_query = (userId: string) => {
'query_hash': 'c9100bf9110dd6361671f113dd02e7d6',
'variables': JSON.stringify({
"user_id": userId,
"include_chaining":false,
"include_reel":true,
"include_suggested_users":false,
"include_logged_out_extras":false,
"include_highlight_reels":true,
"include_live_status":false
"include_chaining": false,
"include_reel": true,
"include_suggested_users": false,
"include_logged_out_extras": false,
"include_highlight_reels": true,
"include_live_status": false
})
}
}
Expand All @@ -19,15 +19,24 @@ export const highlight_media_query = (ids: string) => {
return {
'query_hash': '0a85e6ea60a4c99edc58ab2f3d17cfdf',
'variables': JSON.stringify({
"reel_ids":[],
"tag_names":[],
"location_ids":[],
"highlight_reel_ids":[ids],
"precomposed_overlay":false,
"show_story_viewer_list":true,
"story_viewer_fetch_count":50,
"story_viewer_cursor":"",
"stories_video_dash_manifest":false
"reel_ids": [],
"tag_names": [],
"location_ids": [],
"highlight_reel_ids": [ids],
"precomposed_overlay": false,
"show_story_viewer_list": true,
"story_viewer_fetch_count": 50,
"story_viewer_cursor": "",
"stories_video_dash_manifest": false
})
}
}

export const post_shortcode_query = (shortcode: string) => {
return {
'query_hash': '55a3c4bad29e4e20c20ff4cdfd80f5b4',
'variables': JSON.stringify({
shortcode
})
}
}
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import fs from 'fs'
import FormData from 'form-data';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { bufferToStream, getPostType, parseCookie, randInt, shortcodeFormatter } from './utils/index';
import { username, userId, seachTerm, url, IgCookie, ProductType, MediaType, IChangedProfilePicture, ISearchFollow, IGPostMetadata } from './types';
import { username, userId, seachTerm, url, IgCookie, ProductType, MediaType, IChangedProfilePicture, ISearchFollow, IGPostMetadata, PostGraphQL } from './types';
import { IGUserMetadata, UserGraphQL } from './types/UserMetadata';
import { IGStoriesMetadata, ItemStories, StoriesGraphQL } from './types/StoriesMetadata';
import { highlight_ids_query, highlight_media_query } from './helper/query';
import { highlight_ids_query, highlight_media_query, post_shortcode_query } from './helper/query';
import { HightlighGraphQL, ReelsIds } from './types/HighlightMetadata';
import { HMedia, IHighlightsMetadata, IReelsMetadata, ReelsMediaData } from './types/HighlightMediaMetadata';
import { IPostModels, IRawBody, MediaUrls } from './types/PostModels';
Expand Down Expand Up @@ -161,7 +161,6 @@ export class igApi {
public fetchPost = async (url: url): Promise<IPostModels> => {
const post = shortcodeFormatter(url);

//const req = (await IGFetchDesktop.get(`/${post.type}/${post.shortcode}/?__a=1`))
const metadata = await this.fetchPostByMediaId(post.media_id)

const item = metadata.items[0]
Expand Down Expand Up @@ -194,6 +193,17 @@ export class igApi {
}
}

public fetchPostByShortcode = async (shortcode: string): Promise<PostGraphQL> => {
const res = await this.FetchIGAPI(
config.instagram_base_url,
'/graphql/query/',
config.iPhone,
{ params: post_shortcode_query(shortcode) }
)
const graphql = res?.data;
return graphql;
}

/**
* fetch client account profile
*/
Expand Down
26 changes: 18 additions & 8 deletions src/types/PostMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MediaType, Typename, User } from '.';
import { MediaType, Typename, User } from '.';

export interface IGPostMetadata {
/** an Instagram Username */
Expand Down Expand Up @@ -31,7 +31,13 @@ export interface links {
}

export interface PostGraphQL {
shortcode_media: ShortcodeMedia;
data?: PostData;
extensions?: Extensions;
status?: string;
}

export interface PostData {
shortcode_media?: ShortcodeMedia;
}

export interface ShortcodeMedia extends ViewerInfo {
Expand Down Expand Up @@ -291,10 +297,14 @@ export interface ShortcodeMediaOwner {
}

export interface ClipsMusicAttributionInfo {
artist_name: string;
song_name: string;
uses_original_audio: boolean;
should_mute_audio: boolean;
should_mute_audio_reason: string;
audio_id: string;
artist_name: string;
song_name: string;
uses_original_audio: boolean;
should_mute_audio: boolean;
should_mute_audio_reason: string;
audio_id: string;
}

export interface Extensions {
is_final?: boolean;
}
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const IGPostRegex = /(?:https?:\/\/)?(?:www\.)?(?:instagram\.com(?:\/.+?)
* @returns {formattedShortcode}
*/
export const shortcodeFormatter = (url: string): formattedShortcode => {
const splitted = IGPostRegex.exec(url) || '';
const splitted = /(?:https?:\/\/)?(?:www\.)?(?:instagram\.com(?:\/.+?)?\/(p|reel(?:s|)|tv)\/)([\w-]+)(?:\/)?(\?.*)?$/gim.exec(url) || '';
return {
type: splitted[1],
shortcode: splitted[2],
Expand All @@ -57,7 +57,7 @@ export const shortcodeFormatter = (url: string): formattedShortcode => {
* @returns
*/
export const isIgPostUrl = (url: string): boolean => {
return IGPostRegex.test(url);
return /(?:https?:\/\/)?(?:www\.)?(?:instagram\.com(?:\/.+?)?\/(p|reel(?:s|)|tv)\/)([\w-]+)(?:\/)?(\?.*)?$/gim.test(url);
}

/**
Expand Down

0 comments on commit 5771299

Please sign in to comment.