-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0133838
Showing
9 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package main | ||
|
||
type Track struct { | ||
Artist string | ||
Title string | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") | ||
} |