Skip to content

Commit 60bd625

Browse files
authored
feat/mal-cache-check (#3)
* feat: add secrets.json.example * feat: add mal cache checker * fix(build): update goreleaser skip flag * fix(build): goreleaser args
1 parent b6a4cfa commit 60bd625

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
with:
5555
distribution: goreleaser
5656
version: latest
57-
args: release --clean --skip-validate --skip-publish --parallelism 99
57+
args: release --clean --skip=validate,publish --parallelism 99
5858
env:
5959
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6060

cmd/shinkrodb/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func main() {
2626
switch cmd := pflag.Arg(0); cmd {
2727
case "run":
2828
cfg := config.NewConfig()
29+
domain.CleanCache("./mal_cache")
2930
domain.GetMalIds(cfg)
3031
domain.ScrapeMal()
3132
domain.GetTvdbIDs()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.20
55
require (
66
github.com/gocolly/colly v1.2.0
77
github.com/spf13/pflag v1.0.5
8+
golang.org/x/net v0.9.0
89
gopkg.in/yaml.v3 v3.0.1
910
)
1011

@@ -21,7 +22,6 @@ require (
2122
github.com/kennygrant/sanitize v1.2.4 // indirect
2223
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
2324
github.com/temoto/robotstxt v1.1.2 // indirect
24-
golang.org/x/net v0.9.0 // indirect
2525
golang.org/x/text v0.9.0 // indirect
2626
google.golang.org/appengine v1.6.7 // indirect
2727
google.golang.org/protobuf v1.30.0 // indirect

internal/domain/cache.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package domain
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"regexp"
10+
"strconv"
11+
"strings"
12+
"sync"
13+
"time"
14+
15+
"golang.org/x/net/html"
16+
)
17+
18+
var (
19+
delCount int
20+
nilMatches []string
21+
mutex sync.Mutex
22+
)
23+
24+
func CleanCache(rootDir string) {
25+
var wg sync.WaitGroup
26+
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
27+
if err != nil {
28+
return err
29+
}
30+
31+
if !info.IsDir() {
32+
wg.Add(1)
33+
go func(path string) {
34+
defer wg.Done()
35+
doc := readFile(path)
36+
if shouldDelete(doc, path) {
37+
mutex.Lock()
38+
delCount++
39+
mutex.Unlock()
40+
removeFile(path)
41+
}
42+
}(path)
43+
}
44+
return nil
45+
})
46+
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
51+
wg.Wait()
52+
53+
fmt.Println("Unable to check the following html pages in cache:")
54+
for _, v := range nilMatches {
55+
fmt.Println(v)
56+
}
57+
58+
fmt.Println("Total number of files deleted: ", delCount)
59+
}
60+
61+
func removeFile(path string) {
62+
err := os.Remove(path)
63+
if err != nil {
64+
log.Fatalf("Failed to delete file %s: %v", path, err)
65+
}
66+
}
67+
68+
func readFile(path string) string {
69+
f, err := os.Open(path)
70+
if err != nil {
71+
log.Fatal(err)
72+
}
73+
74+
defer f.Close()
75+
76+
d, err := html.Parse(f)
77+
if err != nil {
78+
log.Fatal(err)
79+
}
80+
81+
var b bytes.Buffer
82+
err = html.Render(&b, d)
83+
if err != nil {
84+
log.Fatal(err)
85+
}
86+
87+
doc := b.String()
88+
return doc
89+
}
90+
91+
func shouldDelete(doc, path string) bool {
92+
return checkTypeAndYear(doc, path) && !checkAnidb(doc)
93+
}
94+
95+
func checkAnidb(doc string) bool {
96+
return strings.Contains(doc, "external-links-anime-pc-anidb")
97+
98+
}
99+
100+
// returns true if the anime is from the current year and it's type is tv
101+
func checkTypeAndYear(doc, path string) bool {
102+
exp := `[\S\s]*(?:https://myanimelist\.net/topanime\.php\?type=(\w+)|Type:</span>\n\s{0,}(\w+)\s{0,}</div>)[\S\s\v]*(?:Aired:</span>\n\s+.*(\d{4}))`
103+
re := regexp.MustCompile(exp)
104+
match := re.FindStringSubmatch(doc)
105+
106+
if match == nil {
107+
nilMatches = append(nilMatches, path)
108+
return false
109+
}
110+
111+
showType := match[1]
112+
113+
if showType == "" {
114+
showType = match[2]
115+
}
116+
117+
currentYear := strconv.Itoa(time.Now().Year())
118+
return showType == "tv" && match[3] == currentYear
119+
}

secrets.json.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Enter your id and key then rename this file to secrets.json
2+
3+
{
4+
"mal-client-id": "",
5+
"tmdb-api-key": ""
6+
}

0 commit comments

Comments
 (0)