gomu/utils.go

154 lines
3.1 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
"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-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-07-21 12:22:00 +08:00
"github.com/ztrue/tracerr"
2020-06-24 11:34:20 +08:00
)
2020-06-19 16:22:20 +08:00
2020-07-23 15:34:56 +08:00
// Logs erros to /tmp/gomu.log
2020-07-23 15:15:39 +08:00
func logError(err error) {
2020-07-22 21:01:13 +08:00
tmpDir := os.TempDir()
logFile := path.Join(tmpDir, "gomu.log")
file, e := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if e != nil {
log.Fatalf("Error opening file %s", logFile)
}
defer file.Close()
log.SetOutput(file)
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
log.Println(tracerr.SprintSource(err))
}
2020-07-24 17:07:53 +08:00
func debugLog(val ...interface{}) {
tmpDir := os.TempDir()
logFile := path.Join(tmpDir, "gomu.log")
file, e := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if e != nil {
log.Fatalf("Error opening file %s", logFile)
}
defer file.Close()
log.SetOutput(file)
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
log.Println(val...)
}
2020-07-21 01:21:59 +08:00
// Wraps error in a formatted way.
2020-07-23 15:15:39 +08:00
func wrapError(fnName string, err error) error {
2020-07-20 21:48:13 +08:00
return fmt.Errorf("%s: \n%e", fnName, err)
}
2020-07-23 15:34:56 +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-24 14:52:42 +08:00
// fmtDurationH returns the formatted duration `x hr x min`
func fmtDurationH(input time.Duration) string {
re := regexp.MustCompile(`\d+s`)
val := input.Round(time.Second).String()
// remove seconds
result := re.ReplaceAllString(val, "")
result = strings.Replace(result, "h", " hr ", 1)
result = strings.Replace(result, "m", " min ", 1)
return result
}
2020-07-23 15:34:56 +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-21 12:22:00 +08:00
log.Panicln(tracerr.SprintSource(err))
2020-06-25 14:12:23 +08:00
}
return path.Join(home, strings.TrimPrefix(_path, "~"))
}
2020-06-26 12:54:48 +08:00
2020-07-23 15:15:39 +08:00
// Detects the filetype of file
func getFileContentType(out *os.File) (string, error) {
2020-06-26 12:54:48 +08:00
buffer := make([]byte, 512)
_, err := out.Read(buffer)
if err != nil {
2020-07-21 12:22:00 +08:00
return "", tracerr.Wrap(err)
2020-06-26 12:54:48 +08:00
}
contentType := http.DetectContentType(buffer)
return strings.SplitAfter(contentType, "/")[1], nil
}
2020-07-23 15:15:39 +08:00
// Gets the file name by removing extension and path
func getName(fn string) string {
2020-07-12 17:04:33 +08:00
return strings.TrimSuffix(path.Base(fn), ".mp3")
2020-06-26 12:54:48 +08:00
}
2020-06-27 17:16:49 +08:00
2020-07-23 15:34:56 +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
2020-06-27 17:16:49 +08:00
// example ~/path/to/song/song.mp3
2020-07-12 17:04:33 +08:00
func extractFilePath(output []byte, dir string) string {
2020-06-27 17:16:49 +08:00
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, "/", `\/`)
}