Last active
June 29, 2020 22:57
-
-
Save fidel/11e08df4b48a181ecabc3d7e1ed8170b to your computer and use it in GitHub Desktop.
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 '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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
ruby blofeld.rb | pbcopy
curl -s https://gist.githubusercontent.com/fidel/11e08df4b48a181ecabc3d7e1ed8170b/raw/blofeld.rb | ruby | pbcopy