From 34a69da36e1a4dd862a360d5bbef749f0f329d2e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 11 Dec 2014 10:02:05 +0100 Subject: First commit. A simple ruby app to download the 78's from http://78records.cdbpdx.com/ Just because clicking on close to 6000 files is a bit too much. --- Gemfile | 4 +++ Gemfile.lock | 12 ++++++++ bin/fetch78.rb | 6 ++++ lib/seventy_eights/application.rb | 60 +++++++++++++++++++++++++++++++++++++++ lib/seventy_eights/record.rb | 22 ++++++++++++++ test/test_helper.rb | 2 ++ test/unit/record_spec.rb | 43 ++++++++++++++++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100755 bin/fetch78.rb create mode 100644 lib/seventy_eights/application.rb create mode 100644 lib/seventy_eights/record.rb create mode 100644 test/test_helper.rb create mode 100644 test/unit/record_spec.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..2abb500 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "https://rubygems.org" + +gem "nokogiri" +#gem "pry", :group => :development diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..aa671e9 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,12 @@ +GEM + remote: https://rubygems.org/ + specs: + mini_portile (0.6.1) + nokogiri (1.6.4.1) + mini_portile (~> 0.6.0) + +PLATFORMS + ruby + +DEPENDENCIES + nokogiri diff --git a/bin/fetch78.rb b/bin/fetch78.rb new file mode 100755 index 0000000..f2ab52a --- /dev/null +++ b/bin/fetch78.rb @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +require_relative '../lib/seventy_eights/application' + +app = SeventyEights::Application.new('http://cdbpdx.com/78records/', File.expand_path('~/Music/78records')) +app.run diff --git a/lib/seventy_eights/application.rb b/lib/seventy_eights/application.rb new file mode 100644 index 0000000..51709a6 --- /dev/null +++ b/lib/seventy_eights/application.rb @@ -0,0 +1,60 @@ +require_relative 'record' +require 'nokogiri' +require 'open-uri' + +module SeventyEights + class Application + def initialize(site_url, target_dir) + @site = site_url + @target_dir = target_dir + @records = [] + end + + def run + read_site + parse + download_files + end + + private + + def read_site + puts "Opening #{@site}..." + @doc = Nokogiri::HTML(open(@site, :http_basic_authentication => %w{New090908 654321})) + end + + def parse + puts "Parsing..." + @doc.xpath('//tr').each do |row| + columns = row.xpath('td') + if columns.size >= 4 + link = columns[0].xpath('a/@href').text + title = columns[0].xpath('a/b').text + artist = columns[1].text + label = columns[2].text + catalog = columns[3].text + unless link.empty? + @records << Record.new(title, artist, label, catalog, URI.parse(link)) + end + end + end + end + + def download_files + puts "Downloading #{@records.size} tracks..." + i = 1 + @records.each do |r| + outfile = File.join(@target_dir, r.to_filename) + if File.exists?(outfile) + puts "Skipping #{i}: #{r.title} by #{r.artist}..." + else + puts "#{i}: #{r.title} by #{r.artist}..." + track = open(r.url, :http_basic_authentication => %w{New090908 654321}) + IO.write(outfile, track.read) + end + i += 1 + end + end + + end +end diff --git a/lib/seventy_eights/record.rb b/lib/seventy_eights/record.rb new file mode 100644 index 0000000..2abbb69 --- /dev/null +++ b/lib/seventy_eights/record.rb @@ -0,0 +1,22 @@ +require 'nokogiri' +require 'open-uri' + +module SeventyEights + class Record + attr_reader :title, :artist, :label, :catalog, :url + + def initialize(title, artist, label, catalog, url) + @title, @artist, @label, @catalog, @url = title, artist, label, catalog, url + end + + def to_filename + "#{safe @title} - #{safe @artist} - #{safe @label} - #{safe @catalog}.mp3" + end + + private + + def safe(str) + str.gsub('/', '-') + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..b61c944 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,2 @@ +require 'seventy_eights' +require 'minitest/autorun' diff --git a/test/unit/record_spec.rb b/test/unit/record_spec.rb new file mode 100644 index 0000000..c45c958 --- /dev/null +++ b/test/unit/record_spec.rb @@ -0,0 +1,43 @@ +require 'test_helper' + +describe SeventyEights::Record do + describe "when creating a record" do + before do + @r = SeventyEights::Record.new("BamBam", "The Strokes", "Virginia Records", "VR666-1", URI.parse('http://example.com/test.mp3')) + end + + it "must have the correct title" do + @r.title.must_equal "BamBam" + end + + it "must have correct artist" do + @r.artist.must_equal "The Strokes" + end + + it "must have correct label" do + @r.label.must_equal "Virginia Records" + end + + it "must have correct catalog number" do + @r.catalog.must_equal "VR666-1" + end + + it "must have correct URL" do + @r.url.to_s.must_equal 'http://example.com/test.mp3' + end + + it "must generate a sensible filename" do + @r.to_filename.must_equal 'BamBam - The Strokes - Virginia Records - VR666-1.mp3' + end + end + + describe "a record with slashes in a field" do + before do + @r = SeventyEights::Record.new("BamBam", "The Strokes w/Bunny Boo", "Virginia Records", "VR666-1", URI.parse('http://example.com/test.mp3')) + end + + it "must create a valid filename" do + @r.to_filename.must_equal 'BamBam - The Strokes w-Bunny Boo - Virginia Records - VR666-1.mp3' + end + end +end \ No newline at end of file -- cgit v1.2.3