Last active
November 16, 2020 10:08
-
-
Save rknj/d95be740898789b2b514555af973cd0e to your computer and use it in GitHub Desktop.
This only works with Instagram public data. It gets all the pictures for the specified year and returns the best 9 (based on number of likes).
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
const getBest9PostsFromYear = (userId, after, year, media = []) => { | |
const jsonDataUrl = `https://www.instagram.com/graphql/query/?query_id=17888483320059182&id=${userId}&first=50`; | |
const instaUrl = `${jsonDataUrl}${after}`; | |
return fetch(instaUrl) | |
.then(response => response.json()) | |
.then(({data}) => { | |
const currentData = data.user.edge_owner_to_timeline_media; | |
const lastMediaYear = getMediaYear(currentData.edges[currentData.edges.length - 1].node.taken_at_timestamp); | |
const hasNextPage = currentData.page_info.has_next_page; | |
const cursor = currentData.page_info.end_cursor; | |
const moreResults = hasNextPage && lastMediaYear > year - 1; | |
const newMedia = currentData.edges.filter(media => getMediaYear(media.node.taken_at_timestamp) === year); | |
const updatedMedia = media | |
.concat(newMedia) | |
.sort((a, b) => b.node.edge_media_preview_like.count - a.node.edge_media_preview_like.count || b.node.edge_media_to_comment.count - a.node.edge_media_to_comment.count) | |
.splice(0, 9); | |
if (moreResults) { | |
return getBest9PostsFromYear(userId, `&after=${cursor}`, year, updatedMedia); | |
} | |
return updatedMedia; | |
}); | |
}; | |
const getMediaYear = date => new Date(date * 1000).getFullYear(); | |
const fetchMedia = (year) => { | |
const userId = "put-the-user-id-here"; | |
return new Promise((resolve, reject) => { | |
getBest9PostsFromYear(userId, "", year) | |
.then(response => resolve(response)) | |
}) | |
} | |
fetchMedia(2020); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment