-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsafaribooks.go
142 lines (107 loc) · 2.69 KB
/
safaribooks.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"encoding/json"
"fmt"
"github.com/rahulvramesh/oreilly-books-grabber/models"
"golang.org/x/crypto/ssh/terminal"
"io/ioutil"
"os"
"strconv"
)
const credentialStore = ".cred"
func main() {
// login first
args := os.Args[1:]
if len(args) < 1 {
fmt.Println("No commands provided, available commands are")
fmt.Println("")
fmt.Println("\tsafaribooks login")
fmt.Println("\tsafaribooks logout")
fmt.Println("\tsafaribooks grab <bookId>\n")
return
}
command := args[0]
switch command {
case "login":
_ = os.Remove(credentialStore)
login()
break
case "logout":
_ = os.Remove(credentialStore)
fmt.Println("You have successfully logged out.")
break
case "grab":
bookid, err := strconv.Atoi(args[1])
if err != nil {
fmt.Println("Wrong book id provided.")
return
}
grab(bookid)
break
default:
fmt.Println("Wrong Command, available commands are")
fmt.Println("")
fmt.Println("\tsafaribooks login")
fmt.Println("\tsafaribooks logout")
fmt.Println("\tsafaribooks grab <bookId>\n")
os.Exit(0)
return
}
}
func login() {
fmt.Print("Enter the your email address: ")
var email, password string
_, err := fmt.Scanf("%s", &email)
if err != nil {
fmt.Println(err)
return
}
fmt.Print("Enter the your password: ")
pass, errpass := terminal.ReadPassword(0)
password = string(pass)
fmt.Println()
if errpass != nil {
fmt.Println(errpass)
return
}
credentials := models.LoginPayload{email, password}
credentialjson, _ := json.Marshal(credentials)
err = ioutil.WriteFile(credentialStore, credentialjson, 0644)
login, err := getCredential()
if err != nil {
fmt.Println("invalid credentials you need to login again.")
return
}
loginRes := models.DoLogin(login)
if loginRes.IDToken == "" {
_ = os.Remove("cred.json")
fmt.Println("login failed, login using \n\n\t safaribooks login \n")
return
}
fmt.Println("\nSuccessfully authenticated.\n")
fmt.Println("\tsafaribooks grab <bookId> to grab the books\n")
}
func getCredential() (models.LoginPayload, error) {
plan, _ := ioutil.ReadFile(credentialStore)
var data models.LoginPayload
err := json.Unmarshal(plan, &data)
return data, err
}
func grab(bookid int) {
login, err := getCredential()
if err != nil {
fmt.Println("invalid credentials you need to login again.")
return
}
loginRes := models.DoLogin(login)
if loginRes.IDToken == "" {
_ = os.Remove("cred.json")
fmt.Println("login failed, login using \n\n\t safaribooks login \n")
return
}
//now get the index
//pass login and book id example: 9781788993708
bookIndex, _ := models.GetBookIndex(loginRes, bookid)
//now for each item get content
models.SaveContentToFile(loginRes, bookIndex)
}