-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
39 lines (35 loc) · 781 Bytes
/
tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main
import (
"encoding/json"
"fmt"
"math/rand"
)
const tagUrl = "https://www.instagram.com/explore/tags/%s/?__a=1"
type InstagTagResponse struct {
Graphql struct {
Hashtag struct {
EdgeHashtagToMedia struct {
Edges []Edge `json:"edges"`
} `json:"edge_hashtag_to_media"`
} `json:"hashtag"`
} `json:"graphql"`
}
func GetFromTag(tag string) (string, error) {
url := fmt.Sprintf(tagUrl, tag)
response, err := Get(url)
if err != nil {
return "", err
}
result := &InstagTagResponse{}
err = json.Unmarshal(response, result)
if err != nil {
return "", err
}
edges := result.Graphql.Hashtag.EdgeHashtagToMedia.Edges
max := len(edges)
if max > 100 {
max = 100
}
randomEdge := edges[rand.Intn(max)]
return randomEdge.Node.DisplayUrl, nil
}