-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnhentai.go
125 lines (115 loc) · 2.99 KB
/
nhentai.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
type HentaiDict struct {
ID int `json:"id"`
MediaID string `json:"media_id"`
Title struct {
English string `json:"english"`
Japanese string `json:"japanese"`
Pretty string `json:"pretty"`
} `json:"title"`
Images struct {
Pages []struct {
T string `json:"t"`
W int `json:"w"`
H int `json:"h"`
Url string
} `json:"pages"`
Cover struct {
T string `json:"t"`
W int `json:"w"`
H int `json:"h"`
Url string
} `json:"cover"`
Thumbnail struct {
T string `json:"t"`
W int `json:"w"`
H int `json:"h"`
Url string
} `json:"thumbnail"`
} `json:"images"`
Scanlator string `json:"scanlator"`
UploadDate int `json:"upload_date"`
Tags []struct {
ID int `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
URL string `json:"url"`
Count int `json:"count"`
} `json:"tags"`
NumPages int `json:"num_pages"`
NumFavorites int `json:"num_favorites"`
}
func ReadHentaiDownloadFile(fpath string) []string {
// Open the file
file, err := os.Open(fpath)
if err != nil {
panic(err)
}
defer file.Close()
// Read the file
links := []string{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
links = append(links, scanner.Text())
}
return links
}
func getByID(id string) HentaiDict {
// Perform a GET request
resp, err := http.Get("http://nhentai.net/api/gallery/" + id)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
panic(err)
}
// Serialize the response body into a HentaiDict struct
var hentai HentaiDict
err = json.Unmarshal(body, &hentai)
if err != nil {
panic(err)
}
// Fill the URL fields now that we have the MediaID
for i := range hentai.Images.Pages {
hentai.Images.Pages[i].Url = fmt.Sprintf("https://i.nhentai.net/galleries/%s/%d.jpg", hentai.MediaID, i+1)
}
// Cover and Thumbnail URLs
hentai.Images.Cover.Url = fmt.Sprintf("https://t.nhentai.net/galleries/%s/cover.jpg", hentai.MediaID)
hentai.Images.Thumbnail.Url = fmt.Sprintf("https://t.nhentai.net/galleries/%s/thumb.jpg", hentai.MediaID)
return hentai
}
// Download a whole hentai
// hentai: The hentai to download
// path: The directory to save the hentai, will be populated with the cover and pages
func DownloadHentai(hentai HentaiDict, path string) string {
path = filepath.Join(path, strconv.Itoa(hentai.ID))
// Check if the directory exists
if _, err := os.Stat(path); err == nil {
fmt.Println("Hentai already downloaded")
return path
}
println("Downloading hentai")
os.MkdirAll(path, os.ModePerm)
// Download the pages
for i, page := range hentai.Images.Pages {
downloadFile(page.Url, fmt.Sprintf("%s/%d.jpg", path, i))
// Sleep for a bit to avoid rate limiting
time.Sleep(time.Millisecond * 100)
println("Downloaded", i, "of", len(hentai.Images.Pages))
}
return path
}