SocialDragon/instagram/api.go

150 lines
4 KiB
Go
Raw Normal View History

package instagram
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"os"
"github.com/disintegration/imaging"
"git.1750studios.com/AniNite/SocialDragon/config"
)
type InstagramListResponse struct {
Tag struct {
Media struct {
Nodes []struct {
ID string `json:"id"`
Caption string `json:"caption"`
DisplaySrc string `json:"display_src"`
IsVideo bool `json:"is_video"`
Owner struct {
ID string `json:"id"`
} `json:"owner"`
ThumbnailSrc string `json:"thumbnail_src"`
Code string `json:"code"`
Date int `json:"date"`
} `json:"nodes"`
PageInfo struct {
EndCursor string `json:"end_cursor"`
HasNextPage bool `json:"has_next_page"`
StartCursor string `json:"start_cursor"`
HasPreviousPage bool `json:"has_previous_page"`
} `json:"page_info"`
Count int `json:"count"`
} `json:"media"`
} `json:"tag"`
}
type InstagramPostResponse struct {
Media struct {
Owner struct {
ProfilePicURL string `json:"profile_pic_url"`
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"`
BlockedByViewer bool `json:"blocked_by_viewer"`
IsUnpublished bool `json:"is_unpublished"`
} `json:"owner"`
CaptionIsEdited bool `json:"caption_is_edited"`
VideoURL string `json:"video_url"`
DisplaySrc string `json:"display_src"`
CommentsDisabled bool `json:"comments_disabled"`
Code string `json:"code"`
IsAd bool `json:"is_ad"`
IsVideo bool `json:"is_video"`
Caption string `json:"caption"`
ID string `json:"id"`
Date int `json:"date"`
VideoViews int `json:"video_views"`
} `json:"media"`
}
func LoadList() (*InstagramListResponse, error) {
var listResponse InstagramListResponse
res, err := GetHTTPResource("https://www.instagram.com/explore/tags/" + config.C.Instagram.Tag + "/?__a=1")
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
jerr := json.Unmarshal(body, &listResponse)
if jerr != nil {
return nil, jerr
}
return &listResponse, nil
}
func LoadPost(code string) (*InstagramPostResponse, error) {
var postResponse InstagramPostResponse
res, err := GetHTTPResource("https://www.instagram.com/p/" + code + "/?__a=1")
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
jerr := json.Unmarshal(body, &postResponse)
if jerr != nil {
return nil, jerr
}
return &postResponse, nil
}
func ImageNameGenerator(seed string) (string, string) {
seedBytes := []byte(seed)
sha256Bytes := sha256.Sum256(seedBytes)
hash := hex.EncodeToString(sha256Bytes[:])
folders := config.C.ContentDirectory + "/" + hash[0:2] + "/" + hash[0:4] + "/"
urls := config.C.ContentWebDirectory + "/" + hash[0:2] + "/" + hash[0:4] + "/"
if err := os.MkdirAll(folders, 0775); err != nil {
log.Fatalf("FAT Could not create ContentDirectory, error: %+v", err)
}
finalPath := folders + hash
finalUrl := urls + hash
return finalPath, finalUrl
}
func DownloadMedia(data io.Reader, path string, video bool) (string, error) {
if video {
ext := ".mp4"
blob, err := ioutil.ReadAll(data)
if err != nil {
return "", err
}
if len(blob) == 0 {
return "", errors.New("Empty response")
}
err = ioutil.WriteFile(path+ext, blob, 0644)
if err != nil {
return "", err
}
return ext, nil
} else {
ext := ".jpg"
image, err := imaging.Decode(data)
if err != nil {
return "", err
}
err = imaging.Save(image, path+ext)
if err != nil {
return "", err
}
return ext, nil
}
}