Last active
July 12, 2022 11:01
-
-
Save hongnguyenhuu96/c0bb2bc3c3ed21a8ca8dc11903c0913d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* login to your instagram account | |
* open browser's console (cmd option j on chrome macOS) | |
* run the following command: | |
* getData('mpt681988') | |
* mpt681988 is insta username | |
*/ | |
/** Convert a 2D array into a CSV string | |
*/ | |
function arrayToCsv(data) { | |
return data | |
.map( | |
(row) => | |
row | |
.map(String) // convert every value to String | |
.map((v) => v.replaceAll('"', '""')) // escape double colons | |
.map((v) => `"${v}"`) // quote it | |
.join(",") // comma-separated | |
) | |
.join("\r\n"); // rows starting on new lines | |
} | |
/** Download contents as a file | |
* Source: https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side | |
*/ | |
function downloadBlob(content, filename, contentType) { | |
// Create a blob | |
var blob = new Blob([content], { type: contentType }); | |
var url = URL.createObjectURL(blob); | |
// Create a link to download it | |
var pom = document.createElement("a"); | |
pom.href = url; | |
pom.setAttribute("download", filename); | |
pom.click(); | |
} | |
function extractResponse(responseData) { | |
let posts = responseData.data.user.edge_owner_to_timeline_media.edges; | |
let result = []; | |
posts.forEach((post) => { | |
let username = post.node.owner.username; | |
let id = post.node.id; | |
let link = `instagram.com/p/${post.node.shortcode}`; | |
let description = ""; | |
let captionEdges = post.node.edge_media_to_caption.edges; | |
let images = [post.node.display_url]; | |
if (captionEdges.length > 0) { | |
description = captionEdges[0].node.text; | |
} | |
if (post.node.edge_sidecar_to_children) { | |
let children = post.node.edge_sidecar_to_children.edges; | |
images = children.map((child) => child.node.display_url); | |
} | |
result.push({ id, link, username, description, images }); | |
}); | |
return result; | |
} | |
function sleep(ms) { | |
return new Promise((resolve) => setTimeout(resolve, ms)); | |
} | |
async function formatResponseToRows(posts) { | |
let rows = []; | |
if (posts.length == 0) { | |
return { rows }; | |
} | |
let images = {}; | |
posts.forEach((post) => { | |
images[post.id] = post.images; | |
}); | |
let username = posts[0].username; | |
console.log(images); | |
for (let postID in images) { | |
for (let j = 0; j < images[postID].length; j += 1) { | |
await sleep(200); | |
let imageName = `${username}-${postID}-${j}`; | |
let imageLink = images[postID][j]; | |
images[postID][j] = imageName; | |
await downloadImage(imageLink, imageName); | |
} | |
} | |
posts.forEach((post) => { | |
let row = [post.username, post.link, post.description]; | |
post.images.forEach((image) => { | |
row.push(image); | |
}); | |
rows.push(row); | |
images[post.id] = post.images; | |
}); | |
return { rows }; | |
} | |
async function downloadImage(imageSrc, imageName) { | |
const image = await fetch(imageSrc); | |
const imageBlog = await image.blob(); | |
const imageURL = URL.createObjectURL(imageBlog); | |
const link = document.createElement("a"); | |
link.href = imageURL; | |
link.download = imageName; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} | |
async function getData(username) { | |
try { | |
let response = await fetch( | |
`https://i.instagram.com/api/v1/users/web_profile_info/?username=${username}`, | |
{ | |
headers: { | |
"x-ig-app-id": "936619743392459", | |
}, | |
referrer: "https://www.instagram.com/", | |
referrerPolicy: "strict-origin-when-cross-origin", | |
body: null, | |
method: "GET", | |
mode: "cors", | |
credentials: "include", | |
} | |
); | |
let result = await response.json(); | |
let posts = extractResponse(result); | |
let { rows, images } = await formatResponseToRows(posts); | |
console.log(rows); | |
downloadBlob( | |
arrayToCsv(rows), | |
`${username}.csv`, | |
"text/csv;charset=utf-8;" | |
); | |
console.log(images); | |
} catch (e) { | |
console.log(e); | |
} | |
} | |
// getData("__viethoan"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment