Skip to content

Instantly share code, notes, and snippets.

@jg-l
Last active May 5, 2020 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jg-l/4f28ea56396de058d25b145454286db5 to your computer and use it in GitHub Desktop.
Save jg-l/4f28ea56396de058d25b145454286db5 to your computer and use it in GitHub Desktop.
Get Recent Media from Instagram
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/tidwall/gjson"
)
type Media struct {
Link, Source string
}
// getRecentMedia returns an array of Media objects. Provide an access token and count of how many images to return
func getRecentMedia(accessToken string, count int) []Media {
out := []Media{}
URL := fmt.Sprintf("https://api.instagram.com/v1/users/self/media/recent/?access_token=%s&count=%d", accessToken, count)
res, err := http.Get(URL)
if err != nil {
panic(err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
bodyString := string(body)
for i := 0; i < count; i++ {
get := fmt.Sprintf("data.%d.link", i)
link := gjson.Get(bodyString, get)
get = fmt.Sprintf("data.%d.images.standard_resolution.url", i)
source := gjson.Get(bodyString, get)
media := Media{Link: link.String(), Source: source.String()}
out = append(out, media)
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment