This repository has been archived by the owner on Dec 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
101 lines (85 loc) · 2.57 KB
/
api.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
package goshindan
import (
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Shindan executes Shindan for given shindanID and returns result.
func Shindan(shindanID int, userName string) (string, error) {
shindanURL := fmt.Sprintf("https://shindanmaker.com/%d", shindanID)
values := url.Values{}
values.Add("u", userName)
resp, err := http.PostForm(shindanURL, values)
if err != nil {
return "", err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", err
}
result := doc.Find("div.result2 > div").Text()
return strings.TrimSpace(result), nil
}
// ShindanInfo is a struct to describe shindan.
type ShindanInfo struct {
Title string
Description string
URL string
ShindanTimes int // Times this shindan was shindaned.
Pattern int // Patterns for shindan result.
Star int
Author string
AuthorPage string
Keywords []string
}
// GetShindanInfo fetches Shindanmaker's information for given shindanID.
func GetShindanInfo(shindanID int) (ShindanInfo, error) {
shindanURL := fmt.Sprintf("https://shindanmaker.com/%d", shindanID)
resp, err := http.Get(shindanURL)
if err != nil {
return ShindanInfo{}, err
}
defer resp.Body.Close()
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return ShindanInfo{}, err
}
title := strings.TrimSpace(doc.Find("title").Text())
desc := strings.TrimSpace(doc.Find("div.shindantitle_block > div.shindandescription").Text())
stats := doc.Find("div.shindanstats > ul > li > b")
shindanTimes, err := strconv.Atoi(strings.Replace(stats.Slice(0, 1).Text(), ",", "", -1))
if err != nil {
return ShindanInfo{}, err
}
patt, err := strconv.Atoi(strings.Replace(stats.Slice(1, 2).Text(), ",", "", -1))
if err != nil {
return ShindanInfo{}, err
}
star, err := strconv.Atoi(strings.Replace((doc.Find("a.favlabel").Text()), "★", "", 1))
if err != nil {
return ShindanInfo{}, err
}
author := doc.Find("span.authorlabel > a").Text()
ap, _ := doc.Find("span.authorlabel > a").Attr("href")
authorPage := fmt.Sprintf("https://shindanmaker.com%s", ap)
keywords := []string{}
kws := doc.Find("span.shindanlabel > a.themelabel")
kws.Each(func(i int, kw *goquery.Selection) {
keywords = append(keywords, strings.TrimSpace(kw.Text()))
})
return ShindanInfo{
Title: title,
Description: desc,
URL: shindanURL,
ShindanTimes: shindanTimes,
Pattern: patt,
Star: star,
Author: author,
AuthorPage: authorPage,
Keywords: keywords,
}, nil
}