This repository has been archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
154 lines (132 loc) · 3.71 KB
/
scraper.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package scraper
import (
"fmt"
"strconv"
"strings"
"github.com/gocolly/colly"
)
// Anime is the struct which cointains the information about an anime
type Anime struct {
IDAnime int
Year int
NameJap string
NameEng string
MalLink string
//ImageURL string
Songs []animeSong
}
// animeSong is the struct which contains the information about a theme
type animeSong struct {
Title string
Link string
Version string
Episodes string
Notes string
}
// List is the struct wihich contains all the animes
type List []Anime
//GetAnimeList returns an array of the Anime struct with all anime loaded
func GetAnimeList(output ...bool) List {
out := false
if len(output) > 0 {
out = output[0]
}
c := colly.NewCollector(colly.MaxDepth(1), colly.Async(true))
d := colly.NewCollector(colly.MaxDepth(0), colly.Async(true))
count := 0
var animeList List
c.OnHTML("div.md.wiki", func(e *colly.HTMLElement) {
el := e.DOM
for i := 0; i < el.Find("h3").Length(); i++ {
link, _ := el.Find("h3").Eq(i).Children().Eq(0).Attr("href")
linkAbs := "https://reddit.com" + link
d.Visit(linkAbs)
}
})
d.OnHTML("div.md.wiki", func(e *colly.HTMLElement) {
year := []rune(e.DOM.Find("h2").Eq(0).Text())
element := e.DOM.Find("h3")
for i := 0; i < element.Length(); i++ {
el := element.Eq(i)
var temp Anime
count++
temp.IDAnime = count
temp.NameJap = el.Text()
temp.MalLink, _ = el.Attr("href")
temp.Year, _ = strconv.Atoi(string(year[0:4]))
el = el.Next()
if el.Is("p") {
temp.NameEng = el.Text()
el = el.Next()
}
el = el.Find("tbody")
tempNameSong := ""
for j := 0; j < el.Find("tr").Length(); j++ {
var songs animeSong
row := el.Find("tr").Eq(j).Children()
title := row.Eq(0)
//fmt.Println(title.Text(), "is a song of", temp.NameEng)
if title.Text() != "" {
songs.Title = getTitle(title.Text()) //len(title.Text())-1)
tempNameSong = songs.Title
} else {
songs.Title = tempNameSong
}
if row.Find("a").Length() != 0 {
songs.Version = strings.Replace(title.Text(), `"`+songs.Title+`"`, "", -1)
link := title.Next()
songs.Link, _ = link.Children().Attr("href")
eps := link.Next()
songs.Episodes = eps.Text()
notes := eps.Next()
songs.Notes = notes.Text()
temp.Songs = append(temp.Songs, songs)
}
}
animeList = append(animeList, temp)
}
if out {
fmt.Println(count)
}
})
c.Visit("https://reddit.com/r/AnimeThemes/wiki/year_index")
c.Wait()
d.Wait()
return animeList
}
func getTitle(a string) string {
d := []rune(a)
return string(d[strings.Index(a, `"`)+1 : len(d)-1])
}
// SelectByJapName will find all the anime which contains the string in the japanese name
func (list List) SelectByJapName(name string) List {
var newList List
for i := range list {
if strings.Contains(strings.ToLower(list[i].NameJap), strings.ToLower(name)) {
newList = append(newList, list[i])
}
}
return newList
}
// SelectByEngName will find all the anime which contains the string in the english name
func (list List) SelectByEngName(name string) List {
var newList List
for i := range list {
if strings.Contains(strings.ToLower(list[i].NameEng), strings.ToLower(name)) {
newList = append(newList, list[i])
}
}
return newList
}
// SelectByBothNames will find all the anime which contains the string in the japanese or english name
func (list List) SelectByBothNames(name string) List {
var newList List
for i := range list {
if strings.Contains(strings.ToLower(list[i].NameEng), strings.ToLower(name)) {
newList = append(newList, list[i])
} else if strings.Contains(strings.ToLower(list[i].NameJap), strings.ToLower(name)) {
newList = append(newList, list[i])
}
}
return newList
}