DEV Community

Cover image for 💡 Tip Rapido: Fetch JSON data Desde un perfil publico simple sin Graph API
Poleselfg
Poleselfg

Posted on

6 1

💡 Tip Rapido: Fetch JSON data Desde un perfil publico simple sin Graph API

Nunca quisieron obtener informacion de un perfil de instagram de una manera facil y sencilla? Bueno...

En una de esas navegaciones nocturnas de poco dormir, me encontre con esta solucion, que por lo que pude ver esta poco difundida.

https://www.instagram.com/{public_profile_name}/?__a=1
Enter fullscreen mode Exit fullscreen mode

Resulta, que agregando el query ?__a=! al final de la direccion al perfil, podemos acceder a un JSON con toda la informacion publica del perfil.

por ejemplo:

https://www.instagram.com/refactordevs/?__a=1
Enter fullscreen mode Exit fullscreen mode

nos devuelve el siguiente JSON:

Alt Text

Como pueden ver el JSON es muy y tenemos toda la informacion de la cuenta, inclusive, con unas pocas lineas de codigo, (en este caso en javascript), podemos obtener el feed:

async function getInstagramPictures (profileName) {
  const baseUrl = "https://www.instagram.com";
  const profileUrl = `${baseUrl}/${profileName}`;
  const jsonDataUrl = `${profileUrl}/?__a=1`;

  const response = await fetch(jsonDataUrl);
  const jsonData = await response.json();
  const pictures = jsonData.graphql.user.edge_owner_to_timeline_media.edges;

  if (response.ok) {
    return pictures;
  } else {
    throw new Error(pictures);
  }
}

Enter fullscreen mode Exit fullscreen mode
getInstagramPictures("refactordevs")
  .then(pictures => console.log("Pictures:", pictures))
  .catch(error => console.error("Error:", error));
Enter fullscreen mode Exit fullscreen mode

Al realizar un bucle,se puede mostrar cada imagen o su miniatura.

Tambien podriamos traer las imagenes de perfil, o cualquier tipo de informacion.

solo basta con cambiar la siguiente linea:

const pictures = jsonData.graphql.user.edge_owner_to_timeline_media.edges;
Enter fullscreen mode Exit fullscreen mode

Saludos! y espero que les sea util!

Sources:

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (3)

Collapse
 
markkos89 profile image
Marcos Iglesias

Im having some CORS error on the fetch :/

Collapse
 
irwingb profile image
irwingb

It happend to me, I think Meta company doesn't allow to you fetch those public JSONs

Collapse
 
ilanvivanco profile image
Ilán Vivanco

Excelente info!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more