Skip to content

Commit

Permalink
feat: first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
izn committed Jun 9, 2024
0 parents commit 0133838
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.24'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
go.work.sum
63 changes: 63 additions & 0 deletions genius.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"html"
)

func FetchLyrics(track Track) (string, error) {
url := buildGeniusURL(track)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

lyrics, err := extractLyrics(string(body))
if err != nil {
return "", err
}

return lyrics, nil
}

func buildGeniusURL(track Track) string {
artist := cleanString(track.Artist)
title := cleanString(track.Title)

artistSlug := strings.ReplaceAll(artist, " ", "-")
titleSlug := strings.ReplaceAll(title, " ", "-")

artistEncoded := url.PathEscape(artistSlug)
titleEncoded := url.PathEscape(titleSlug)

geniusURL := fmt.Sprintf("https://genius.com/%s-%s-lyrics", artistEncoded, titleEncoded)

return geniusURL
}

func extractLyrics(rawHtml string) (string, error) {
re := regexp.MustCompile(`<div data-lyrics-container="true"[^>]*>(.*?)<\/div>`)
matches := re.FindStringSubmatch(rawHtml)

if len(matches) < 2 {
return "", fmt.Errorf("Lyrics not found")
}

lyrics := matches[1]
lyrics = html.UnescapeString(lyrics)
lyrics = strings.ReplaceAll(lyrics, "<br/>", "\n")
lyrics = removeHTMLTags(lyrics)

return lyrics, nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/lyrify/v2

go 1.22.4
Empty file added go.sum
Empty file.
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

func main() {
track, err := CurrentSpotifyTrack()
if err != nil {
fmt.Println(err)
return
}

fmt.Println(track.Artist, "-", track.Title)

lyrics, err := FetchLyrics(track)
if err != nil {
fmt.Println(err)
return
}

fmt.Println(lyrics)
}
39 changes: 39 additions & 0 deletions spotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"os/exec"
"strings"
"errors"
)

// Darwin-only
func CurrentSpotifyTrack() (Track, error) {
script := `
tell application "Spotify"
if it is running then
set trackName to name of current track
set artistName to artist of current track
return artistName & "\t" & trackName
else
return "Spotify is not running"
end if
end tell
`

cmd := exec.Command("osascript", "-e", script)
output, err := cmd.CombinedOutput()
if err != nil {
return Track{}, err
}

parts := strings.Split(string(output), "\t")

if len(parts) < 2 {
return Track{}, errors.New("Is Spotify running?")
}

return Track{
Artist: parts[0],
Title: parts[1],
}, nil
}
7 changes: 7 additions & 0 deletions track.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type Track struct {
Artist string
Title string
}

20 changes: 20 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"regexp"
"strings"
)

func cleanString(s string) string {
reg := regexp.MustCompile(`[^\w\s-]`)
s = reg.ReplaceAllString(s, "")

s = strings.TrimSpace(s)

return s
}

func removeHTMLTags(htmlString string) string {
re := regexp.MustCompile(`<[^>]*>`)
return re.ReplaceAllString(htmlString, "")
}

0 comments on commit 0133838

Please sign in to comment.