initial commit

This commit is contained in:
rramiachraf 2022-06-30 21:32:56 +01:00
commit 7b2c43cc16
13 changed files with 285 additions and 0 deletions

13
go.mod Normal file
View File

@ -0,0 +1,13 @@
module github.com/rramiachraf/dumb
go 1.18
require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/gorilla/mux v1.8.0
)
require (
github.com/andybalholm/cascadia v1.3.1 // indirect
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 // indirect
)

13
go.sum Normal file
View File

@ -0,0 +1,13 @@
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk=
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

81
lyrics.go Normal file
View File

@ -0,0 +1,81 @@
package main
import (
"fmt"
"log"
"net/http"
"path"
"text/template"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
)
type song struct {
Artist string
Title string
Image string
Lyrics string
}
func (s *song) parseLyrics(doc *goquery.Document) {
doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) {
h, err := ss.Html()
if err != nil {
log.Println(err)
}
s.Lyrics += h
})
}
func (s *song) parseMetadata(doc *goquery.Document) {
artist := doc.Find("a[class*='Artist']").First().Text()
title := doc.Find("h1[class*='Title']").First().Text()
image, exists := doc.Find("meta[property='og:image']").Attr("content")
if exists {
s.Image = image
}
s.Title = title
s.Artist = artist
}
func (s *song) parse(doc *goquery.Document) {
s.parseLyrics(doc)
s.parseMetadata(doc)
}
func lyricsHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
url := fmt.Sprintf("https://genius.com/%s-lyrics", id)
resp, err := http.Get(url)
if err != nil {
write(w, http.StatusInternalServerError, []byte("can't reach genius servers"))
return
}
if resp.StatusCode == http.StatusNotFound {
write(w, http.StatusNotFound, []byte("Not found"))
return
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
write(w, http.StatusInternalServerError, []byte("something went wrong"))
return
}
var s song
s.parse(doc)
w.Header().Set("content-type", "text/html")
t, err := template.ParseFiles(path.Join("views", "lyrics.tmpl"))
if err != nil {
write(w, http.StatusInternalServerError, []byte("something went wrong"))
return
}
t.Execute(w, s)
}

54
main.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"fmt"
"log"
"net"
"net/http"
"os"
"strconv"
"time"
"github.com/gorilla/mux"
)
func fatal(err any) {
log.Fatalf("[ERR] %s\n", err)
}
func info(s string) {
log.Printf("[INFO] %s\n", s)
}
func write(w http.ResponseWriter, status int, data []byte) {
w.WriteHeader(status)
w.Write(data)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/{id}-lyrics", lyricsHandler)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
server := &http.Server{
Handler: r,
WriteTimeout: 10 * time.Second,
ReadTimeout: 10 * time.Second,
}
port, _ := strconv.Atoi(os.Getenv("PORT"))
if port == 0 {
port = 5555
}
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
fatal(err)
}
info(fmt.Sprintf("server is listening on port %d", port))
fatal(server.Serve(l))
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

7
static/script.js Normal file
View File

@ -0,0 +1,7 @@
document.querySelectorAll("#lyrics > a").forEach(item => {
item.addEventListener("click", getAnnotation)
})
function getAnnotation(e) {
e.preventDefault()
}

99
static/style.css Normal file
View File

@ -0,0 +1,99 @@
/* inter-regular - latin */
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 400;
src: local(''),
url('/static/fonts/inter-v11-latin-regular.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
url('/static/fonts/inter-v11-latin-regular.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
/* inter-500 - latin */
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 500;
src: local(''),
url('/static/fonts/inter-v11-latin-500.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
url('/static/fonts/inter-v11-latin-500.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
/* inter-700 - latin */
@font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 700;
src: local(''),
url('/static/fonts/inter-v11-latin-700.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
url('/static/fonts/inter-v11-latin-700.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
* {
margin: 0;
padding: 0;
}
html {
font-size: 62.5%;
}
body {
font-size: 1.5rem;
font-family: inter;
background-color: #f9f9f9;
}
#lyrics {
position: absolute;
left: 50%;
transform: translateX(-50%);
padding: 1rem 0;
color: #171717;
line-height: 2.5rem;
}
#lyrics a {
background-color: #ddd;
color: inherit;
}
#nav {
font-size: 2.5rem;
text-align: center;
font-weight: 700;
background-color: #FFCD38;
color: #1B1A17;
padding: 0.5rem;
letter-spacing: 1rem;
font-family: monospace;
}
a {
text-decoration: none;
}
#metadata {
display: flex;
flex-direction: column;
align-items: center;
gap: 0.5rem;
padding: 1rem;
}
#metadata h1 {
font-size: 2.2rem;
color: #171717;
}
#metadata h2 {
font-size: 1.5rem;
color: #1E1E1E;
text-transform: uppercase;
font-weight: 500;
}
#metadata > img {
width: 20rem;
border-radius: 3px;
}

18
views/lyrics.tmpl Normal file
View File

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title>{{.Artist}} - {{.Title}} lyrics</title>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="/static/style.css" />
<script type="text/javascript" src="/static/script.js" async></script>
</head>
<body>
<h1 id="nav">DUMB</h1>
<div id="metadata">
<img src="{{.Image}}"/>
<h2>{{.Artist}}</h2>
<h1>{{.Title}}</h1>
</div>
<div id="lyrics">{{.Lyrics}}</div>
</body>
</html>