-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (89 loc) · 1.91 KB
/
main.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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
"strings"
"github.com/k0kubun/pp/v3"
)
type Leecher struct {
cookie string
}
type TeaserList struct {
TeaserGroups []struct {
Items []struct {
Title string `json:"title"`
Navigation struct {
Href string `json:"href"`
} `json:"navigation"`
} `json:"items"`
} `json:"teaserGroups"`
}
func main() {
_ = pp.Print
l := Leecher{}
l.fetchCookie()
l.fetchAllMagazines()
l.fetchAllAudiobooks()
}
func (l *Leecher) fetchCookie() {
// make a request to the start page to obtain the session tookie cooen
resp, err := http.Get("https://iceportal.de")
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
cookieHeader := resp.Header.Get("Set-Cookie")
if cookieHeader == "" {
log.Fatal("No cookie header found")
}
l.cookie = strings.Split(cookieHeader, ";")[0]
}
func (l *Leecher) get(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatal(err)
}
if l.cookie != "" {
req.Header.Set("Cookie", l.cookie)
}
resp, err := http.DefaultClient.Do(req)
return resp, err
}
func (l *Leecher) getJson(url string, v any) error {
resp, err := l.get(url)
if err != nil {
return err
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&v)
if err != nil {
return err
}
return nil
}
func (l *Leecher) saveTo(url string, filePath string) error {
if _, err := os.Stat(filePath); err == nil {
log.Print("File ", filePath, " exists already, skipping")
return nil
}
resp, err := l.get(url)
if err != nil {
return err
}
defer resp.Body.Close()
outFile, err := os.Create(filePath)
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
io.Copy(outFile, resp.Body)
log.Print("Saved to ", filePath)
return nil
}
func sanitizeFileOrPathName(input string) string {
input = strings.ReplaceAll(input, " ", "_")
return strings.ReplaceAll(input, "/", "_")
}