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

Get username #24

Open
freddygreve opened this issue Jan 26, 2025 · 7 comments
Open

Get username #24

freddygreve opened this issue Jan 26, 2025 · 7 comments

Comments

@freddygreve
Copy link

Is there any way to get the username of the owner in the api?

@riad-azz
Copy link
Owner

Hey, you can get the username from the meta elements like so by making these changes in src/features/instagram/utils.ts:

function getUsernameFromPostHtml(postHtml: CheerioAPI){
  const titleMetaContent = postHtml("meta[name='twitter:title']").attr("content") ?? ""

  if(titleMetaContent.length === 0){
   return null;
  }

  return titleMetaContent.split("@")[1].split(")")[0]
}

and you can add it here:

export const formatPageJson = (postHtml: CheerioAPI) => {
  const videoElement = postHtml("meta[property='og:video']");

  if (videoElement.length === 0) {
    return null;
  }

  const videoUrl = videoElement.attr("content");
  if (!videoUrl) return null;

  const width =
    postHtml("meta[property='og:video:width']").attr("content") ?? "";
  const height =
    postHtml("meta[property='og:video:height']").attr("content") ?? "";

  const filename = getIGVideoFileName();
  const username = getUsernameFromPostHtml(postHtml)

  const videoJson: VideoInfo = {
    filename,
    username.
    width,
    height,
    videoUrl,
  };

  return videoJson;
};

as for the graphal you can simply:

export const formatGraphqlJson = (data: MediaData) => {
  const filename = getIGVideoFileName();
  const username = data.owner.username ?? null;
  const width = data.dimensions.width.toString();
  const height = data.dimensions.height.toString();
  const videoUrl = data.video_url;

  const videoJson: VideoInfo = {
    filename,
    username 
    width,
    height,
    videoUrl,
  };

  return videoJson;
};

and of course dont forget to update the VideoInfo in src/types/index.ts type to fit the new structure:

export type VideoInfo = {
  filename: string;
  username: string | null;
  width: string;
  height: string;
  videoUrl: string;
};

@anggasanusi
Copy link

@riad-azz is there a way to get caption or description of the video ?

@riad-azz
Copy link
Owner

riad-azz commented Feb 2, 2025

@riad-azz is there a way to get caption or description of the video ?

you can do the same as the username one, scrape in the postHtml side and get it from the graphql on the API

@anggasanusi
Copy link

@riad-azz what is the element name for caption ?

@anggasanusi
Copy link

would appreciate if you can write with full code like above. thanks

@riad-azz
Copy link
Owner

would appreciate if you can write with full code like above. thanks

Hey,
I am not really a big Instagram user, so if you could show me an example of where the captions appear on a post,

I would be happy to try and help

@riad-azz
Copy link
Owner

would appreciate if you can write with full code like above. thanks

function getCaptionFromPostHtml(postHtml: CheerioAPI) {
  const captionEleOne = postHtml("[role=presentation] ul li h2+div");

  if (captionEleOne.length !== 0) {
    return captionEleOne.text();
  }

  const captionEleTwo = postHtml(
    "[role=main] hr + div div:not([class]) span > div > span"
  );

  if (captionEleTwo.length !== 0) {
    return captionEleTwo.text();
  }

  return null;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants