Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching timestamps (hh:mm:ss) from Youtube comments with Youtube API using regex in JavaScript

I am analyzing timestamped YouTube comments. Because some comments may refer to a period in either mm:ss, m:ss, hh:mm:ss, or h:mm:ss, I need to prepare for these cases. The following code works on mm:ss and m:ss, but still treats the one with hours as if it was mm:ss. For example, 02:24:30 returns 144, as it is only analyzing the first two parts. Here is the code:

var timePattern = /(([0-5][0-9])|[0-9])\:[0-9]{2,2}/;
var seconds = "";

for (var i = 0; i < comments.length; i++) {
    var matches = comments[i].match(timePattern);
    var matched = matches[0];
    var a = matched.split(':');
    if(matched.length == 7 || matched.length == 8) {
        seconds = (+a[0])*60*60 + (+a[1])*60 + a[2];        
    } else {
        seconds = (+a[0])*60 + (+a[1]); 
    }
    times.push(seconds);
}   
like image 738
Mark Lyons Avatar asked Jun 16 '12 22:06

Mark Lyons


2 Answers

Try a different regex.

(?:([0-5]?[0-9]):)?([0-5]?[0-9]):([0-5][0-9])

First contains hours, second contains minutes, last contains seconds.

Hours will be empty if no hours are found.

like image 167
ohaal Avatar answered Nov 15 '22 05:11

ohaal


You can also get this info on the comments feed, if you're using the API: https://developers.google.com/youtube/2.0/developers_guide_protocol#Comments

The comments feed have a published element that contains the date and time information that you can use to parse the seconds of.

like image 41
Ricardo Souza Avatar answered Nov 15 '22 04:11

Ricardo Souza