From 6f872fe2f7fa2ee5cae749bdde5ad002d380b556 Mon Sep 17 00:00:00 2001 From: lost+skunk Date: Thu, 13 Jun 2024 23:56:03 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B0=D0=BB=D1=8C=D1=84=D0=B0:=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=82=20=D1=82=D0=BE=D0=BB=D1=8C?= =?UTF-8?q?=D0=BA=D0=BE=20=D0=BF=D0=BE=D1=81=D1=82=D1=8B=20=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=B8=D1=81=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config.go | 48 +++++++++++ app/router.go | 81 +++++++++++++++++++ app/wraper.go | 192 ++++++++++++++++++++++++++++++++++++++++++++ config.json | 12 +++ css/skunky.css | 81 +++++++++++++++++++ go.mod | 7 ++ go.sum | 0 html/about.htm | 23 ++++++ html/deviantion.htm | 44 ++++++++++ html/index.htm | 20 +++++ html/search.htm | 29 +++++++ main.go | 7 ++ 12 files changed, 544 insertions(+) create mode 100644 app/config.go create mode 100644 app/router.go create mode 100644 app/wraper.go create mode 100644 config.json create mode 100644 css/skunky.css create mode 100644 go.mod create mode 100644 go.sum create mode 100644 html/about.htm create mode 100644 html/deviantion.htm create mode 100644 html/index.htm create mode 100644 html/search.htm create mode 100644 main.go diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..9a5aef1 --- /dev/null +++ b/app/config.go @@ -0,0 +1,48 @@ +package app + +import ( + "os" +) + +type cache_config struct { + Enabled bool + Path string + Max_size, Lifetime int64 +} + +type config struct { + cfg string + Listen, Base_uri string + Cache cache_config + Proxy, Nsfw bool +} + +var CFG = config{ + cfg: "config.json", + Listen: "127.0.0.1:3003", + Base_uri: "/", + Cache: cache_config{ + Enabled: true, + Path: "cache", + }, + Proxy: true, + Nsfw: true, +} + +func execcfg() { + a := os.Args + for num, val := range a { + switch val { + case "-conf": + CFG.cfg = a[num] + case "-help": + println(`SkunkyArt v 1.3 [refactoring] +Usage: + - -conf - path to config + - -help this message +Example: + ./skunkyart -conf config.json +Copyright lost+skunk, X11. https://git.macaw.me/skunky/skunkyart/src/tag/v1.3`) + } + } +} diff --git a/app/router.go b/app/router.go new file mode 100644 index 0000000..b4af3f1 --- /dev/null +++ b/app/router.go @@ -0,0 +1,81 @@ +package app + +import ( + "io" + "net/http" + u "net/url" + "os" + "strconv" + "strings" +) + +const addr string = "0.0.0.0:3003" + +// роутер +func Router() { + // расшифровка эндпоинта из урл + endpoint := func(url string) (string, string) { + if CFG.Base_uri != "" { + url = strings.Replace(url, CFG.Base_uri, "/", 1) + } + + end := strings.Index(url[1:], "/") + if end == -1 { + return url[1:], "" + } + return url[1 : end+1], url[end+2:] + } + + // функция, что управляет всем + handle := func(w http.ResponseWriter, r *http.Request) { + e, url := endpoint(r.URL.Path) + var wr = io.WriteString + open_n_send := func(name string) { + f, e := os.ReadFile(name) + err(e) + wr(w, string(f)) + } + + // структура с функциями + var skunky skunkyart + skunky.Args = r.URL.Query() + skunky.Writer = w + + arg := skunky.Args.Get + skunky.Query = u.QueryEscape(arg("q")) + if t := arg("type"); len(t) > 0 { + skunky.Type = rune(t[0]) + } + p, _ := strconv.Atoi(arg("p")) + skunky.Page = p + + // пути + switch e { + default: + skunky.httperr(404) + case "/", "": + open_n_send("html/index.htm") + case "post": + slash := strings.Index(url, "/") + skunky.Deviation(url[:slash], url[slash+1:]) + case "search": + skunky.Search() + case "media": + skunky.Emojitar(url) + case "about": + open_n_send("html/about.htm") + case "gui": + w.Header().Add("content-type", "text/css") + open_n_send(url) + } + } + + http.HandleFunc("/", handle) + http.ListenAndServe(addr, nil) +} + +func err(e error) { + if e != nil { + println(e.Error()) + } +} diff --git a/app/wraper.go b/app/wraper.go new file mode 100644 index 0000000..d8a459f --- /dev/null +++ b/app/wraper.go @@ -0,0 +1,192 @@ +package app + +import ( + "bytes" + "io" + "net/http" + "net/url" + "regexp" + "strconv" + "strings" + "text/template" + + "git.macaw.me/skunky/devianter" +) + +var wr = io.WriteString + +type skunkyart struct { + Writer http.ResponseWriter + Args url.Values + Type rune + Query string + Page int +} + +// парсинг темплейтов +func (s skunkyart) exe(file string, data any) { + var buf bytes.Buffer + tmp, e := template.ParseFiles(file) + err(e) + tmp.Execute(&buf, &data) + wr(s.Writer, buf.String()) +} + +func (s skunkyart) httperr(status int) { + s.Writer.WriteHeader(status) + + // пострйока с помощью strings.Builder, потому что такой метод быстрее обычного сложения + var msg strings.Builder + msg.WriteString(``) + msg.WriteString("

