Skip to content

Instantly share code, notes, and snippets.

@Chew
Created May 27, 2024 02:54
Show Gist options
  • Save Chew/b6c3273355aaac8cdaac50776d782018 to your computer and use it in GitHub Desktop.
Save Chew/b6c3273355aaac8cdaac50776d782018 to your computer and use it in GitHub Desktop.
EthoWiki episode table maker
#!/usr/bin/ruby
require 'rest-client'
require 'json'
require 'rails'
# YouTube API Key
KEY = "KEY_HERE"
# the YouTube playlist ID.
PLAYLIST_ID = "PL22E36306B459C4E2"
PLAYLIST_NAME = "" # The name of the playlist to be used on the table, not the actual YouTube name. If a season, put "Season x"
URL = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet%2CcontentDetails&maxResults=50&playlistId=#{PLAYLIST_ID}&key=#{KEY}"
VIDEO_URL = "https://youtube.googleapis.com/youtube/v3/videos?key=#{KEY}&part=snippet,contentDetails,statistics&id="
#puts "Requesting #{URL}"
def parse_duration(duration)
times = duration.gsub('PT', '').split(/[A-Z]/)
times.unshift('0') if times.size == 1
times.map! { |time| time.length == 1 ? "0#{time}" : time }
times.join(':')
end
def print_episodes(items)
items.each do |item|
title = item['snippet']['title']
duration = item['contentDetails']['duration']
view_count = (item['statistics']['viewCount'].to_i / 1000).round
upload_date = item['snippet']['publishedAt']
title_pattern = /^(.*) - Episode (\d+): (.*)$/ # Etho PLays MC
#title_pattern = /^Minecraft - (.*?) #(\d+): (.*)$/ # Ozone 2
# Matching the text
title_match = title.match(title_pattern)
# info
series = title_match[1]
ep_num = title_match[2]
ep_name = title_match[3]
# header
puts "|-"
# ep #
puts "|#{ep_num}"
puts "|[[#{ep_name}]]"
puts "|#{Time.parse(upload_date).getlocal.strftime("%b %-d, %Y")}"
puts "|#{view_count}"
puts "|#{parse_duration duration}"
end
end
# table header
puts '{| class="wikitable"'
puts "|+Episodes in #{PLAYLIST_NAME}"
puts '!Ep #'
puts '!Title'
puts '!Date'
puts "!Views"
puts "(thouands)"
puts '!Duration'
r1 = JSON.parse(RestClient.get(URL))
ids = r1['items'].map{|e| e['contentDetails']['videoId']}
videos = JSON.parse(RestClient.get("#{VIDEO_URL}#{ids.join(',')}"))
print_episodes videos['items']
page_token = r1['nextPageToken']
#puts "Request complete!"
until page_token.nil?
# puts "Requesting #{URL}&pageToken=#{page_token}"
req = JSON.parse(RestClient.get("#{URL}&pageToken=#{page_token}"))
page_token = req['nextPageToken']
ids = req['items'].map{|e| e['contentDetails']['videoId']}
videos = JSON.parse(RestClient.get("#{VIDEO_URL}#{ids.join(',')}"))
print_episodes videos['items']
# puts "Request complete!"
end
puts '|}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment