From 0291c4631b4f3f7622cdb19f0a036a615198f5a0 Mon Sep 17 00:00:00 2001 From: Tanat Date: Wed, 12 Jun 2024 19:22:11 +0700 Subject: [PATCH 1/2] feat: added backup script --- .gitignore | 3 + scripts/backup/go.mod | 3 + scripts/backup/main.go | 148 ++++++++++++++++++ src/content/blog/fuzzy.md | 28 ++-- ...periences-as-an-backend-intern-at-amity.md | 2 +- 5 files changed, 169 insertions(+), 15 deletions(-) create mode 100644 scripts/backup/go.mod create mode 100644 scripts/backup/main.go diff --git a/.gitignore b/.gitignore index 6240da8..4a150b2 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ pnpm-debug.log* # macOS-specific files .DS_Store + +# backup medias +scripts/backup/*medias diff --git a/scripts/backup/go.mod b/scripts/backup/go.mod new file mode 100644 index 0000000..e48bf1d --- /dev/null +++ b/scripts/backup/go.mod @@ -0,0 +1,3 @@ +module github.com/RiwEz/TanatBlog/backup + +go 1.22.1 diff --git a/scripts/backup/main.go b/scripts/backup/main.go new file mode 100644 index 0000000..e92984a --- /dev/null +++ b/scripts/backup/main.go @@ -0,0 +1,148 @@ +package main + +import ( + "errors" + "fmt" + "io" + "net/http" + "os" + "regexp" + "strings" + "time" +) + +type Pair[T, U any] struct { + Fst T + Snd U +} + +type Media = Pair[string, []byte] + +func GetFileName(url string) (string, error) { + for i := len(url) - 1; i >= 0; i-- { + if url[i] == '/' { + return url[i+1:], nil + } + } + + return "", errors.New("The URL is not valid") +} + +func FindUrls(content []byte, pattern string) []string { + urls := []string{} + + r, err := regexp.Compile(pattern) + if err != nil { + panic(err) + } + for _, match := range r.FindAllSubmatch(content, -1) { + // we have only one capture group, get it at index 1 + urls = append(urls, string(match[1])) + } + + return urls +} + +func GetMediasUrl(content []byte) []string { + urls := []string{} + + urls = append(urls, FindUrls(content, `]*src="([^"]+)"`)...) + urls = append(urls, FindUrls(content, `]*src="([^"]+)"`)...) + urls = append(urls, FindUrls(content, `!\[.*?\]\(([^\)]+)`)...) + + return urls +} + +func DownloadMedias(urls []string) ([]Media, error) { + results := []Pair[string, []byte]{} + client := &http.Client{} + + for _, url := range urls { + filename, err := GetFileName(url) + if err != nil { + return nil, err + } + + req, err := http.NewRequest("GET", url, nil) + if err != nil { + return nil, err + } + req.Header.Set("Connection", "keep-alive") + req.Header.Set("User-Agent", "TanatBlog/0.0.1") + + resp, err := client.Do(req) + if err != nil { + return nil, err + } + + bytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + results = append(results, Media{filename, bytes}) + + // sleep for 100 ms, if we recently used imgur + if strings.Contains(url, "imgur") { + time.Sleep(100 * time.Millisecond) + } + } + + return results, nil +} + +func WriteMedias(dir string, medias []Media) error { + if _, err := os.Stat(dir); err != nil { + if os.IsNotExist(err) { + err := os.MkdirAll(dir, 0777) + if err != nil { + return err + } + } else { + return err + } + } + + for _, v := range medias { + err := os.WriteFile(dir+v.Fst, v.Snd, 0666) + if err != nil { + return err + } + } + return nil +} + +func main() { + blogsPath := "../../src/content/blog/" + + dir, err := os.Open(blogsPath) + if err != nil { + panic(err) + } + files, err := dir.Readdirnames(-1) + if err != nil { + panic(err) + } + + currTime := time.Now().Format("2006-01-02T15:04:05") + backupsPath := currTime + "-medias/" + for _, file := range files { + fmt.Printf("backuping for" + file + ": ") + + content, err := os.ReadFile(blogsPath + file) + if err != nil { + fmt.Println(err) + } + + urls := GetMediasUrl(content) + medias, err := DownloadMedias(urls) + if err != nil { + fmt.Println(err) + } + err = WriteMedias(backupsPath+file+"/", medias) + if err != nil { + fmt.Println(err) + } + + fmt.Printf("success\n") + } +} diff --git a/src/content/blog/fuzzy.md b/src/content/blog/fuzzy.md index 0d0a8d5..ca80be6 100644 --- a/src/content/blog/fuzzy.md +++ b/src/content/blog/fuzzy.md @@ -39,7 +39,7 @@ are vauge and lack certainty. We think that Fuzzy Logic can help improving technical indicator usage.
- +
Simple example of Fuzzy Logic used with RSI @@ -77,7 +77,7 @@ from my friend (yes, it's in Thai). Right now, I'm currently not hosting the web not visit it.
- +
Architecture Overview @@ -172,7 +172,7 @@ We can think of linguistic variable as a collection of fuzzy sets with the same to describe fuzzy system inputs and outputs.
- +
Linguistic Variable Example @@ -283,7 +283,7 @@ The idea is TradingView already has a good API interface for creating varios tec PineScript, we can actually just copy the API interface and implemented its logic ourself.
- +
Technical Indicator Example (RSI) @@ -344,7 +344,7 @@ happen in so many more places in our code. #### Web Server
- +
Overview of how each thread work together. @@ -602,7 +602,7 @@ gain more profit while having low drawdown. Actually, we could change the object it better in other aspects but this idea should be in further development.
- +
Parameters of the linguistic variable that we will tune via PSO (a, b, c)
@@ -652,7 +652,7 @@ I didn't talk about. If you want to, you can checkout the code at ### Frontend
- +
One page of our website with candlestick graph of the market and our fuzzy technical indicator.
@@ -710,7 +710,7 @@ UI/UX thing. If you are interested, you can check out our [frontend repoitory](h ## Experiments & Results
- +
Variations of indicators we used.
@@ -751,21 +751,21 @@ I'll omit the details about the linguistic variables and fuzzy rules of fuzzy va idea is simple, it's very similar to classical one but we are using fuzzy sets instead.
- +
AROON-MACD backtesting result on BTC, ETH, BNB while market is in uptrend.
- +
AROON-MACD backtest result on ETH while market is in downtrend.
- +
AROON-MACD backtest result on ETH while market is in sideway.
@@ -786,21 +786,21 @@ I'll omit the details about the linguistic variables and fuzzy rules of fuzzy va the same reason on AROON-MACD too.
- +
RSI-BB backtesting result on BTC, ETH, BNB while market is in uptrend.
- +
RSI-BB backtest result on ETH while market is in downtrend.
- +
RSI-BB backtest result on ETH while market is in sideway.
diff --git a/src/content/blog/my-experiences-as-an-backend-intern-at-amity.md b/src/content/blog/my-experiences-as-an-backend-intern-at-amity.md index 65a3f75..1c3f674 100644 --- a/src/content/blog/my-experiences-as-an-backend-intern-at-amity.md +++ b/src/content/blog/my-experiences-as-an-backend-intern-at-amity.md @@ -49,7 +49,7 @@ And the works I do are mainly ### The new technology that I learned -![tech](https://imgur.com/CeBkIAn.png) +![tech](https://i.imgur.com/CeBkIAn.png) I have little experience writing a backend in JS/TS (express 🤣) and I still don't think it's a good choice to write backend in JS/TS because we don't have From 99cd79fbf47955cbf3053ccfdc07ab796ff6cbd6 Mon Sep 17 00:00:00 2001 From: Tanat Date: Wed, 12 Jun 2024 20:13:01 +0700 Subject: [PATCH 2/2] feat: add plausible, and change how we loaded our font --- src/layouts/MainLayout.astro | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/layouts/MainLayout.astro b/src/layouts/MainLayout.astro index 44a4639..0dbf22f 100644 --- a/src/layouts/MainLayout.astro +++ b/src/layouts/MainLayout.astro @@ -22,8 +22,18 @@ const { title, description } = Astro.props; + + + + +