2024-03-04 14:59:47 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2024-03-05 22:01:32 +01:00
|
|
|
"strings"
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-03-05 22:01:32 +01:00
|
|
|
"github.com/PuerkitoBio/goquery"
|
2024-03-04 14:59:47 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/rramiachraf/dumb/data"
|
2024-05-02 21:29:50 +01:00
|
|
|
"github.com/rramiachraf/dumb/utils"
|
2024-03-04 14:59:47 +01:00
|
|
|
"github.com/rramiachraf/dumb/views"
|
|
|
|
)
|
|
|
|
|
2024-05-02 21:29:50 +01:00
|
|
|
func annotations(l *utils.Logger) http.HandlerFunc {
|
2024-03-04 14:59:47 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-04-06 00:14:51 -06:00
|
|
|
id := mux.Vars(r)["annotation-id"]
|
2024-03-04 20:43:46 +01:00
|
|
|
if a, err := getCache[data.Annotation]("annotation:" + id); err == nil {
|
2024-03-04 19:49:03 +01:00
|
|
|
encoder := json.NewEncoder(w)
|
2024-03-04 14:59:47 +01:00
|
|
|
|
|
|
|
w.Header().Set("content-type", "application/json")
|
2024-03-04 20:43:46 +01:00
|
|
|
if err = encoder.Encode(&a); err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error(err.Error())
|
2024-03-04 14:59:47 +01:00
|
|
|
}
|
2024-03-04 20:43:46 +01:00
|
|
|
|
2024-03-04 14:59:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://genius.com/api/referents/%s?text_format=html", id)
|
2024-05-03 12:45:58 +01:00
|
|
|
resp, err := utils.SendRequest(url)
|
2024-03-04 14:59:47 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error(err.Error())
|
2024-03-04 14:59:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
views.ErrorPage(500, "cannot reach genius servers").Render(context.Background(), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
views.ErrorPage(404, "page not found").Render(context.Background(), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = buf.ReadFrom(resp.Body)
|
|
|
|
if err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error("Error paring genius api response: %s", err.Error())
|
2024-03-04 14:59:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var data data.AnnotationsResponse
|
|
|
|
err = json.Unmarshal(buf.Bytes(), &data)
|
|
|
|
if err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error("could not unmarshal json: %s\n", err)
|
2024-03-04 14:59:47 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
views.ErrorPage(500, "something went wrong").Render(context.Background(), w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
body := data.Response.Referent.Annotations[0].Body
|
2024-03-04 20:43:46 +01:00
|
|
|
body.HTML = cleanBody(body.HTML)
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-03-04 20:43:46 +01:00
|
|
|
w.Header().Set("content-type", "application/json")
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
|
|
|
|
if err = encoder.Encode(&body); err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error("Error sending response: %s", err.Error())
|
2024-03-04 14:59:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-04 20:43:46 +01:00
|
|
|
if err = setCache("annotation:"+id, body); err != nil {
|
2024-05-02 21:29:50 +01:00
|
|
|
l.Error(err.Error())
|
2024-03-04 18:46:23 +01:00
|
|
|
}
|
2024-03-04 14:59:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanBody(body string) string {
|
2024-03-05 22:01:32 +01:00
|
|
|
if doc, err := goquery.NewDocumentFromReader(strings.NewReader(body)); err == nil {
|
|
|
|
doc.Find("iframe").Each(func(i int, s *goquery.Selection) {
|
|
|
|
src, exists := s.Attr("src")
|
|
|
|
if exists {
|
|
|
|
html := fmt.Sprintf(`<a id="iframed-link" href="%s">Link</a>`, src)
|
|
|
|
s.ReplaceWithHtml(html)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
doc.Find("img").Each(func(i int, s *goquery.Selection) {
|
|
|
|
src, exists := s.Attr("src")
|
|
|
|
if exists {
|
|
|
|
re := regexp.MustCompile(`(?i)https:\/\/images\.(rapgenius|genius)\.com\/`)
|
|
|
|
pSrc := re.ReplaceAllString(src, "/images/")
|
|
|
|
s.SetAttr("src", pSrc)
|
|
|
|
}
|
|
|
|
})
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-03-05 22:01:32 +01:00
|
|
|
if source, err := doc.Html(); err == nil {
|
|
|
|
body = source
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-03-05 22:01:32 +01:00
|
|
|
re := regexp.MustCompile(`https?:\/\/[a-z]*.?genius.com`)
|
|
|
|
return re.ReplaceAllString(body, "")
|
2024-03-04 14:59:47 +01:00
|
|
|
}
|