我正在尝试搜索给定YouTube视频的评论,以查看它是否包含特定字符串。
使用Yt gem进行的api通话工作正常。但是,我正在努力,因为.all
和某些SQL查询不起作用,因为响应集合似乎是延迟加载的。
问题 - 如何查询CommentThreads集合以查看哪些项目包含特定字符串,例如“parts?
下面是我尝试运行的一些表达式的控制台输出,包括LIKE查询:
@video_comments
=> #<Yt::Collections::CommentThreads:0x007fe626f70120>
@video_comments.all
=> NoMethodError: undefined method `all' for #<Yt::Collections::CommentThreads:0x007fe626f70120>
@video_comments.first.text_display
=> "Links for the parts in the description! <br /><br />What color do you want?"
@video_comments.first.text_display.include? "parts"
=> true
@video_comments.where('text_display LIKE ?', "%parts%").first
=> ArgumentError: wrong number of arguments (given 2, expected 0..1)
from /Users/macbook/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/yt-0.28.5/lib/yt/collections/base.rb:37:in `where'
Rails 5.0.1 + ruby 2.3.0p0 + psql(PostgreSQL)9.6.1
编辑:实际@video_comments
回复http://imgur.com/bjkXWfO
答案 0 :(得分:0)
.all
适用于模型,例如如果你有一个模型VideoComments,那么VideoComments.all
将返回所有评论。
您的上一个查询应该有效,只是语法错误,请尝试
@video_comments.where('text_display iLIKE = ?', "%parts%").first
答案 1 :(得分:0)
首先,@ video_comments本身不是活动记录。因此,您无法使用@video_comments.all
根据谷歌的评论线索,回复应该是:
{
"kind": "youtube#commentThread",
"etag": etag,
"id": string,
"snippet": {
"channelId": string,
"videoId": string,
"topLevelComment": comments Resource,
"canReply": boolean,
"totalReplyCount": unsigned integer,
"isPublic": boolean
},
"replies": {
"comments": [
comments Resource
]
}
}
所以,你不是使用@video_comments.all
,而是你的命令应该是:
@video_comments.replies.comments
(如果不成功,请尝试@video_comments["replies"]["comments"]
)
它应该返回一个评论列表。
好的,下一步,您将过滤评论列表,以获得对内容“部分”字词的评论。
@video_comments.replies.comments.select{|comment| comment.text_display.include? "parts"}
这样的事情。
详情请参阅以下链接:
https://developers.google.com/youtube/v3/docs/commentThreads
答案 2 :(得分:0)
我做了类似的事情,最后通过参考:
使其工作 video_comments['items'][0]['snippet']['data']['textOriginal']
(这将返回实际评论。然后可以搜索该文本。)
如果没有单引号,或['items']
之后没有[0](没有引号),它就无法工作。
Items
是一个包含单个元素的数组,其中包含一个数组和散列的嵌套。如果我尝试搜索整个Items
数组,它只返回其中嵌套的哈希和数组的名称;不是他们的内容。
我发现获取实际内容的唯一方法是直接引用我想要的元素(在这种情况下,['textOriginal']
中的注释文本。