-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
89 lines (75 loc) · 1.84 KB
/
session.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
/* session
Instagram Bot - autofollow, like, comment dan unfollow not followback (c) Free Angel - [email protected]
please subscribe to my channel :
https://www.youtube.com/channel/UC15iFd0nlfG_tEBrt6Qz1NQ
*/
package main
import (
"errors"
"io/ioutil"
"math/rand"
"os"
"github.com/ahmdrz/goinsta/store"
"github.com/jimlawless/whereami"
)
// Logins and saves the session
func createAndSaveSession() error {
err := Instagram.Login()
if err != nil {
mylog(whereami.WhereAmI(), err)
return err
}
key := createKey()
bytes, err := store.Export(Instagram, key)
if err != nil {
mylog(whereami.WhereAmI(), err)
return err
}
err = ioutil.WriteFile("session", bytes, 0644)
if err != nil {
mylog(whereami.WhereAmI(), err)
return err
}
return nil
}
// reloadSession will attempt to recover a previous session
func reloadSession() error {
if _, err := os.Stat("session"); os.IsNotExist(err) {
mylog(whereami.WhereAmI(), "No Session Found")
return errors.New("No session found")
}
session, err := ioutil.ReadFile("session")
if err != nil {
mylog(whereami.WhereAmI(), err)
return err
}
mylog(whereami.WhereAmI(), "A session file exists")
key, err := ioutil.ReadFile("key")
if err != nil {
mylog(whereami.WhereAmI(), err)
return err
}
Instagram, err = store.Import(session, key)
if err != nil {
mylog(whereami.WhereAmI(), err)
return errors.New("Couldn't recover the session")
}
mylog(whereami.WhereAmI(), "Successfully logged in")
return nil
}
// createKey creates a key and saves it to file
func createKey() []byte {
key := make([]byte, 32)
_, err := rand.Read(key)
if err != nil {
mylog(whereami.WhereAmI(), err)
return nil
}
err = ioutil.WriteFile("key", key, 0644)
if err != nil {
mylog(whereami.WhereAmI(), err)
return nil
}
// mylog(whereami.WhereAmI(), "Created and saved the key", key)
return key
}