Last active
February 29, 2016 10:34
-
-
Save stevensona/4e4fc89b9e3cd505f48e to your computer and use it in GitHub Desktop.
Get Podcast Feed from iTunes URL
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 'httparty' | |
require 'nokogiri' | |
def get_id url | |
#extract the id from the url | |
url.scan(/id(\d+)/).first.first | |
end | |
def get_page url | |
#download the html content of the podcast page using itunes user-agent | |
HTTParty.post(url, headers: { | |
'User-Agent' => 'iTunes/10.3.1 (Macintosh; Intel Mac OS X 10.6.8) AppleWebKit/533.21.1' | |
}) | |
end | |
def get_feed html | |
#extract the feed-url attribute from the first button element containing one | |
Nokogiri::HTML(html).css('button').find{ |b| not b['feed-url'].nil? }['feed-url'] | |
end | |
def get_feed_from_itunes url | |
#convert a itunes podcast url into a feed url | |
get_feed(get_page "https://itunes.apple.com/WebObjects/DZR.woa/wa/viewPodcast?id=#{get_id url}") | |
end | |
test = 'https://itunes.apple.com/us/podcast/99-invisible/id394775318?mt=2' | |
puts get_feed_from_itunes u | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment