-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
114 lines (95 loc) · 2.7 KB
/
utils.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
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"strings"
"sync"
"github.com/gocolly/colly"
"log/slog"
"recipe-scraper/parser"
"recipe-scraper/shared"
)
var recipes []shared.Recipe
func isBlackListedExtension(link string) bool {
for _, ext := range config.BlackListedExtensions {
if strings.HasSuffix(link, ext) {
return true
}
}
return false
}
func isRootLink(link string, rootLink string) bool {
return link == rootLink+"/" || link == rootLink
}
func isValidLink(link string, rootLink string) bool {
return strings.HasPrefix(link, rootLink)
}
func handleLink(e *colly.HTMLElement, c *colly.Collector, wg *sync.WaitGroup, recipeChannel chan shared.Recipe, visitedLinks *map[string]struct{}, visitedLinksMu *sync.Mutex, parser parser.RecipeParser) {
defer wg.Done()
link := e.Attr("href")
visitedLinksMu.Lock()
defer visitedLinksMu.Unlock()
// dont use if contains the string comment
if strings.Contains(link, "comment") {
return
}
if !isValidLink(link, parser.RootLink()) || isBlackListedExtension(link) || isRootLink(link, parser.RootLink()) || isVisitedLink(link, *visitedLinks) {
return
}
(*visitedLinks)[link] = struct{}{}
slog.Info("Link found: ", link)
c.Visit(e.Request.AbsoluteURL(link))
}
func isVisitedLink(link string, visitedLinks map[string]struct{}) bool {
_, found := visitedLinks[link]
return found
}
func writeRecipesToJSON() {
if err := os.MkdirAll("recipes", os.ModePerm); err != nil {
slog.Error("Error creating directory:", err)
return
}
for _, recipe := range recipes {
sanitizedTitle := sanitizeFileName(recipe.Title)
fileName := fmt.Sprintf("recipes/%s.json", sanitizedTitle)
file, err := os.Create(fileName)
if err != nil {
slog.Error("Error creating file:", err)
continue
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(recipe); err != nil {
slog.Error("Error encoding JSON to file:", err)
}
}
}
func writeRecipeToJSON(recipe shared.Recipe) {
sanitizedTitle := sanitizeFileName(recipe.Title)
fileName := fmt.Sprintf("recipes/%s.json", sanitizedTitle)
file, err := os.Create(fileName)
if err != nil {
slog.Error("Error creating file:", err)
return
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(recipe); err != nil {
slog.Error("Error encoding JSON to file:", err)
}
}
func createRecipeDirectory() {
if err := os.MkdirAll("recipes", os.ModePerm); err != nil {
slog.Error("Error creating directory:", err)
return
}
}
func sanitizeFileName(name string) string {
name = strings.ReplaceAll(name, " ", "_")
reg := regexp.MustCompile("[^a-zA-Z0-9_]+")
return reg.ReplaceAllString(name, "")
}