-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
115 lines (101 loc) · 2.86 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
111
112
113
114
115
package main
import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/otiai10/syobocal/api"
"github.com/otiai10/syobocal/factory"
"github.com/otiai10/syobocal/models"
)
var (
dbpath string
)
func init() {
flag.StringVar(&dbpath, "db", "db", "dir path of database")
flag.Parse()
}
func main() {
if err := crawl(); err != nil {
log.Fatalln(err)
}
}
func crawl() error {
// Define start/end time
tokyo, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
return fmt.Errorf("failed to load location: %v", err)
}
now := time.Now().In(tokyo)
log.Println("Now:\t", now)
_24HoursAgo := now.Add(-24 * time.Hour)
log.Println("24H Ago:\t", _24HoursAgo)
start := time.Date(
_24HoursAgo.Year(),
_24HoursAgo.Month(),
_24HoursAgo.Day(),
_24HoursAgo.Hour(),
0, 0, 0, tokyo,
)
end := start.Add(24 * time.Hour)
log.Println("Start:\t", start)
log.Println("End:\t", end)
// Build query
builder := api.TitleLookup().LastUpdate(start, end)
query := builder.Build().Encode()
log.Println("Query:\t", query)
// HTTP request
res, err := http.Get(api.BaseURL + "?" + query)
log.Println("Actual:\t", res.Request.URL.String())
if err != nil {
return fmt.Errorf("failed to send GET request: %v", err)
}
if res.StatusCode >= 400 {
return fmt.Errorf("http status seems ng: %v", res.Status)
}
defer res.Body.Close()
// Parse XML
lookup := api.TitleLookupResponse{}
if err := xml.NewDecoder(res.Body).Decode(&lookup); err != nil {
return fmt.Errorf("failed to decode XML: %v", err)
}
log.Println("Response:\t", lookup.Result.Code, lookup.Result.Message)
if lookup.Result.Code != 200 {
return fmt.Errorf("got syoboal non-200 response: %v %v", lookup.Result.Code, lookup.Result.Message)
}
// Convert to out model
animes, err := factory.ToAnimeListFromTitleLookup(lookup)
if err != nil {
return fmt.Errorf("failed to convert to Anime struct: %v", err)
}
log.Println("Animes:\t", len(animes))
// Prepare dir for JSON output
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get current wd: %v", err)
}
dir := filepath.Join(pwd, dbpath, start.Format("2006"), start.Format("01"))
if err = os.MkdirAll(dir, os.ModeSticky|os.ModePerm); err != nil {
return fmt.Errorf("failed to create dir for json output: %v", err)
}
// Write JSON file
f, err := os.Create(filepath.Join(dir, start.Format("02")+".json"))
if err != nil {
return fmt.Errorf("failed to open file to write json output: %v", err)
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
enc.SetEscapeHTML(false)
history := models.History{Animes: animes, Context: models.Context{Time: now, Query: res.Request.URL.String(), Output: f.Name()}}
if err := enc.Encode(history); err != nil {
return fmt.Errorf("failed to encode struct to json file: %v", err)
}
log.Println("File:\t", f.Name())
return nil
}