Skip to content

Commit

Permalink
feat: use cache file html
Browse files Browse the repository at this point in the history
  • Loading branch information
hblab-ngocnd committed Aug 30, 2022
1 parent 123b649 commit a283122
Show file tree
Hide file tree
Showing 7 changed files with 402 additions and 1 deletion.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN apk --no-cache add curl
WORKDIR /root/
COPY --from=0 go/src/app/main .
COPY --from=0 go/src/app/static static
COPY --from=0 go/src/app/cache cache
COPY --from=0 go/src/app/public/views public/views
EXPOSE 80
CMD ["./main"]
Expand Down
77 changes: 77 additions & 0 deletions cache/jlpt-n1-vocabulary-list

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions cache/jlpt-n2-vocabulary-list

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions cache/jlpt-n3-vocabulary-list

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions cache/jlpt-n4-vocabulary-list

Large diffs are not rendered by default.

77 changes: 77 additions & 0 deletions cache/jlpt-n5-vocabulary-list

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion services/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"path"
"strings"
"time"

Expand Down Expand Up @@ -45,12 +46,25 @@ func (d *dictionaryService) GetDictionary(ctx context.Context, url string) ([]mo
if err != nil {
log.Fatal(err)
}
log.Println(string(body))
//log.Println(string(body))
elms := strings.Split(url, "/")
fileName := elms[len(elms)-2]
doc, err := html.Parse(bytes.NewReader(body))
if err != nil {
log.Fatal(err)
}
tag := helpers.GetElementByClass(doc, "entry clearfix")
if tag == nil {
body, err = os.ReadFile(path.Join("cache", fileName))
if err != nil {
return nil, nil
}
doc, err = html.Parse(bytes.NewReader(body))
if err != nil {
log.Fatal(err)
}
tag = helpers.GetElementByClass(doc, "entry clearfix")
}
if tag == nil {
log.Println("can't get entry clearfix")
return nil, nil
Expand All @@ -60,6 +74,7 @@ func (d *dictionaryService) GetDictionary(ctx context.Context, url string) ([]mo
log.Println("can't get entry clearfix p")
return nil, nil
}
os.WriteFile(path.Join("cache", fileName), body, 0666)
if len(targets) > 2 {
targets = targets[2:]
}
Expand Down

0 comments on commit a283122

Please sign in to comment.