") + msg.WriteString(strconv.Itoa(status)) + msg.WriteString(" - ") + msg.WriteString(http.StatusText(status)) + msg.WriteString("

") + + wr(s.Writer, msg.String()) +} + +func (s skunkyart) DeviationList(devs []devianter.Deviation) string { + var list strings.Builder + list.WriteString(`
`) + for _, data := range devs { + url := devianter.UrlFromMedia(data.Media) + + list.WriteString(`") + } + list.WriteString("
") + return list.String() +} + +// посты +func (s skunkyart) Deviation(author, postname string) { + // поиск ID + re := regexp.MustCompile("[0-9]+").FindAllString(postname, -1) + if len(re) >= 1 { + var post struct { + Post devianter.Post + StringTime string + Tags string + Comments string + } + + id := re[len(re)-1] + post.Post = devianter.DeviationFunc(id, author) + + // время публикации + post.StringTime = post.Post.Deviation.PublishedTime.UTC().String() + + println(post.Post.Description) + // хештэги + for _, x := range post.Post.Deviation.Extended.Tags { + var tag strings.Builder + tag.WriteString(` #`) + tag.WriteString(x.Name) + tag.WriteString("") + + post.Tags += tag.String() + } + + // генерация комментов + var cmmts strings.Builder + var replied map[int]string + _ = replied + c := devianter.CommentsFunc(id, post.Post.Comments.Cursor, s.Page, 1) + + cmmts.WriteString("
Comments: ") + cmmts.WriteString(strconv.Itoa(c.Total)) + cmmts.WriteString("") + for _, x := range c.Thread { + cmmts.WriteString(`

") + cmmts.WriteString(x.User.Username) + cmmts.WriteString(" ") + cmmts.WriteString(x.Posted.UTC().String()) + cmmts.WriteString("

") + cmmts.WriteString(x.Comment) + cmmts.WriteString("

👍: ") + cmmts.WriteString(strconv.Itoa(x.Likes)) + cmmts.WriteString(" ⏩: ") + cmmts.WriteString(strconv.Itoa(x.Replies)) + cmmts.WriteString("

