-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMALScraper.go
88 lines (77 loc) · 1.99 KB
/
MALScraper.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"golang.org/x/net/html"
)
func getAnimeList() []string {
animeData, err := ioutil.ReadFile("animes.txt")
if err != nil {
panic(err)
}
animes := strings.Split(string(animeData), "\n")
for i := range animes {
animes[i] = strings.TrimSpace(animes[i])
}
return animes
}
func findHrefsInLink(pageLink string) []string {
resp, err := http.Get(pageLink)
if err != nil {
panic(err)
}
defer resp.Body.Close()
tokenizer := html.NewTokenizer(resp.Body)
var hrefSlice []string
for {
currentTokenType := tokenizer.Next()
switch currentTokenType {
case html.ErrorToken:
return hrefSlice
case html.StartTagToken:
token := tokenizer.Token()
if token.Data == "a" {
for _, attr := range token.Attr {
if attr.Key == "href" {
path, _ := url.PathUnescape(attr.Val)
hrefSlice = append(hrefSlice, path)
}
}
}
}
}
}
func getAnimePage(animeName string) string {
fixedAnimeName := strings.ReplaceAll(animeName, " ", "%20")
malSearchLink := "https://myanimelist.net/search/all?q=" + fixedAnimeName
hrefs := findHrefsInLink(malSearchLink)
var wantedHref string
reg := regexp.MustCompile(`myanimelist.net/anime/[0-9]+/`)
for _, href := range hrefs {
if reg.FindString(href) != "" {
wantedHref = href
break
}
}
return wantedHref
}
func findBroadcastDateInAnimePage(animePage string) string {
resp, err := http.Get(animePage)
if err != nil {
panic(err)
}
defer resp.Body.Close()
html, err := ioutil.ReadAll(resp.Body)
dirtyBroadcastTimeRegex := regexp.MustCompile(`<span class="dark_text">Broadcast:</span>\s*.*\s*</div>`)
cleanBroadcastTimeRegex := regexp.MustCompile(`.*at.*`)
broadcastDate := dirtyBroadcastTimeRegex.FindString(string(html))
cleanBroadcastDate := cleanBroadcastTimeRegex.FindString(broadcastDate)
return cleanBroadcastDate
}
func printAnimeBroadcastDate(animeName string, date string) {
fmt.Println(animeName + ": " + strings.TrimSpace(date))
}