Skip to content

Commit

Permalink
golint, go vet
Browse files Browse the repository at this point in the history
  • Loading branch information
gkiryaziev committed May 8, 2016
1 parent 0ec5639 commit 28adb74
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 44 deletions.
51 changes: 26 additions & 25 deletions instagram.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import (
"time"
)

// Instagram struct
type Instagram struct {
userName string
password string
token string
isLoggedIn bool
uuid string
deviceId string
userNameId int64
deviceID string
userNameID int64
rankToken string
cookies []*http.Cookie
}
Expand All @@ -31,8 +32,8 @@ func NewInstagram(userName, password string) (*Instagram, error) {
token: "",
isLoggedIn: false,
uuid: generateUUID(true),
deviceId: generateDeviceId(),
userNameId: 0,
deviceID: generateDeviceID(),
userNameID: 0,
rankToken: "",
cookies: nil,
}
Expand All @@ -48,7 +49,7 @@ func NewInstagram(userName, password string) (*Instagram, error) {
// Login to instagram.
func (i *Instagram) Login() error {

fetch := API_URL + "/si/fetch_headers/?challenge_type=signup&guid=" + generateUUID(false)
fetch := APIURL + "/si/fetch_headers/?challenge_type=signup&guid=" + generateUUID(false)

resp, err := i.requestLogin("GET", fetch, nil)
if err != nil {
Expand All @@ -65,8 +66,8 @@ func (i *Instagram) Login() error {

// login
login := &Login{
DeviceId: i.deviceId,
Guid: i.uuid,
DeviceID: i.deviceID,
GUID: i.uuid,
UserName: i.userName,
Password: i.password,
Csrftoken: i.token,
Expand All @@ -80,7 +81,7 @@ func (i *Instagram) Login() error {

signature := generateSignature(jsonData)

resp, err = i.requestLogin("POST", API_URL+"/accounts/login/?", bytes.NewReader([]byte(signature)))
resp, err = i.requestLogin("POST", APIURL+"/accounts/login/?", bytes.NewReader([]byte(signature)))
if err != nil {
return err
}
Expand All @@ -105,16 +106,16 @@ func (i *Instagram) Login() error {
return errors.New(object.Message)
}

i.userNameId = object.LoggedInUser.Pk
i.rankToken = strconv.FormatInt(i.userNameId, 10) + "_" + i.uuid
i.userNameID = object.LoggedInUser.Pk
i.rankToken = strconv.FormatInt(i.userNameID, 10) + "_" + i.uuid

return nil
}

// GetMediaLikers return media likers.
func (i *Instagram) GetMediaLikers(mediaId string) (*MediaLikers, error) {
func (i *Instagram) GetMediaLikers(mediaID string) (*MediaLikers, error) {

endpoint := API_URL + "/media/" + mediaId + "/likers/?"
endpoint := APIURL + "/media/" + mediaID + "/likers/?"

resp, err := i.request("GET", endpoint, nil)
if err != nil {
Expand All @@ -131,9 +132,9 @@ func (i *Instagram) GetMediaLikers(mediaId string) (*MediaLikers, error) {
}

// GetMedia return media comments.
func (i *Instagram) GetMedia(mediaId string) (*Media, error) {
func (i *Instagram) GetMedia(mediaID string) (*Media, error) {

endpoint := API_URL + "/media/" + mediaId + "/comments/?"
endpoint := APIURL + "/media/" + mediaID + "/comments/?"

resp, err := i.request("GET", endpoint, nil)
if err != nil {
Expand All @@ -152,7 +153,7 @@ func (i *Instagram) GetMedia(mediaId string) (*Media, error) {
// GetRecentActivity return recent activity.
func (i *Instagram) GetRecentActivity() (*RecentActivity, error) {

endpoint := API_URL + "/news/inbox/?"
endpoint := APIURL + "/news/inbox/?"

resp, err := i.request("GET", endpoint, nil)
if err != nil {
Expand All @@ -171,7 +172,7 @@ func (i *Instagram) GetRecentActivity() (*RecentActivity, error) {
// SearchUsers return users.
func (i *Instagram) SearchUsers(query string) (*SearchUsers, error) {

endpoint := API_URL + "/users/search/?ig_sig_key_version=" + SIG_KEY_VERSION +
endpoint := APIURL + "/users/search/?ig_sig_key_version=" + SIGKEYVERSION +
"&is_typeahead=true&query=" + query + "&rank_token=" + i.rankToken

resp, err := i.request("GET", endpoint, nil)
Expand All @@ -189,9 +190,9 @@ func (i *Instagram) SearchUsers(query string) (*SearchUsers, error) {
}

// GetUserNameInfo return username info.
func (i *Instagram) GetUserNameInfo(userNameId int64) (*UserNameInfo, error) {
func (i *Instagram) GetUserNameInfo(userNameID int64) (*UserNameInfo, error) {

endpoint := API_URL + "/users/" + strconv.FormatInt(userNameId, 10) + "/info/?"
endpoint := APIURL + "/users/" + strconv.FormatInt(userNameID, 10) + "/info/?"

resp, err := i.request("GET", endpoint, nil)
if err != nil {
Expand All @@ -208,9 +209,9 @@ func (i *Instagram) GetUserNameInfo(userNameId int64) (*UserNameInfo, error) {
}

// GetUserTags return user tags.
func (i *Instagram) GetUserTags(userNameId int64) (*UserTags, error) {
func (i *Instagram) GetUserTags(userNameID int64) (*UserTags, error) {

endpoint := API_URL + "/usertags/" + strconv.FormatInt(userNameId, 10) + "/feed/?rank_token=" +
endpoint := APIURL + "/usertags/" + strconv.FormatInt(userNameID, 10) + "/feed/?rank_token=" +
i.rankToken + "&ranked_content=false"

resp, err := i.request("GET", endpoint, nil)
Expand All @@ -230,7 +231,7 @@ func (i *Instagram) GetUserTags(userNameId int64) (*UserTags, error) {
// SearchTags return tags.
func (i *Instagram) SearchTags(query string) (*SearchTags, error) {

endpoint := API_URL + "/tags/search/?is_typeahead=true&q=" + query + "&rank_token=" + i.rankToken
endpoint := APIURL + "/tags/search/?is_typeahead=true&q=" + query + "&rank_token=" + i.rankToken

resp, err := i.request("GET", endpoint, nil)
if err != nil {
Expand All @@ -247,9 +248,9 @@ func (i *Instagram) SearchTags(query string) (*SearchTags, error) {
}

// TagFeed return tagged media.
func (i *Instagram) TagFeed(tag, maxId string) (*TagFeed, error) {
func (i *Instagram) TagFeed(tag, maxID string) (*TagFeed, error) {

endpoint := API_URL + "/feed/tag/" + tag + "/?rank_token=" + i.rankToken + "&ranked_content=false&max_id=" + maxId
endpoint := APIURL + "/feed/tag/" + tag + "/?rank_token=" + i.rankToken + "&ranked_content=false&max_id=" + maxID

resp, err := i.request("GET", endpoint, nil)
if err != nil {
Expand All @@ -272,7 +273,7 @@ func (i *Instagram) requestLogin(method, endpoint string, body io.Reader) (*http
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", USER_AGENT)
req.Header.Add("User-Agent", USERAGENT)
resp, err := client.Do(req)
if err != nil {
return nil, err
Expand All @@ -287,7 +288,7 @@ func (i *Instagram) requestMain(method, endpoint string, body io.Reader) (*http.
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", USER_AGENT)
req.Header.Add("User-Agent", USERAGENT)
for _, cookie := range i.cookies {
req.AddCookie(cookie)
}
Expand Down
8 changes: 4 additions & 4 deletions model_login.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package instagram

// Login to Instagram.
// Login struct
type Login struct {
DeviceId string `json:"device_id"`
Guid string `json:"guid"`
DeviceID string `json:"device_id"`
GUID string `json:"guid"`
UserName string `json:"username"`
Password string `json:"password"`
Csrftoken string `json:"csrftoken"`
LoginAttemptCount string `json:"login_attempt_count"`
}

// Login to Instagram.
// LoginResponse struct
type LoginResponse struct {
LoggedInUser struct {
Username string `json:"username"`
Expand Down
4 changes: 2 additions & 2 deletions model_media.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package instagram

// Get media comments.
// Media struct
type Media struct {
Status string `json:"status"`
CaptionIsEdited bool `json:"caption_is_edited"`
Expand Down Expand Up @@ -45,7 +45,7 @@ type Media struct {
Message string `json:"message"` // from Error
}

// Get media likers.
// MediaLikers struct
type MediaLikers struct {
Status string `json:"status"`
UserCount int `json:"user_count"`
Expand Down
1 change: 1 addition & 0 deletions model_message.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package instagram

// Message struct
type Message struct {
Status string `json:"status"`
Message string `json:"message"` // from Error
Expand Down
3 changes: 2 additions & 1 deletion model_recent_activity.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package instagram

// Get recent activity.
// RecentActivity struct
type RecentActivity struct {
Status string `json:"status"`
NewStories []RecentActivityStories `json:"new_stories"`
Expand All @@ -19,6 +19,7 @@ type RecentActivity struct {
Message string `json:"message"` // from Error
}

// RecentActivityStories struct
type RecentActivityStories struct {
Pk string `json:"pk"`
Counts struct {
Expand Down
4 changes: 2 additions & 2 deletions model_search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package instagram

// Search tags.
// SearchTags struct
type SearchTags struct {
HasMore interface{} `json:"has_more"`
Status string `json:"status"`
Expand All @@ -12,7 +12,7 @@ type SearchTags struct {
Message string `json:"message"` // from Error
}

// Search users.
// SearchUsers struct
type SearchUsers struct {
HasMore bool `json:"has_more"`
Status string `json:"status"`
Expand Down
8 changes: 7 additions & 1 deletion model_tag_feed.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package instagram

// Get tagged media.
// TagFeed struct
type TagFeed struct {
RankedItems []struct {
Code string `json:"code"`
Expand Down Expand Up @@ -70,6 +70,7 @@ type TagFeed struct {
Message string `json:"message"` // from Error
}

// TagFeedImageVersions2 struct
type TagFeedImageVersions2 struct {
Candidates []struct {
URL string `json:"url"`
Expand All @@ -78,6 +79,7 @@ type TagFeedImageVersions2 struct {
} `json:"candidates"`
}

// TagFeedLocation struct
type TagFeedLocation struct {
ExternalSource string `json:"external_source"`
City string `json:"city"`
Expand All @@ -92,6 +94,7 @@ type TagFeedLocation struct {
FoursquareV2ID interface{} `json:"foursquare_v2_id"`
}

// TagFeedUser struct
type TagFeedUser struct {
Username string `json:"username"`
HasAnonymousProfilePicture bool `json:"has_anonymous_profile_picture"`
Expand All @@ -107,6 +110,7 @@ type TagFeedUser struct {
IsPrivate bool `json:"is_private"`
}

// TagFeedCaption struct
type TagFeedCaption struct {
Status string `json:"status"`
UserID int `json:"user_id"`
Expand All @@ -121,6 +125,7 @@ type TagFeedCaption struct {
Type int `json:"type"`
}

// TagFeedComments struct
type TagFeedComments struct {
Status string `json:"status"`
UserID int `json:"user_id"`
Expand All @@ -135,6 +140,7 @@ type TagFeedComments struct {
Type int `json:"type"`
}

// TagFeedUser2 struct
type TagFeedUser2 struct {
Username string `json:"username"`
Pk int `json:"pk"`
Expand Down
4 changes: 2 additions & 2 deletions model_user.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package instagram

// Get user tags
// UserTags struct
type UserTags struct {
Status string `json:"status"`
NumResults int `json:"num_results"`
Expand Down Expand Up @@ -100,7 +100,7 @@ type UserTags struct {
Message string `json:"message"` // from Error
}

// Get username info.
// UserNameInfo struct
type UserNameInfo struct {
Status string `json:"status"`
User struct {
Expand Down
18 changes: 11 additions & 7 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (
)

const (
API_URL = "https://i.instagram.com/api/v1"
USER_AGENT = "Instagram 7.13.1 Android (23/6.0.1; 515dpi; 1440x2416; huawei/google; Nexus 6P; angler; angler; en_US)"
IG_SIG_KEY = "8b46309eb680f272cc770d214b7dbe5f0c5d26b6cb82b0b740257360b43618f0"
SIG_KEY_VERSION = "4"
// APIURL api url
APIURL = "https://i.instagram.com/api/v1"
// USERAGENT user agent
USERAGENT = "Instagram 7.13.1 Android (23/6.0.1; 515dpi; 1440x2416; huawei/google; Nexus 6P; angler; angler; en_US)"
// IGSIGKEY ig sig key
IGSIGKEY = "8b46309eb680f272cc770d214b7dbe5f0c5d26b6cb82b0b740257360b43618f0"
// SIGKEYVERSION sig key version
SIGKEYVERSION = "4"
)

// generateUUID return uuid
Expand All @@ -30,14 +34,14 @@ func generateUUID(t bool) string {

// generateSignature return signature
func generateSignature(data []byte) string {
h := hmac.New(sha256.New, []byte(IG_SIG_KEY))
h := hmac.New(sha256.New, []byte(IGSIGKEY))
h.Write(data)
hash := hex.EncodeToString(h.Sum(nil))
return "ig_sig_key_version=" + SIG_KEY_VERSION + "&signed_body=" + hash + "." + url.QueryEscape(string(data))
return "ig_sig_key_version=" + SIGKEYVERSION + "&signed_body=" + hash + "." + url.QueryEscape(string(data))
}

// generateDeviceId return deviceId
func generateDeviceId() string {
func generateDeviceID() string {
buffer := make([]byte, 32)
rand.Read(buffer)
hash := md5.New()
Expand Down

0 comments on commit 28adb74

Please sign in to comment.