feat: improve timestamp parsing to handle fractional seconds in YouTube tool

## CHANGES

- Move timestamp regex initialization to init function
- Add parseSeconds helper function for fractional seconds
- Replace direct strconv.Atoi calls with parseSeconds function
- Support decimal seconds in timestamp format parsing
- Extract seconds parsing logic into reusable function
This commit is contained in:
Kayvan Sylvan
2025-07-18 11:53:28 -07:00
parent 1a35f32a48
commit dc722f9724

View File

@@ -29,11 +29,15 @@ import (
"google.golang.org/api/youtube/v3"
)
// Match timestamps like "00:00:01.234" or just numbers or sequence numbers
var timestampRegex = regexp.MustCompile(`^\d+$|^\d{1,2}:\d{2}(:\d{2})?(\.\d{3})?$`)
var timestampRegex *regexp.Regexp
const TimeGapForRepeats = 10 // seconds
func init() {
// Match timestamps like "00:00:01.234" or just numbers or sequence numbers
timestampRegex = regexp.MustCompile(`^\d+$|^\d{1,2}:\d{2}(:\d{2})?(\.\d{3})?$`)
}
func NewYouTube() (ret *YouTube) {
label := "YouTube"
@@ -340,7 +344,7 @@ func parseTimestampToSeconds(timestamp string) (int, error) {
if minutes, err = strconv.Atoi(parts[1]); err != nil {
return 0, err
}
if seconds, err = strconv.Atoi(parts[2]); err != nil {
if seconds, err = parseSeconds(parts[2]); err != nil {
return 0, err
}
} else {
@@ -348,7 +352,7 @@ func parseTimestampToSeconds(timestamp string) (int, error) {
if minutes, err = strconv.Atoi(parts[0]); err != nil {
return 0, err
}
if seconds, err = strconv.Atoi(parts[1]); err != nil {
if seconds, err = parseSeconds(parts[1]); err != nil {
return 0, err
}
}
@@ -356,6 +360,23 @@ func parseTimestampToSeconds(timestamp string) (int, error) {
return hours*3600 + minutes*60 + seconds, nil
}
func parseSeconds(seconds_str string) (int, error) {
var seconds int
var err error
if strings.Contains(seconds_str, ".") {
// Handle fractional seconds
second_parts := strings.Split(seconds_str, ".")
if seconds, err = strconv.Atoi(second_parts[0]); err != nil {
return 0, err
}
} else {
if seconds, err = strconv.Atoi(seconds_str); err != nil {
return 0, err
}
}
return seconds, nil
}
func (o *YouTube) GrabComments(videoId string) (ret []string, err error) {
if err = o.initService(); err != nil {
return