-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
100 lines (79 loc) · 2.12 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/gocolly/colly/v2"
)
type Book struct {
Title string
Author string
AuthorURL string
ImageURL string
Votes string
}
func ExtractBookAttributes(e *colly.HTMLElement) map[string]string {
image_url := e.ChildAttr("img", "src")
author_name := e.ChildText(".authorAllBooks__singleTextAuthor")
book_title := e.ChildText(".authorAllBooks__singleTextTitle")
author_url := e.ChildAttr(".authorAllBooks__singleTextAuthor a", "href")
rating := e.ChildText(".listLibrary__ratingAll") // split and get only number
book := make(map[string]string)
book["image_url"] = image_url
book["author_name"] = author_name
book["book_title"] = book_title
book["author_url"] = author_url
book["rating"] = rating
return book
}
func TotalPageNumber() int {
return 7 // so far, still static
}
func CreateJSONFile(books []Book) {
jsonData, err := json.MarshalIndent(books, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON:", err)
return
}
file, err := os.Create("books.json")
if err != nil {
fmt.Println("Error creating file:", err)
return
}
defer file.Close()
_, err = file.Write(jsonData)
if err != nil {
fmt.Println("Error writing JSON to file:", err)
return
}
fmt.Println("\nFILE CREATED: ", file.Name())
}
func main() {
var books []Book
c := colly.NewCollector()
c.OnRequest(func(r *colly.Request) {
// log.Print("VISITING ", r.URL)
})
c.OnHTML(".container", func(e *colly.HTMLElement) {
e.ForEach(".authorAllBooks__single", func(_ int, e *colly.HTMLElement) {
book_attribute := ExtractBookAttributes(e)
book := Book{
Title: book_attribute["book_title"],
Author: book_attribute["author_name"],
AuthorURL: book_attribute["author_url"],
ImageURL: book_attribute["image_url"],
Votes: book_attribute["rating"]}
books = append(books, book)
log.Print(book)
})
})
c.OnError(func(r *colly.Response, err error) {
log.Fatal(err)
})
for i := 1; i <= TotalPageNumber(); i++ {
url := fmt.Sprintf("https://lubimyczytac.pl/top100?page=%d", i)
c.Visit(url)
}
CreateJSONFile(books)
}