Created
October 25, 2016 18:11
-
-
Save tfl/c80195b97293b0057ad1fbc9730e2a46 to your computer and use it in GitHub Desktop.
Example for querying Arte.tv mediathek in Ruby
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 'json' | |
require 'net/http' | |
# %s gets replaced with yyyy-mm-dd/yyyy-mm-dd | |
URL = "http://www.arte.tv/papi/tvguide/epg/schedule/D/L3/%s/%s.json" | |
#### | |
## lets get started | |
#### | |
# ### | |
# returns the link to the json containing the video options | |
# ### | |
def get_video_url_from_json(json) | |
begin | |
json["VDO"]["videoStreamUrl"] | |
rescue | |
begin | |
json["VDS"]["videoStreamUrl"] | |
rescue Exception => x | |
#puts "get_video_url_from_json: #{x.message}" | |
nil | |
end | |
end | |
end | |
# ### | |
# returns the link to the best mp4 available via http download | |
# ### | |
def get_video_info_from_url(video_url) | |
begin | |
response = Net::HTTP.get(URI.parse(URI.escape(video_url))) | |
json = JSON.parse(response) | |
# get best quality as direct mp4 download | |
json["video"]["VSR"].each do |quality| | |
if(quality["bitrate"] == 2200 && quality["VFO"] == "HBBTV" && quality["VQU"] == "SQ") | |
return quality["VUR"] | |
end | |
end | |
# defaults | |
nil | |
rescue Exception => x | |
#puts "get_video_info_from_url: #{x.message}" | |
nil | |
end | |
end | |
# we fetch data for yesterday only | |
yesterday = Time.at(Time.now.to_i - 86400).strftime("%Y-%m-%d") | |
url = URL % [yesterday, yesterday] | |
uri = URI(url) | |
response = Net::HTTP.get(uri) | |
begin | |
json = JSON.parse(response) | |
json["abstractBroadcastList"].each do |elem| | |
video_url = get_video_url_from_json(elem) | |
if video_url | |
puts "#{elem["TIT"]}\t#{get_video_info_from_url(video_url)}" | |
end | |
end | |
rescue Exception => x | |
puts "Something went wrong parsing the response from #{url}: #{x.message}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment