gomu/utils.go

196 lines
3.7 KiB
Go
Raw Normal View History

2020-06-22 00:05:56 +08:00
// Copyright (C) 2020 Raziman
2020-06-19 16:22:20 +08:00
package main
2020-06-24 11:34:20 +08:00
import (
2020-06-27 17:16:49 +08:00
"bytes"
"fmt"
2020-07-02 16:11:10 +08:00
"log"
2020-06-26 12:54:48 +08:00
"net/http"
2020-06-24 11:34:20 +08:00
"os"
2020-06-27 17:16:49 +08:00
"os/exec"
2020-06-25 14:12:23 +08:00
"path"
2020-06-27 17:16:49 +08:00
"regexp"
2020-06-24 11:34:20 +08:00
"strings"
"time"
2020-06-26 12:54:48 +08:00
2020-06-27 17:16:49 +08:00
"github.com/rivo/tview"
"github.com/spf13/viper"
2020-06-24 11:34:20 +08:00
)
2020-06-19 16:22:20 +08:00
2020-07-02 16:11:10 +08:00
// initialiaze simple log
func appLog(v ...interface{}) {
2020-06-19 16:22:20 +08:00
2020-07-02 16:11:10 +08:00
tmpDir := os.TempDir()
2020-06-19 16:22:20 +08:00
2020-07-02 16:11:10 +08:00
logFile := path.Join(tmpDir, "gomu.log")
2020-06-19 16:22:20 +08:00
2020-07-02 16:11:10 +08:00
file, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
2020-06-19 16:22:20 +08:00
2020-07-02 16:11:10 +08:00
if err != nil {
log.Fatalf("Error opening file %s", logFile)
2020-06-19 16:22:20 +08:00
}
2020-07-02 16:11:10 +08:00
defer file.Close()
log.SetOutput(file)
log.Println(v...)
2020-06-19 16:22:20 +08:00
}
2020-06-24 11:34:20 +08:00
2020-07-02 16:11:10 +08:00
2020-07-01 17:49:07 +08:00
// formats duration to my desired output mm:ss
2020-06-24 11:34:20 +08:00
func fmtDuration(input time.Duration) string {
val := input.Round(time.Second).String()
if !strings.Contains(val, "m") {
val = "0m" + val
}
val = strings.ReplaceAll(val, "h", ":")
val = strings.ReplaceAll(val, "m", ":")
val = strings.ReplaceAll(val, "s", "")
var result []string
for _, v := range strings.Split(val, ":") {
2020-06-26 12:54:48 +08:00
2020-06-24 11:34:20 +08:00
if len(v) < 2 {
2020-06-26 12:54:48 +08:00
result = append(result, "0"+v)
2020-06-24 11:34:20 +08:00
} else {
result = append(result, v)
}
}
return strings.Join(result, ":")
}
2020-06-25 14:12:23 +08:00
2020-07-01 17:49:07 +08:00
// expands tilde alias to /home/user
2020-06-25 14:12:23 +08:00
func expandTilde(_path string) string {
if !strings.HasPrefix(_path, "~") {
return _path
}
2020-06-26 12:54:48 +08:00
home, err := os.UserHomeDir()
2020-06-25 14:12:23 +08:00
if err != nil {
2020-07-02 16:11:10 +08:00
appLog(err)
2020-06-25 14:12:23 +08:00
}
return path.Join(home, strings.TrimPrefix(_path, "~"))
}
2020-06-26 12:54:48 +08:00
// detects the filetype of file
func GetFileContentType(out *os.File) (string, error) {
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
return "", err
}
contentType := http.DetectContentType(buffer)
return strings.SplitAfter(contentType, "/")[1], nil
}
// gets the file name by removing extension and path
func GetName(fn string) string {
return strings.TrimSuffix(path.Base(fn), path.Ext(fn))
}
2020-06-27 17:16:49 +08:00
// download audio from youtube audio and adds the song to the selected playlist
func Ytdl(url string, selPlaylist *tview.TreeNode) {
2020-06-28 12:52:38 +08:00
// lookup if youtube-dl exists
_, err := exec.LookPath("youtube-dl")
if err != nil {
2020-06-28 14:03:30 +08:00
timedPopup(" Error ", "youtube-dl is not in your $PATH", popupTimeout)
2020-06-28 12:52:38 +08:00
return
}
2020-06-27 17:16:49 +08:00
dir := viper.GetString("music_dir")
selAudioFile := selPlaylist.GetReference().(*AudioFile)
selPlaylistName := selAudioFile.Name
2020-07-02 16:11:10 +08:00
timedPopup(" Ytdl ", "Downloading", time.Second*5)
2020-06-27 17:16:49 +08:00
// specify the output path for ytdl
outputDir := fmt.Sprintf(
2020-06-28 13:41:14 +08:00
"%s/%s/%%(title)s.%%(ext)s",
2020-06-27 17:18:10 +08:00
dir,
2020-06-27 17:16:49 +08:00
selPlaylistName)
args := []string{
"--extract-audio",
"--audio-format",
"mp3",
"--output",
outputDir,
url,
}
cmd := exec.Command("youtube-dl", args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
go func() {
err := cmd.Run()
if err != nil {
2020-06-28 14:03:30 +08:00
timedPopup(" Error ", "Error running youtube-dl", time.Second*5)
2020-06-27 17:16:49 +08:00
return
}
2020-07-01 17:49:07 +08:00
playlistPath := path.Join(expandTilde(dir), selPlaylistName)
2020-06-27 17:16:49 +08:00
downloadedAudioPath := downloadedFilePath(
stdout.Bytes(), playlistPath)
2020-07-03 00:50:32 +08:00
err = playlist.AddSongToPlaylist(downloadedAudioPath, selPlaylist)
if err != nil {
log.Println(err)
}
2020-06-27 17:16:49 +08:00
2020-07-02 16:11:10 +08:00
downloadFinishedMessage := fmt.Sprintf("Finished downloading\n%s",
path.Base(downloadedAudioPath))
2020-06-28 14:03:30 +08:00
timedPopup(
2020-06-27 17:16:49 +08:00
" Ytdl ",
2020-07-02 16:11:10 +08:00
downloadFinishedMessage,
time.Second*5,
)
app.Draw()
2020-06-27 17:16:49 +08:00
}()
}
// this just parsing the output from the ytdl to get the audio path
// this is used because we need to get the song name
// example ~/path/to/song/song.mp3
func downloadedFilePath(output []byte, dir string) string {
regexSearch := fmt.Sprintf(`\[ffmpeg\] Destination: %s\/.*.mp3`,
escapeBackSlash(dir))
parseAudioPathOnly := regexp.MustCompile(`\/.*mp3$`)
re := regexp.MustCompile(regexSearch)
return string(parseAudioPathOnly.Find(re.Find(output)))
}
func escapeBackSlash(input string) string {
return strings.ReplaceAll(input, "/", `\/`)
}