Created
August 11, 2014 10:11
-
-
Save cantlin/6bb4f1ea60fd9edb6737 to your computer and use it in GitHub Desktop.
This file contains 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 'curb' | |
require 'json' | |
require 'sinatra' | |
last_poll = Time.now | |
poll_interval = 10 | |
borked_videos = [] | |
EXPECT_ENCODINGS = ['video/mp4','video/3gp:small','video/3gp:large','video/m3u8','video/webm'] | |
Thread.abort_on_exception = true | |
Thread.new do | |
while true | |
last_poll = Time.now | |
r = Curl.get "http://content.guardianapis.com/search?tag=type/video&show-elements=video&page-size=50&show-fields=shortUrl,internalOctopusCode,internalContentCode" | |
data = JSON.parse r.body_str | |
videos = data['response']['results'] | |
videos.each do |v| | |
video_element = v['elements'].select {|e| e['type'] == 'video'}.first | |
encodings = video_element['assets'].select {|a| a['type'] == 'video'}.map {|va| va['mimeType']} | |
v['missing'] = EXPECT_ENCODINGS - encodings | |
if v['missing'].length > 0 && !borked_videos.map {|bv| bv['webUrl']}.include?(v['webUrl']) | |
borked_videos << v | |
end | |
end | |
borked_videos = borked_videos.select do |v| | |
video_element = v['elements'].select {|e| e['type'] == 'video'}.first | |
encodings = video_element['assets'].select {|a| a['type'] == 'video'}.map {|va| va['mimeType']} | |
v['missing'] = EXPECT_ENCODINGS - encodings | |
v['missing'].length > 0 | |
end | |
puts "Checked #{videos.length} videos. #{borked_videos.length} had less than #{EXPECT_ENCODINGS.length} encodings." | |
borked_videos.each do |v| | |
puts "\n #{v['webUrl']}" | |
v['missing'].each {|mt| puts " * missing #{mt}"} | |
puts "" | |
end | |
sleep poll_interval | |
end | |
end | |
get '/' do | |
o = "<h1>Video Checker</h1><h2>Videos Missing Encodings</h2>" | |
o += borked_videos.length == 0 ? "<p>Hurrah! No videos are missing encodings right now." : "<ul>" | |
borked_videos.each do |v| | |
o += "<li><a href=\"#{v['webUrl']}\" target=\"_blank\">#{v['fields']['shortUrl']}</a> (published #{v['webPublicationDate']}) (octopus ID #{v['fields']['internalOctopusCode']}) (<a href=\"http://cms.guprod.gnl/tools/video/#{v['fields']['internalContentCode']}/edit\" target=\"_blank\">R2 link</a>)</li>" | |
o += "<ul>" | |
v['missing'].each do |e| | |
o += "<li>missing #{e}</li>" | |
end | |
o += "</ul>" | |
end | |
o += "</ul><h2>Happy Videos</h2><p>All other recent videos have each of #{EXPECT_ENCODINGS.join(', ')}.</p><p>Last poll: #{last_poll}.<br />Checking every: #{poll_interval} seconds." | |
o | |
end | |
get '/api' do | |
content_type :json | |
borked_videos.to_json | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment