Skip to content

Instantly share code, notes, and snippets.

@fidel
Last active June 29, 2020 22:57
Show Gist options
  • Save fidel/11e08df4b48a181ecabc3d7e1ed8170b to your computer and use it in GitHub Desktop.
Save fidel/11e08df4b48a181ecabc3d7e1ed8170b to your computer and use it in GitHub Desktop.
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'nokogiri'
gem 'playlist'
end
require 'open-uri'
require 'uri'
require 'cgi'
require 'optparse'
module Playlist::Format::M3U
class << self
def generate(playlist)
text = "#EXTM3U\n"
playlist.tracks.each do |t|
duration = (t.duration.to_i / 1000).round
text += "#EXTINF:#{duration},#{t.creator} - #{t.title}\n"
text += "#{t.location}\n"
end
text
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: blofeld.rb [options]"
opts.on("-d", "--dry-run", "Print all the shows' urls to STDOUT") do |d|
options[:dryrun] = d
end
opts.on("-r", "--reverse", "Return latest shows first") do |r|
options[:reverse] = r
end
end.parse!
directory_url = 'http://archives.bassdrivearchive.com/1%20-%20Monday/The%20Prague%20Connection%20-%20Blofeld/'.freeze
playlist = Playlist.new(title: "The Prague Connection - Blofeld")
title = ->(href) { CGI.unescape(href).sub('.mp3','') }
full_url = ->(href) { URI.join(directory_url, href) }
Nokogiri::HTML(URI(directory_url).open)
.xpath('//table/tr/td/a')
.map(&:attributes)
.map(&->(x) {x["href"]})
.map(&:value)
.select { |href| href =~ /.*\.mp3$/ }
.yield_self { |_| options[:reverse] ? _.reverse : _ }
.each { |href| playlist.add_track(creator: 'Blofeld', title: title.(href), location: full_url.(href), duration: 0); href }
.map { |href| full_url.(href) }
.yield_self { |_| puts *_ if options[:dryrun] }
File.open("blofeld.m3u", "wb") do |file|
file.write Playlist::Format::M3U.generate(playlist)
end unless options[:dryrun]
@fidel
Copy link
Author

fidel commented May 21, 2020

Usage

  1. Download & run: ruby blofeld.rb | pbcopy
  2. Alternatively run this trusted script straight outta internetz: curl -s https://gist.githubusercontent.com/fidel/11e08df4b48a181ecabc3d7e1ed8170b/raw/blofeld.rb | ruby | pbcopy
  3. Paste list to your favourite player, eg. VOX on Mac via cmd+u

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment