Created
October 13, 2016 19:56
-
-
Save kookster/bb1573b8de74a1926c773c9ad3b9f46e to your computer and use it in GitHub Desktop.
Turn a SoundCloud playlist (set) into RSS
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
#!/usr/bin/env ruby | |
require 'soundcloud' | |
require 'builder' | |
require 'pp' | |
require 'csv' | |
# You'll need a sc api key | |
client_id = ENV['SC_CLIENT_ID'] | |
client = SoundCloud.new(client_id: client_id) | |
# Which sound cloud set path? | |
# e.g. set_id = '/playlists/12345678' | |
set_id = ENV['SC_PLAYLIST_PATH'] || '' | |
# What will be the final public url for this feed | |
# e.g. feed_url = 'https://somestation.fm/feeds/myfeed-rss.xml' | |
feed_url = ENV['FEED_URL'] || '' | |
playlist = client.get(set_id) | |
target = "" | |
xml = Builder::XmlMarkup.new(indent: 2, target: target) | |
xml.instruct! :xml, version: '1.0' | |
xml.rss 'xmlns:atom' => 'http://www.w3.org/2005/Atom', | |
'xmlns:itunes' => 'http://www.itunes.com/dtds/podcast-1.0.dtd', | |
'xmlns:media' => 'http://search.yahoo.com/mrss/', | |
'version' => '2.0' do | |
xml.channel do | |
xml.title playlist['title'] | |
xml.link playlist['permalink_url'] | |
xml.pubDate Time.now.utc.rfc2822 | |
xml.lastBuildDate Time.now.utc.rfc2822 | |
xml.ttl 60 | |
xml.language 'en-us' | |
xml.description playlist['description'] | |
tags = CSV.parse_line(playlist['tag_list'], col_sep: " ") | |
tags.each do |category| | |
xml.category category | |
end | |
xml.generator 'SoundCloud Playlist to RSS 1.0' | |
xml.docs 'http://blogs.law.harvard.edu/tech/rss' | |
xml.image do | |
xml.url playlist['artwork_url'] | |
xml.title playlist['title'] | |
xml.link playlist['permalink_url'] | |
end | |
xml.atom :link, href: feed_url, rel: 'self', type: 'application/rss+xml' | |
xml.itunes :author, playlist['user']['username'] | |
xml.itunes :image, href: playlist['artwork_url'] | |
xml.itunes :explicit, 'clean' | |
xml.itunes :owner do | |
xml.itunes :name, playlist['user']['username'] | |
end | |
xml.itunes :subtitle, playlist['title'] | |
xml.itunes(:summary) { xml.cdata!(playlist['description']) } | |
xml.media :copyright, "copyright #{playlist['user']['username']}, #{playlist['license'].gsub('-', ' ')}" | |
xml.media :thumbnail, url: playlist['artwork_url'] | |
playlist['tracks'].each_with_index do |ep, index| | |
xml.item do | |
xml.guid(ep['uri'], isPermaLink: false) | |
xml.title(ep['title']) | |
xml.pubDate Time.parse(ep['last_modified'] || ep['created_at']).utc.rfc2822 | |
xml.link ep['permalink_url'] | |
xml.description { xml.cdata!(ep['description'] || '') } | |
xml.enclosure url: ep['stream_url'] + "?client_id=#{client_id}", type: 'audio/mpeg', length: ep['original_content_size'] | |
end | |
end | |
end | |
end | |
puts target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment