-
Notifications
You must be signed in to change notification settings - Fork 0
/
booklog.go
189 lines (152 loc) · 4.6 KB
/
booklog.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
type Response struct {
Books []struct {
ID string `json:"id"`
Tags []string `json:"tags"`
} `json:"books"`
}
func GetBookInfo(keyword string) (string, []string, error) {
url := fmt.Sprintf("https://booklog.jp/users/%s/all?category_id=all&status=all&sort=sort_desc&rank=all&tag=&page=1&keyword=%s&reviewed="ed=&json=true", config.Username, keyword)
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", nil, fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("Cookie", fmt.Sprintf("bid=%s%%3D", config.Cookie))
req.Header.Set("Referer", fmt.Sprintf("https://booklog.jp/users/%s", config.Username))
req.Header.Set("X-Requested-With", "XMLHttpRequest")
resp, err := client.Do(req)
if err != nil {
return "", nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", nil, fmt.Errorf("error reading response: %v", err)
}
var data Response
err = json.Unmarshal(body, &data)
if err != nil {
return "", nil, fmt.Errorf("error parsing JSON: %v", err)
}
if len(data.Books) > 0 {
return data.Books[0].ID, data.Books[0].Tags, nil
}
return "", nil, fmt.Errorf("book not found")
}
func UpdateItemTag(itemId string, tags []string) error {
apiURL := "https://booklog.jp/api/book/tag"
tagString := strings.Join(tags, " ")
data := url.Values{}
data.Set("service_id", "1")
data.Set("id", itemId)
data.Set("tags", tagString)
client := &http.Client{}
req, err := http.NewRequest("POST", apiURL, strings.NewReader(data.Encode()))
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
req.Header.Set("Cookie", fmt.Sprintf("bid=%s%%3D", config.Cookie))
req.Header.Set("Referer", fmt.Sprintf("https://booklog.jp/users/%s", config.Username))
req.Header.Set("X-Requested-With", "XMLHttpRequest")
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return nil
}
func AddTagToItemByItemId(itemId, newTag string) error {
// Get the current tags from the API
_, currentTags, err := GetBookInfo(itemId)
if err != nil {
return fmt.Errorf("error getting current tags: %v", err)
}
// Check if the newTag is already in the currentTags
for _, tag := range currentTags {
if tag == newTag {
fmt.Printf("Tag already exists: %v\n", itemId)
return nil
}
}
// Add the newTag to the currentTags
newTags := append(currentTags, newTag)
err = UpdateItemTag(itemId, newTags)
if err != nil {
return fmt.Errorf("error updating tags: %v", err)
}
fmt.Printf("Tag added successfully: %v\n", itemId)
return nil
}
func AddTagToItemByIsbn(isbn, newTag string) error {
itemId, _, err := GetBookInfo(isbn)
if err != nil {
return fmt.Errorf("error getting book info: %v", err)
}
err = AddTagToItemByItemId(itemId, newTag)
if err != nil {
return fmt.Errorf("error updating tags: %v", err)
}
return nil
}
func UpdateItemLocationByItemId(itemId, newLocation string) error {
// Get the current tags from the API
_, currentTags, err := GetBookInfo(itemId)
if err != nil {
return fmt.Errorf("error getting current tags: %v", err)
}
// Check if currentTags contain the newLocation tag or any tag starting with "loc_"
found := false
locationTagExists := false
newTags := make([]string, 0)
for _, tag := range currentTags {
if tag == newLocation {
locationTagExists = true
} else if strings.HasPrefix(tag, "loc_") {
found = true
} else {
newTags = append(newTags, tag)
}
}
if locationTagExists {
fmt.Println("Location tag already added")
return nil
}
if found {
// Replace the existing location tag with newLocation
newTags = append(newTags, newLocation)
} else {
// Add newLocation to newTags
newTags = append(currentTags, newLocation)
}
// Update the item's tags with newTags
err = UpdateItemTag(itemId, newTags)
if err != nil {
return fmt.Errorf("error updating tags: %v", err)
}
fmt.Printf("Location updated successfully: %v\n", itemId)
return nil
}
func UpdateItemLocationByIsbn(isbn, newLocation string) error {
itemId, _, err := GetBookInfo(isbn)
if err != nil {
return fmt.Errorf("error getting book info: %v", err)
}
err = UpdateItemLocationByItemId(itemId, newLocation)
if err != nil {
return fmt.Errorf("error updating location: %v", err)
}
return nil
}