-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapimgr.go
73 lines (62 loc) · 2.39 KB
/
apimgr.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
// Package instago helps you access the private API of Instagram. For examle,
// get URLs of all posts of a specific Instagram user, media (photos and videos)
// links of posts, stories of a Instagram user, your following and followers.
package instago
import (
"encoding/json"
"io/ioutil"
)
type IGApiManager struct {
cookies map[string]string
headers map[string]string
}
// UserAgent is important for accessing Instagram private API. Some Instagram
// Private API can only be accessed by Mobile UserAgent and some API only by
// Desktop UserAgent.
// Google Search: useragent ios instagram 134.00
// https://developers.whatismybrowser.com/useragents/parse/31984417-instagram-ios-iphone-11-pro-max-webkit
var appUserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 142.0.0.22.109 (iPhone12,5; iOS 14_1; en_US; en-US; scale=3.00; 1242x2688; 214888322) NW/1"
// SetUserAgent let you set User-Agent header in HTTP requests.
func SetUserAgent(s string) {
appUserAgent = s
}
// GetUserAgent returns current User-Agent header in HTTP requests.
func GetUserAgent() string {
return appUserAgent
}
// NewInstagramApiManager returns a API manager of a logged-in Instagram user,
// given the JSON file of cookies of a Instagram logged-in account.
//
// The cookies, such as *ds_user_id*, *sessionid*, or *csrftoken* can be viewed
// in Chrome Developer Tools. See https://stackoverflow.com/a/44773079
//
// You can get the JSON format cookies using chrome extension in crx-cookies/
// directory.
func NewInstagramApiManager(authFilePath string) (*IGApiManager, error) {
b, err := ioutil.ReadFile(authFilePath)
if err != nil {
return nil, err
}
cookies := make(map[string]string)
err = json.Unmarshal(b, &cookies)
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["User-Agent"] = appUserAgent
return NewApiManager(cookies, headers), nil
}
// NewApiManager is a more low-level initialization function for API manager.
// The original goal of this method is used to create API manager used in Chrome
// extension.
func NewApiManager(cookies, headers map[string]string) *IGApiManager {
var m IGApiManager
m.cookies = cookies
m.headers = headers
return &m
}
// GetSelfId returns the id of a Instagram user of the API manager.
func (m *IGApiManager) GetSelfId() string {
id, _ := m.cookies["ds_user_id"]
return id
}