\n") + } + cmmts.WriteString("
") + post.Comments = cmmts.String() + + s.exe("html/deviantion.htm", &post) + } else { + s.httperr(400) + } +} + +func (s skunkyart) Search() { + // тут всё и так понятно + if s.Type == 'a' || s.Type == 't' || s.Type == 'g' { + var srch struct { + Search devianter.Search + List string + } + + var e error + srch.Search, e = devianter.SearchFunc(s.Query, s.Page, s.Type) + err(e) + srch.List = s.DeviationList(srch.Search.Results) + + s.exe("html/search.htm", &srch) + } else { + s.httperr(400) + } +} + +func (s skunkyart) Emojitar(name string) { + if name != "" && (s.Type == 'a' || s.Type == 'e') { + ae, e := devianter.AEmedia(name, s.Type) + if e != nil { + s.httperr(404) + println(e.Error()) + } + wr(s.Writer, ae) + } else { + s.httperr(400) + } +} diff --git a/config.json b/config.json new file mode 100644 index 0000000..1d979bd --- /dev/null +++ b/config.json @@ -0,0 +1,12 @@ +{ + "listen": "0.0.0.0:3003", + "base_uri": null, + "cache": { + "enabled": true, + "path": "cache", + "lifetime": null, + "max_size": 10000 + }, + "proxy": false, + "nsfw": false +} \ No newline at end of file diff --git a/css/skunky.css b/css/skunky.css new file mode 100644 index 0000000..ef281ce --- /dev/null +++ b/css/skunky.css @@ -0,0 +1,81 @@ +html { + font-family: ubuntu; + background-color:black; + color: rgb(234, 216, 216); +} +a { + text-decoration: none; + color: cadetblue; +} +a:hover { + color: red; + transition: 400ms; +} +header h1 { + padding-right: 0.2%; +} +header form { + align-self: center; +} +header { + display: flex; +} +form input, button, select { + background-color: #134134; + padding: 5px; + color: whitesmoke; + border: 0px; + border-radius: 1px; +} +.nsfw, .true { + color: red; +} +.author { + color: seagreen; +} +.msg { + background-color: #091f19; + color: whitesmoke; + width: fit-content; + max-width: 90%; + padding: 4px; + border-radius: 2px; + margin-top: 6px; + text-wrap: pretty; + transition: 350ms; +} +.msg:hover { + background-color: #134134; +} +.dd { + color: rgb(160, 0, 147); +} +.content { + border-radius: 3px; + display: flex; + flex-wrap: wrap; + justify-content: center; +} +.block { + max-width: 20%; + height: 0%; + padding: 4px; + border-radius: 2px; + border: 3px solid #091f19; + word-break: break-all; + background-color: #091f19; + margin-left: 5px; + margin-top: 5px; + text-align: center; +} +.block:hover { + border: 3px solid #4d27d6; + transition: 400ms; +} +.block img { + width: 100%; +} +.block p { + word-break: break-all; +} + diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ac1e0d0 --- /dev/null +++ b/go.mod @@ -0,0 +1,7 @@ +module skunkyart + +go 1.22.3 + +replace git.macaw.me/skunky/devianter v0.1.0 => /home/skunk/projects/devianter + +require git.macaw.me/skunky/devianter v0.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/html/about.htm b/html/about.htm new file mode 100644 index 0000000..2c43bbe --- /dev/null +++ b/html/about.htm @@ -0,0 +1,23 @@ + + + + SkunkyArt + + +
+
+

HOME | DD

+
+ + + +
+
+

+ SkunkyArt is an alternative frontend for deviantart.com, written in Go. +

+
+ \ No newline at end of file diff --git a/html/deviantion.htm b/html/deviantion.htm new file mode 100644 index 0000000..8516965 --- /dev/null +++ b/html/deviantion.htm @@ -0,0 +1,44 @@ + + + + SkunkyArt | {{.Post.Deviation.Author.Username}} - {{.Post.Deviation.Title}} + + +
+
+

HOME | DD

+
+ + + +
+
+
+ + {{.Post.Deviation.Author.Username}} — {{if (.Post.Deviation.DD)}} + {{.Post.Deviation.Title}} + {{else}}{{.Post.Deviation.Title}}{{end}} + {{if (ne .Post.Deviation.License "none")}}{{.Post.Deviation.License}}{{end}} {{if (.Post.Deviation.AI)}}[🤖]{{end}} + {{if (.Post.Deviation.NSFW)}}[NSFW]{{end}} + +
+ {{if (ne .Post.IMG "")}} + +
+ {{end}} + {{if (ne .Tags "")}} + {{.Tags}}
+ {{end}} + Published: {{.StringTime}}; Views: {{.Post.Deviation.Stats.Views}}; Favourites: {{.Post.Deviation.Stats.Favourites}}; Downloads: {{.Post.Deviation.Stats.Downloads}} + {{if (ne .Post.Description "")}} + {{.Post.Description}} + {{end}} + {{if (ne .Comments "")}} + {{.Comments}} + {{end}} +
+
+ \ No newline at end of file diff --git a/html/index.htm b/html/index.htm new file mode 100644 index 0000000..e754884 --- /dev/null +++ b/html/index.htm @@ -0,0 +1,20 @@ + + + + SkunkyArt + +
+
+
+ + + +
+

Daily Deviations | About | Source Code

+
+
+ \ No newline at end of file diff --git a/html/search.htm b/html/search.htm new file mode 100644 index 0000000..4ef9fbd --- /dev/null +++ b/html/search.htm @@ -0,0 +1,29 @@ + + + + SkunkyArt | Search + + +
+
+

HOME | DD

+
+ + + +
+
+ {{if (ne .List "")}} + {{if (ne .Search.Total 0)}} +

Total resuls: {{.Search.Total}}

+ {{end}} + {{.List}} + {{.Search.Pages}} + {{else}} +

No results :(

+ {{end}} +
+ \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..412f4fa --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "skunkyart/app" + +func main() { + app.Router() +}