Last active
September 20, 2020 06:01
-
-
Save viz3/011a07c83f344c776924c7ef87eb617d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'json' | |
require 'optparse' | |
class AllItems | |
attr_reader :items | |
def initialize(channel_id, api_key) | |
@channel_id = channel_id | |
@api_key = api_key | |
@count = 0 | |
@items = [] | |
call_api | |
end | |
def call_api(page_token = nil) | |
# https://developers.google.com/youtube/v3/docs/search/list?hl=ja | |
uri = URI.parse 'https://www.googleapis.com/youtube/v3/search' | |
params = { | |
part: 'snippet', | |
channelId: @channel_id, | |
maxResults: '50', | |
key: @api_key | |
} | |
params['pageToken'] = page_token unless page_token == nil | |
uri.query = URI.encode_www_form(params) | |
body = Net::HTTP.get uri | |
@count = @count + 1 | |
ofname = sprintf('%s.%02d.json', @channel_id, @count) | |
File.write(ofname, body) | |
result = JSON.parse(body) | |
result['items'].each do |item| | |
next unless item['id']['kind'] == 'youtube#video' | |
@items << item | |
end | |
call_api(result['nextPageToken']) if result.has_key?('nextPageToken') | |
end | |
end | |
opts = { | |
channel_id: '', | |
api_key: '' | |
} | |
opt = OptionParser.new | |
opt.on('-c VAL') {|v| opts[:channel_id] = v } | |
opt.on('-k VAL') {|v| opts[:api_key] = v } | |
opt.parse!(ARGV) | |
items = {} | |
AllItems.new(opts[:channel_id], opts[:api_key]).items.each do |item| | |
publish_time = item['snippet']['publishTime'] | |
items[publish_time] = item | |
end | |
items.sort.each do |publish_time, item| | |
video_id = item['id']['videoId'] | |
title = item['snippet']['title'] | |
puts sprintf('* %s [%s](https://www.youtube.com/watch?v=%s)', publish_time, title, video_id) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment