Last active
May 5, 2020 02:58
-
-
Save jg-l/4f28ea56396de058d25b145454286db5 to your computer and use it in GitHub Desktop.
Get Recent Media from Instagram
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
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