Timestamps and offsets correctly calculated and displayed for days and hours

This commit is contained in:
Matthieu Grieger 2015-05-12 22:47:14 -07:00
parent e947524524
commit 2843d94179

View file

@ -15,6 +15,7 @@ import (
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -52,55 +53,63 @@ func NewYouTubeSong(user, id, offset string, playlist *YouTubePlaylist) (*YouTub
return nil, err return nil, err
} }
var offsetMinutes, offsetSeconds int64 var offsetDays, offsetHours, offsetMinutes, offsetSeconds int64
if offset != "" { if offset != "" {
if strings.Contains(offset, "m") { offsetExp := regexp.MustCompile(`t\=(?P<days>\d+d)?(?P<hours>\d+h)?(?P<minutes>\d+m)?(?P<seconds>\d+s)?`)
offsetMinutes, _ = strconv.ParseInt(offset[3:strings.Index(offset, "m")], 10, 32) offsetMatch := offsetExp.FindStringSubmatch(offset)
if strings.Contains(offset, "s") { offsetResult := make(map[string]string)
offsetSeconds, _ = strconv.ParseInt(offset[strings.Index(offset, "m")+1:strings.Index(offset, "s")], 10, 32) for i, name := range offsetExp.SubexpNames() {
} else { offsetResult[name] = offsetMatch[i]
offsetSeconds = 0 }
}
} else if strings.Contains(offset, "s") { if offsetResult["days"] != "" {
offsetMinutes = 0 offsetDays, _ = strconv.ParseInt(strings.TrimSuffix(offsetResult["days"], "d"), 10, 32)
offsetSeconds, _ = strconv.ParseInt(offset[3:strings.Index(offset, "s")], 10, 32) }
if offsetResult["hours"] != "" {
offsetHours, _ = strconv.ParseInt(strings.TrimSuffix(offsetResult["hours"], "h"), 10, 32)
}
if offsetResult["minutes"] != "" {
offsetMinutes, _ = strconv.ParseInt(strings.TrimSuffix(offsetResult["minutes"], "m"), 10, 32)
}
if offsetResult["seconds"] != "" {
offsetSeconds, _ = strconv.ParseInt(strings.TrimSuffix(offsetResult["seconds"], "s"), 10, 32)
} }
} else {
offsetMinutes = 0
offsetSeconds = 0
} }
title, _ := apiResponse.String("items", "0", "snippet", "title") title, _ := apiResponse.String("items", "0", "snippet", "title")
thumbnail, _ := apiResponse.String("items", "0", "snippet", "thumbnails", "high", "url") thumbnail, _ := apiResponse.String("items", "0", "snippet", "thumbnails", "high", "url")
duration, _ := apiResponse.String("items", "0", "contentDetails", "duration") duration, _ := apiResponse.String("items", "0", "contentDetails", "duration")
var minutes, seconds int64 var days, hours, minutes, seconds int64
if strings.Contains(duration, "M") { timestampExp := regexp.MustCompile(`P(?P<days>\d+D)?T(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\d+S)?`)
minutes, _ = strconv.ParseInt(duration[2:strings.Index(duration, "M")], 10, 32) timestampMatch := timestampExp.FindStringSubmatch(duration)
if strings.Contains(duration, "S") { timestampResult := make(map[string]string)
seconds, _ = strconv.ParseInt(duration[strings.Index(duration, "M")+1:len(duration)-1], 10, 32) for i, name := range timestampExp.SubexpNames() {
} else { timestampResult[name] = timestampMatch[i]
seconds = 0
}
} else if strings.Contains(duration, "S") {
minutes = 0
seconds, _ = strconv.ParseInt(duration[2:len(duration)-1], 10, 32)
} else {
minutes = 0
seconds = 0
} }
combinedMinutes := minutes - offsetMinutes if timestampResult["days"] != "" {
combinedSeconds := seconds - offsetSeconds days, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["days"], "D"), 10, 32)
totalSeconds := int((minutes * 60) + seconds) }
durationString := fmt.Sprintf("%d:%02d", combinedMinutes, combinedSeconds) if timestampResult["hours"] != "" {
hours, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["hours"], "H"), 10, 32)
}
if timestampResult["minutes"] != "" {
minutes, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["minutes"], "M"), 10, 32)
}
if timestampResult["seconds"] != "" {
seconds, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["seconds"], "S"), 10, 32)
}
totalSeconds := int((days * 86400) + (hours * 3600) + (minutes * 60) + seconds)
durationString := fmt.Sprintf("%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration { if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration {
song := &YouTubeSong{ song := &YouTubeSong{
submitter: user, submitter: user,
title: title, title: title,
id: id, id: id,
offset: int((offsetMinutes * 60) + offsetSeconds), offset: int((offsetDays * 86400) + (offsetHours * 3600) + (offsetMinutes * 60) + offsetSeconds),
filename: id + ".m4a", filename: id + ".m4a",
duration: durationString, duration: durationString,
thumbnail: thumbnail, thumbnail: thumbnail,
@ -332,19 +341,29 @@ func NewYouTubePlaylist(user, id string) (*YouTubePlaylist, error) {
} }
videoDuration, _ := durationResponse.String("items", "0", "contentDetails", "duration") videoDuration, _ := durationResponse.String("items", "0", "contentDetails", "duration")
var minutes, seconds int64 var days, hours, minutes, seconds int64
if strings.Contains(videoDuration, "M") { timestampExp := regexp.MustCompile(`P(?P<days>\d+D)?T(?P<hours>\d+H)?(?P<minutes>\d+M)?(?P<seconds>\d+S)?`)
minutes, _ = strconv.ParseInt(videoDuration[2:strings.Index(videoDuration, "M")], 10, 32) timestampMatch := timestampExp.FindStringSubmatch(videoDuration)
} else { timestampResult := make(map[string]string)
minutes = 0 for i, name := range timestampExp.SubexpNames() {
timestampResult[name] = timestampMatch[i]
} }
if strings.Contains(videoDuration, "S") {
seconds, _ = strconv.ParseInt(videoDuration[strings.Index(videoDuration, "M")+1:len(videoDuration)-1], 10, 32) if timestampResult["days"] != "" {
} else { days, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["days"], "D"), 10, 32)
seconds = 0
} }
totalSeconds := int((minutes * 60) + seconds) if timestampResult["hours"] != "" {
durationString := fmt.Sprintf("%d:%02d", minutes, seconds) hours, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["hours"], "H"), 10, 32)
}
if timestampResult["minutes"] != "" {
minutes, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["minutes"], "M"), 10, 32)
}
if timestampResult["seconds"] != "" {
seconds, _ = strconv.ParseInt(strings.TrimSuffix(timestampResult["seconds"], "S"), 10, 32)
}
totalSeconds := int((days * 86400) + (hours * 3600) + (minutes * 60) + seconds)
durationString := fmt.Sprintf("%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration { if dj.conf.General.MaxSongDuration == 0 || totalSeconds <= dj.conf.General.MaxSongDuration {
playlistSong := &YouTubeSong{ playlistSong := &YouTubeSong{