Skip to content

Instantly share code, notes, and snippets.

@metade
Created October 25, 2012 13:25
Show Gist options
  • Save metade/3952531 to your computer and use it in GitHub Desktop.
Save metade/3952531 to your computer and use it in GitHub Desktop.
artists_played_by_a_programmes_brand.rb
require 'open-uri'
require 'json'
require 'date'
require 'pp'
brand_pids = ['b0100rp6', 'b0072lb2', 'b006tmr6', 'b01mrh21', 'b006wr19']
brand_pids.each do |brand_pid|
now = Time.now
url = "http://www.bbc.co.uk/programmes/#{brand_pid}/episodes/#{now.year}/#{now.month}.json"
begin
data = JSON.parse open(url).read
rescue
puts "oops! #{url}"
next
end
episodes = data['broadcasts'].
select { |b| Time.parse(b['start']) < now }[0...10].
map { |b| b['programme'] }
artists = {}
artist_count = Hash.new(0)
episodes.map do |episode|
url = "http://www.bbc.co.uk/programmes/#{episode['pid']}/segments.json"
segment_events_data = JSON.parse open(url).read
segment_events_data['segment_events'].each do |segment_event|
artist = segment_event['segment']['primary_contributor'] if segment_event['segment']['primary_contributor']
if artist['musicbrainz_gid']
artists[artist['musicbrainz_gid']] ||= artist
artist_count[artist['musicbrainz_gid']] += 1
end
end
end
puts "*[#{episodes.first['programme']['title']}|http://www.bbc.co.uk/programmes/#{episodes.first['programme']['pid']}]*"
puts "||artist||count||"
artist_count.
sort { |a,b| b.last <=> a.last }[0...10].
map { |a| [artists[a.first], a.last] }.
each { |a| puts "|[#{a.first['name']}|http://www.bbc.co.uk/music/artists/#{a.first['musicbrainz_gid']}]|#{a.last}|" }
puts ""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment