From 14645842643c4e5fa542def715ac1629e182388c Mon Sep 17 00:00:00 2001 From: lost+skunk Date: Mon, 2 Sep 2024 11:16:54 +0300 Subject: instance list update --- app/api.go | 34 ++++++++++++++++++++++++++++++++++ app/cache.go | 31 +++++++++++++++++-------------- app/parsers.go | 6 ++++-- app/router.go | 10 +++++++++- app/util.go | 9 +++++---- app/wrapper.go | 6 +++--- 6 files changed, 72 insertions(+), 24 deletions(-) create mode 100644 app/api.go (limited to 'app') diff --git a/app/api.go b/app/api.go new file mode 100644 index 0000000..ddc67c1 --- /dev/null +++ b/app/api.go @@ -0,0 +1,34 @@ +package app + +import ( + "math/rand" + "strings" + + "git.macaw.me/skunky/devianter" +) + +type API struct { + skunkyartLink *skunkyart +} + +func (a API) sendMedia(d *devianter.Deviation) { + mediaUrl, name := devianter.UrlFromMedia(d.Media) + + var filename strings.Builder + filename.WriteString(`filename="`) + filename.WriteString(name) + filename.WriteString(`"`) + a.skunkyartLink.Writer.Header().Add("Content-Disposition", filename.String()) + + if len(mediaUrl) != 0 { + mediaUrl = mediaUrl[21:] + dot := strings.Index(mediaUrl, ".") + a.skunkyartLink.DownloadAndSendMedia(mediaUrl[:dot], mediaUrl[dot+11:]) + } +} + +func (a API) Random() { + s, err := devianter.PerformSearch(string(rand.Intn(999)), rand.Intn(30), 'a') + try(err) + a.sendMedia(&s.Results[rand.Intn(len(s.Results))]) +} diff --git a/app/cache.go b/app/cache.go index 6506cfc..cbf2432 100644 --- a/app/cache.go +++ b/app/cache.go @@ -26,9 +26,11 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) { url.WriteString(subdomain) url.WriteString(".wixmp.com/") url.WriteString(path) - url.WriteString("?token=") - url.WriteString(s.Args.Get("token")) - + if t := s.Args.Get("token"); t != "" { + url.WriteString("?token=") + url.WriteString(t) + } + var response []byte switch { @@ -40,17 +42,18 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) { if tempFS[fileName] == nil { tempFS[fileName] = &file{} } - f := *tempFS[fileName] mx.Unlock() - if f.Content != nil { - f.Score += 2 + if tempFS[fileName].Content != nil { + response = tempFS[fileName].Content + tempFS[fileName].Score += 2 + break } else { file, err := os.Open(filePath) if err != nil { if dwnld := Download(url.String()); dwnld.Status == 200 && dwnld.Headers["Content-Type"][0][:5] == "image" { - f.Content = dwnld.Body - try(os.WriteFile(filePath, f.Content, 0700)) + response = dwnld.Body + try(os.WriteFile(filePath, response, 0700)) } else { s.ReturnHTTPError(dwnld.Status) return @@ -58,11 +61,16 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) { } else { file, e := io.ReadAll(file) try(e) - f.Content = file + response = file } go func() { defer restore() + + mx.RLock() + tempFS[fileName].Content = response + mx.RUnlock() + for { time.Sleep(1 * time.Minute) @@ -77,11 +85,6 @@ func (s skunkyart) DownloadAndSendMedia(subdomain, path string) { } }() } - - mx.Lock() - tempFS[fileName] = &f - mx.Unlock() - response = f.Content case CFG.Proxy: dwnld := Download(url.String()) if dwnld.Status != 200 { diff --git a/app/parsers.go b/app/parsers.go index f93c18d..3a318be 100644 --- a/app/parsers.go +++ b/app/parsers.go @@ -82,8 +82,9 @@ func (s skunkyart) DeviationList(devs []devianter.Deviation, allowAtom bool, con for i, l := 0, len(devs); i < l; i++ { data := &devs[i] - if preview, fullview := ParseMedia(data.Media, data.Title, 320), ParseMedia(data.Media, data.Title); !(data.NSFW && !CFG.Nsfw) { + if preview, fullview := ParseMedia(data.Media, 320), ParseMedia(data.Media); !(data.NSFW && !CFG.Nsfw) { if allowAtom && s.Atom { + s.Writer.Header().Add("Content-type", "application/atom+xml") id := strconv.Itoa(data.ID) listContent.WriteString(``) listContent.WriteString(data.Author.Username) @@ -183,6 +184,7 @@ type text struct { To int } +// переписать весь этот пиздец нахуй func ParseDescription(dscr devianter.Text) string { var parsedDescription strings.Builder TagBuilder := func(content string, tags ...string) string { @@ -286,7 +288,7 @@ func ParseDescription(dscr devianter.Text) string { parsedDescription.WriteString(``) @@ -185,7 +185,7 @@ func (s skunkyart) Deviation(author, postname string) { } // время публикации post.StringTime = post.Post.Deviation.PublishedTime.UTC().String() - post.Post.IMG = ParseMedia(post.Post.Deviation.Media, post.Post.Deviation.Title) + post.Post.IMG = ParseMedia(post.Post.Deviation.Media) for _, x := range post.Post.Deviation.Extended.RelatedContent { if len(x.Deviations) != 0 { post.Related += s.DeviationList(x.Deviations, false) -- cgit v1.2.3