Skip to content

Instantly share code, notes, and snippets.

@miyagawa
Last active December 27, 2015 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miyagawa/7281722 to your computer and use it in GitHub Desktop.
Save miyagawa/7281722 to your computer and use it in GitHub Desktop.
Dashing widget to fetch iTunes Store Podcast ranking & ratings
#!/usr/bin/env ruby
require 'net/http'
require 'uri'
require 'nokogiri'
id = '603013428'
country = 'jp'
genre = '1318'
filter = ["275834665", "537657040", "470664050"] # Apple's
feed = URI("https://itunes.apple.com/#{country}/rss/toppodcasts/limit=100/genre=#{genre}/xml")
page = URI("https://itunes.apple.com/#{country}/podcast/id#{id}")
SCHEDULER.every '1h', :first_in => 0 do |job|
http = Net::HTTP.new(feed.host, feed.port)
http.use_ssl = true
response = http.request(Net::HTTP::Get.new(feed.request_uri))
doc = Nokogiri::XML(response.body)
entries = doc.css('entry').map { |node| node.css('id')[0]['im:id'] } - filter
rank = entries.find_index(id) + 1
http = Net::HTTP.new(page.host, page.port)
http.use_ssl = true
response = http.request(Net::HTTP::Get.new(page.request_uri))
doc = Nokogiri::HTML(response.body)
stars = "\u2605" * doc.css('.customer-ratings .rating-star').count
rating_count = doc.css('.customer-ratings .rating-count').text[/\d+/]
send_event 'itunes_ranking', current: rank, moreinfo: "#{stars} #{rating_count} ratings", prefix: '#'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment