Fix Instagram API

Instagram API working again, in question is for how long...
This commit is contained in:
Andreas Mieke 2017-05-07 16:50:13 +02:00
parent ee82fdf57e
commit d1b91fcee3
2 changed files with 143 additions and 55 deletions

View file

@ -17,56 +17,144 @@ import (
type InstagramListResponse struct { type InstagramListResponse struct {
Tag struct { Tag struct {
Name string `json:"name"`
ContentAdvisory interface{} `json:"content_advisory"`
Media struct { Media struct {
Nodes []struct { Nodes []struct {
CommentsDisabled bool `json:"comments_disabled"`
ID string `json:"id"` ID string `json:"id"`
Caption string `json:"caption"` Dimensions struct {
DisplaySrc string `json:"display_src"` Height int `json:"height"`
IsVideo bool `json:"is_video"` Width int `json:"width"`
} `json:"dimensions"`
Owner struct { Owner struct {
ID string `json:"id"` ID string `json:"id"`
} `json:"owner"` } `json:"owner"`
ThumbnailSrc string `json:"thumbnail_src"` ThumbnailSrc string `json:"thumbnail_src"`
IsVideo bool `json:"is_video"`
Code string `json:"code"` Code string `json:"code"`
Date int `json:"date"` Date int `json:"date"`
} `json:"nodes"` DisplaySrc string `json:"display_src"`
PageInfo struct { Caption string `json:"caption"`
EndCursor string `json:"end_cursor"` Comments struct {
HasNextPage bool `json:"has_next_page"`
StartCursor string `json:"start_cursor"`
HasPreviousPage bool `json:"has_previous_page"`
} `json:"page_info"`
Count int `json:"count"` Count int `json:"count"`
} `json:"comments"`
Likes struct {
Count int `json:"count"`
} `json:"likes"`
} `json:"nodes"`
Count int `json:"count"`
PageInfo struct {
HasNextPage bool `json:"has_next_page"`
EndCursor string `json:"end_cursor"`
} `json:"page_info"`
} `json:"media"` } `json:"media"`
TopPosts struct {
Nodes []struct {
ID string `json:"id"`
Dimensions struct {
Height int `json:"height"`
Width int `json:"width"`
} `json:"dimensions"`
Owner struct {
ID string `json:"id"`
} `json:"owner"`
ThumbnailSrc string `json:"thumbnail_src"`
IsVideo bool `json:"is_video"`
Code string `json:"code"`
Date int `json:"date"`
DisplaySrc string `json:"display_src"`
Caption string `json:"caption"`
Comments struct {
Count int `json:"count"`
} `json:"comments"`
Likes struct {
Count int `json:"count"`
} `json:"likes"`
} `json:"nodes"`
} `json:"top_posts"`
} `json:"tag"` } `json:"tag"`
} }
type InstagramPostResponse struct { type InstagramPostResponse struct {
Media struct { Graphql struct {
Owner struct { ShortcodeMedia struct {
ProfilePicURL string `json:"profile_pic_url"` Typename string `json:"__typename"`
HasBlockedViewer bool `json:"has_blocked_viewer"`
Username string `json:"username"`
FullName string `json:"full_name"`
RequestedByViewer bool `json:"requested_by_viewer"`
FollowedByViewer bool `json:"followed_by_viewer"`
IsPrivate bool `json:"is_private"`
ID string `json:"id"` ID string `json:"id"`
BlockedByViewer bool `json:"blocked_by_viewer"` Shortcode string `json:"shortcode"`
IsUnpublished bool `json:"is_unpublished"` Dimensions struct {
} `json:"owner"` Height int `json:"height"`
CaptionIsEdited bool `json:"caption_is_edited"` Width int `json:"width"`
} `json:"dimensions"`
DisplayURL string `json:"display_url"`
VideoURL string `json:"video_url"` VideoURL string `json:"video_url"`
DisplaySrc string `json:"display_src"` VideoViewCount int `json:"video_view_count"`
CommentsDisabled bool `json:"comments_disabled"`
Code string `json:"code"`
IsAd bool `json:"is_ad"`
IsVideo bool `json:"is_video"` IsVideo bool `json:"is_video"`
Caption string `json:"caption"` EdgeMediaToTaggedUser struct {
Edges []interface{} `json:"edges"`
} `json:"edge_media_to_tagged_user"`
EdgeMediaToCaption struct {
Edges []struct {
Node struct {
Text string `json:"text"`
} `json:"node"`
} `json:"edges"`
} `json:"edge_media_to_caption"`
CaptionIsEdited bool `json:"caption_is_edited"`
EdgeMediaToComment struct {
Count int `json:"count"`
PageInfo struct {
HasNextPage bool `json:"has_next_page"`
EndCursor string `json:"end_cursor"`
} `json:"page_info"`
Edges []struct {
Node struct {
ID string `json:"id"` ID string `json:"id"`
Date int `json:"date"` Text string `json:"text"`
VideoViews int `json:"video_views"` CreatedAt int `json:"created_at"`
} `json:"media"` Owner struct {
ID string `json:"id"`
ProfilePicURL string `json:"profile_pic_url"`
Username string `json:"username"`
} `json:"owner"`
} `json:"node"`
} `json:"edges"`
} `json:"edge_media_to_comment"`
CommentsDisabled bool `json:"comments_disabled"`
TakenAtTimestamp int `json:"taken_at_timestamp"`
EdgeMediaPreviewLike struct {
Count int `json:"count"`
Edges []struct {
Node struct {
ID string `json:"id"`
ProfilePicURL string `json:"profile_pic_url"`
Username string `json:"username"`
} `json:"node"`
} `json:"edges"`
} `json:"edge_media_preview_like"`
EdgeMediaToSponsorUser struct {
Edges []interface{} `json:"edges"`
} `json:"edge_media_to_sponsor_user"`
Location interface{} `json:"location"`
ViewerHasLiked bool `json:"viewer_has_liked"`
Owner struct {
ID string `json:"id"`
ProfilePicURL string `json:"profile_pic_url"`
Username string `json:"username"`
FollowedByViewer bool `json:"followed_by_viewer"`
FullName string `json:"full_name"`
IsPrivate bool `json:"is_private"`
RequestedByViewer bool `json:"requested_by_viewer"`
IsUnpublished bool `json:"is_unpublished"`
BlockedByViewer bool `json:"blocked_by_viewer"`
HasBlockedViewer bool `json:"has_blocked_viewer"`
} `json:"owner"`
IsAd bool `json:"is_ad"`
EdgeWebMediaToRelatedMedia struct {
Edges []interface{} `json:"edges"`
} `json:"edge_web_media_to_related_media"`
} `json:"shortcode_media"`
} `json:"graphql"`
} }
func LoadList(token string) (*InstagramListResponse, error) { func LoadList(token string) (*InstagramListResponse, error) {

View file

@ -46,32 +46,32 @@ func iterList(list *InstagramListResponse) string {
log.Printf("Can't load Instagram post %s: %+v", image.Code, err) log.Printf("Can't load Instagram post %s: %+v", image.Code, err)
continue continue
} }
if post.Media.IsAd { if post.Graphql.ShortcodeMedia.IsAd {
continue continue
} }
var US database.User var US database.User
if database.Db.Model(database.User{}).Where("name = ? AND service = ?", post.Media.Owner.Username, database.Instagram).First(&US).Count(&count); count == 0 { if database.Db.Model(database.User{}).Where("name = ? AND service = ?", post.Graphql.ShortcodeMedia.Owner.Username, database.Instagram).First(&US).Count(&count); count == 0 {
US.DisplayName = post.Media.Owner.FullName US.DisplayName = post.Graphql.ShortcodeMedia.Owner.FullName
US.Name = post.Media.Owner.Username US.Name = post.Graphql.ShortcodeMedia.Owner.Username
US.Service = database.Instagram US.Service = database.Instagram
US.Blocked = false US.Blocked = false
database.Db.Create(&US) database.Db.Create(&US)
} }
if post.Media.IsVideo { if post.Graphql.ShortcodeMedia.IsVideo {
log.Printf("Found video %s from %s", post.Media.Code, post.Media.Owner.Username) log.Printf("Found video %s from %s", post.Graphql.ShortcodeMedia.Shortcode, post.Graphql.ShortcodeMedia.Owner.Username)
name, uname := ImageNameGenerator(post.Media.Code) name, uname := ImageNameGenerator(post.Graphql.ShortcodeMedia.Shortcode)
res, err := GetHTTPResource(post.Media.VideoURL) res, err := GetHTTPResource(post.Graphql.ShortcodeMedia.VideoURL)
if err != nil { if err != nil {
log.Printf("Can't load video %s: %+v", post.Media.Code, err) log.Printf("Can't load video %s: %+v", post.Graphql.ShortcodeMedia.Shortcode, err)
continue continue
} }
defer res.Body.Close() defer res.Body.Close()
ext, err := DownloadMedia(res.Body, name, true) ext, err := DownloadMedia(res.Body, name, true)
if err != nil { if err != nil {
log.Printf("Can't load video %s: %+v", post.Media.Code, err) log.Printf("Can't load video %s: %+v", post.Graphql.ShortcodeMedia.Shortcode, err)
continue continue
} }
log.Printf("Loaded video %s, location %s!", post.Media.Code, uname+ext) log.Printf("Loaded video %s, location %s!", post.Graphql.ShortcodeMedia.Shortcode, uname+ext)
var IT database.Item var IT database.Item
IT.UserID = US.ID IT.UserID = US.ID
IT.Service = database.Instagram IT.Service = database.Instagram
@ -82,23 +82,23 @@ func iterList(list *InstagramListResponse) string {
} }
IT.IsVideo = true IT.IsVideo = true
IT.Path = uname + ext IT.Path = uname + ext
IT.OriginalID = post.Media.Code IT.OriginalID = post.Graphql.ShortcodeMedia.Shortcode
database.Db.Create(&IT) database.Db.Create(&IT)
} else { } else {
log.Printf("Found picture %s from %s", post.Media.Code, post.Media.Owner.Username) log.Printf("Found picture %s from %s", post.Graphql.ShortcodeMedia.Shortcode, post.Graphql.ShortcodeMedia.Owner.Username)
name, uname := ImageNameGenerator(post.Media.Code) name, uname := ImageNameGenerator(post.Graphql.ShortcodeMedia.Shortcode)
res, err := GetHTTPResource(post.Media.DisplaySrc) res, err := GetHTTPResource(post.Graphql.ShortcodeMedia.DisplayURL)
if err != nil { if err != nil {
log.Printf("Can't load picture %s: %+v", post.Media.Code, err) log.Printf("Can't load picture %s: %+v", post.Graphql.ShortcodeMedia.Shortcode, err)
continue continue
} }
defer res.Body.Close() defer res.Body.Close()
ext, err := DownloadMedia(res.Body, name, false) ext, err := DownloadMedia(res.Body, name, false)
if err != nil { if err != nil {
log.Printf("Can't load picture %s: %+v", post.Media.Code, err) log.Printf("Can't load picture %s: %+v", post.Graphql.ShortcodeMedia.Shortcode, err)
continue continue
} }
log.Printf("Loaded picture %s, location %s!", post.Media.Code, uname+ext) log.Printf("Loaded picture %s, location %s!", post.Graphql.ShortcodeMedia.Shortcode, uname+ext)
var IT database.Item var IT database.Item
IT.UserID = US.ID IT.UserID = US.ID
IT.Service = database.Instagram IT.Service = database.Instagram
@ -109,7 +109,7 @@ func iterList(list *InstagramListResponse) string {
} }
IT.IsVideo = false IT.IsVideo = false
IT.Path = uname + ext IT.Path = uname + ext
IT.OriginalID = post.Media.Code IT.OriginalID = post.Graphql.ShortcodeMedia.Shortcode
database.Db.Create(&IT) database.Db.Create(&IT)
} }
} }