最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

I am analyzing timestamped YouTube ments. Because some ments 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 < ments.length; i++) {
    var matches = ments[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);
}   

I am analyzing timestamped YouTube ments. Because some ments 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 < ments.length; i++) {
    var matches = ments[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);
}   
Share Improve this question edited Jun 16, 2012 at 23:08 ohaal 5,2682 gold badges37 silver badges56 bronze badges asked Jun 16, 2012 at 22:33 Mark LyonsMark Lyons 1,4227 gold badges23 silver badges60 bronze badges 4
  • Have a look at the YouTube API. Read more write less. – noob Commented Jun 16, 2012 at 22:36
  • 2 Well I'm using the YouTube API for plenty of things with this particular project, but I don't think there's anything there that will get me ments with time references in them (ex. "LOL 3:04!!!") and certainly not anything that will break that reference into seconds for me. – Mark Lyons Commented Jun 16, 2012 at 22:39
  • 1 Actually, this is possible, look at the entry elements: developers.google./youtube/2.0/… – Ricardo Souza Commented Jun 16, 2012 at 22:45
  • 2 Yes, you can get ments. You just can't retrieve the video time references in those ments with the API. – Mark Lyons Commented Jun 16, 2012 at 22:47
Add a ment  | 

2 Answers 2

Reset to default 8

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.

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论