This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
/
backnumber.go
119 lines (98 loc) · 2.7 KB
/
backnumber.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
package command
import (
"flag"
"fmt"
"io/fs"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/KleinChiu/fantia-dl/core"
)
type BacknumberParams struct {
year int
month int
plan int
global GlobalParams
}
func NewBacknumberCommand(fs *flag.FlagSet) *BacknumberParams {
cmd := new(BacknumberParams)
addGlobalFlags(fs, &cmd.global)
fs.IntVar(&cmd.year, "year", 0, "Year of the back number issue")
fs.IntVar(&cmd.month, "month", 0, "Month of the back number issue")
fs.IntVar(&cmd.plan, "plan", 0, "Plan of the back number")
fs.Func("url", "Backnumber URL", func(s string) error {
raw, err := url.Parse(s)
if err != nil {
return fmt.Errorf("invalid url")
}
path := strings.Split(raw.Path, "/")
if path[3] != "backnumbers" {
return fmt.Errorf("invalid backnumber url")
}
m := raw.Query().Get("month")
if len(m) != 6 {
return fmt.Errorf("invalid backnumber url")
}
cmd.year, _ = strconv.Atoi(m[0:4])
cmd.month, _ = strconv.Atoi(m[4:])
cmd.plan, _ = strconv.Atoi(raw.Query().Get("plan"))
return nil
})
return cmd
}
func (p BacknumberParams) Sanitize() error {
if err := sanitizeGlobalParam(p.global); err != nil {
return err
}
if p.year < 2016 || p.year > time.Now().Year() {
return fmt.Errorf("invalid year of %d", p.year)
}
if p.month < 0 || p.month > 12 {
return fmt.Errorf("invalid month of %d", p.month)
}
if p.plan == 0 {
return fmt.Errorf("invalid plan of %d", p.plan)
}
return nil
}
func (p BacknumberParams) Execute() error {
agent := core.NewAgent(p.global.session)
api, err := core.FetchBacknumber(agent, p.plan, p.year, p.month)
if err != nil {
return err
}
bnRoot := api.JoinBasePath(p.global.dir)
os.MkdirAll(bnRoot, fs.ModeDir)
for _, content := range api.BacknumberContents {
root := content.JoinBasePath(bnRoot)
os.MkdirAll(root, fs.ModeDir)
src := content.ParentPost.Title
if len(content.Title) > 0 {
src += " - " + content.Title
}
switch content.Category {
case "file":
if content.DownloadURI == "" {
continue
}
if p.global.dryRun {
fmt.Fprintf(os.Stdout, "Will download file %s from %s\n", content.Filename, src)
continue
}
_, path, _ := core.DownloadContent(agent, root, core.BaseUrl+content.DownloadURI, content.Title, p.global.overwrite)
os.Chtimes(path, time.Now(), content.ParentPost.Date)
case "photo_gallery":
for _, photo := range content.PostContentPhotos {
if p.global.dryRun {
fmt.Fprintf(os.Stdout, "Will download photo %d from %s\n", photo.ID, src)
continue
}
_, path, _ := core.DownloadContent(agent, root, photo.URL.Original, strconv.Itoa(photo.ID), p.global.overwrite)
os.Chtimes(path, time.Now(), content.ParentPost.Date)
}
}
}
return nil
}