-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstagram.go
110 lines (90 loc) · 2.65 KB
/
instagram.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
//"github.com/kr/pretty"
)
type InstagramPic struct {
ID string
ShortCode string
Type string
Caption string
TotalLikes int64
UserID string
UserName string
TotalComments uint64
Thumbnail string
PublishedAt string
}
//var igMedia IGMedia
func (ig *InstagramPic) FetchMedia(maxID string) IGMedia {
var err error
igMedia := IGMedia{}
igClient := http.Client{
Timeout: time.Second * 5, // Maximum of 2 secs
}
url := fmt.Sprintf("https://www.instagram.com/p/%v?__a=1&maxid=%v", ig.ShortCode, maxID)
//fmt.Println(url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
LogMsg(err.Error())
return igMedia
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1")
res, err := igClient.Do(req)
if err != nil {
LogMsg(err.Error())
return igMedia
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
LogMsg(err.Error())
return igMedia
}
err = json.Unmarshal(body, &igMedia)
if err != nil {
LogMsg(err.Error())
return igMedia
}
return igMedia
}
func (ig *InstagramPic) GetMetadata() bool {
igMedia := ig.FetchMedia("")
ig.ID = igMedia.Graphql.ShortcodeMedia.ID
ig.ShortCode = igMedia.Graphql.ShortcodeMedia.Shortcode
if igMedia.Graphql.ShortcodeMedia.IsVideo {
ig.Type = "video"
} else {
ig.Type = "photo"
}
ig.Caption = igMedia.Graphql.ShortcodeMedia.EdgeMediaToCaption.Edges[0].Node.Text
ig.TotalLikes = int64(igMedia.Graphql.ShortcodeMedia.EdgeMediaPreviewLike.Count)
ig.UserID = igMedia.Graphql.ShortcodeMedia.Owner.ID
ig.UserName = igMedia.Graphql.ShortcodeMedia.Owner.Username
ig.TotalComments = uint64(igMedia.Graphql.ShortcodeMedia.EdgeMediaToComment.Count)
ig.PublishedAt = fmt.Sprint(igMedia.Graphql.ShortcodeMedia.TakenAtTimestamp)
ig.Thumbnail = igMedia.Graphql.ShortcodeMedia.DisplayURL
return true
}
func (ig InstagramPic) CommentLoop(comments []*Comment, maxID string) CommentList {
igMedia := ig.FetchMedia(maxID)
for _, c := range igMedia.Graphql.ShortcodeMedia.EdgeMediaToComment.Edges {
thisComment := &Comment{
ID: c.Node.ID,
Published: fmt.Sprint(c.Node.CreatedAt),
Content: c.Node.Text,
AuthorName: c.Node.Owner.Username,
}
comments = append(comments, thisComment)
}
//if igMedia.Graphql.ShortcodeMedia.EdgeMediaToComment.PageInfo.HasNextPage {
//return ig.CommentLoop(comments, igMedia.Graphql.ShortcodeMedia.EdgeMediaToComment.PageInfo.EndCursor)
//}
return CommentList{Comments: comments}
}
func (ig InstagramPic) GetComments() CommentList {
return ig.CommentLoop([]*Comment{}, "")
}