2024-03-04 14:59:47 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/rramiachraf/dumb/data"
|
|
|
|
"github.com/rramiachraf/dumb/views"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Annotations(l *logrus.Logger) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := mux.Vars(r)["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-03-04 19:49:03 +01:00
|
|
|
l.Errorln(err)
|
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)
|
|
|
|
resp, err := sendRequest(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
l.Errorln(err)
|
|
|
|
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 {
|
|
|
|
l.Errorln("Error paring genius api response", err)
|
|
|
|
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 {
|
|
|
|
l.Errorf("could not unmarshal json: %s\n", err)
|
|
|
|
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 {
|
|
|
|
l.Errorln("Error sending response: ", err)
|
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-03-04 18:46:23 +01:00
|
|
|
l.Errorln(err)
|
|
|
|
}
|
2024-03-04 14:59:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanBody(body string) string {
|
2024-03-04 23:34:46 +01:00
|
|
|
re := regexp.MustCompile(`(?i)https:\/\/images\.(rapgenius|genius)\.com\/`)
|
|
|
|
clean := re.ReplaceAllString(body, "/images/")
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-03-04 23:34:46 +01:00
|
|
|
re = regexp.MustCompile(`https?:\/\/[a-z]*.?genius.com`)
|
|
|
|
clean = re.ReplaceAllString(clean, "")
|
2024-03-04 14:59:47 +01:00
|
|
|
|
2024-03-04 23:34:46 +01:00
|
|
|
return clean
|
2024-03-04 14:59:47 +01:00
|
|
|
}
|