get_tapas 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9e18e87b07cb269780647167e73b4d6808da1ced
4
+ data.tar.gz: 58893d7ffe0865724bb949b2f5ceff60cd7c15e8
5
+ SHA512:
6
+ metadata.gz: dc2feee0bc5331e30ebbecfc9905f973e942f609688471dc1114aed2f9092b5e07d2a97b7b941275c72544d2cdddf1cc77bac81713aec973cb3959c5b736bfe5
7
+ data.tar.gz: 161bdebe77eca12d4bc4f3432d96f4052507e89aaee412516f2c8675a56ea267f0a33a28be4f3adebe152ac2903663a5c393b516382cad1945cbc315ba3f27cc
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.idea/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in get_tapas.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Keith Bennett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # get-tapas #
2
+
3
+ Downloads Ruby Tapas screencast videos for paid subscribers.
4
+
5
+ Prerequisites:
6
+
7
+ * ruby (of course)
8
+ * curl (install with your package manager such as brew, yum, or apt-get)
9
+
10
+ Instructions:
11
+
12
+ ```
13
+ gem install get-tapas
14
+ # Get the video download HTML text (see below.)
15
+ get-tapas [options]
16
+ ```
17
+
18
+ This gem provides a script that _partially_ automates the process of
19
+ downloading Ruby Tapas screencasts en masse, eliminating the need to
20
+ click each screencast's link individually.
21
+
22
+ ## Getting the Required HTML Text
23
+
24
+ Here are the steps you will need to follow:
25
+
26
+ * Log in to https://www.rubytapas.com.
27
+ * Navigate to https://www.rubytapas.com/download-list, either by
28
+ inserting the URL above into the address bar, or by
29
+ selecting _Video Downloads_ from the _Episodes_ menu.
30
+ * View the page's source, in many browsers by pressing _Ctrl/Cmd-U_.
31
+ * Select all its text (_Ctrl/Cmd-A_).
32
+ * Copy that text to the clipboard (_Ctrl/Cmd-C_).
33
+ * You can either:
34
+ * pipe the text from your clipboard into the get-tapas script using `pbpaste`
35
+ or whatever other clipboard paste command your system provides,
36
+ and specifying to the script that it should get the HTML
37
+ from standard input (`-i -`), or
38
+ * save the text to a file, as in `pbpaste > download_list.html`,
39
+ and specify that filename with `-i path/to/download_list.html`.
40
+
41
+
42
+ ## Command Line Options
43
+
44
+ Running the script without any arguments will output the command line
45
+ options. They're pretty self explanatory. For your convenience, here is
46
+ the output:
47
+
48
+ ```
49
+ Usage: get-tapas [options]
50
+ -d, --data_dir dir Directory for downloaded files (default: .)
51
+ -i, --input_spec spec Filespec (or '-' for STDIN) containing Ruby Tapas download list page HTML
52
+ -0, --min_episode_num number Minimum episode number to download
53
+ -9, --max_episode_num number Maximum episode number to download
54
+ -n, --[no-]no_op Don't download anything, just show what would have been downloaded.
55
+ -r, --[no-]reverse Reverse the list of input files to download most recent first
56
+ -z, --sleep_interval seconds Sleep this many seconds between files
57
+ -h, --help Show this message
58
+ ```
59
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "get_tapas"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/get-tapas ADDED
@@ -0,0 +1,104 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ =begin
4
+ Usage: get-tapas [options]
5
+ -d, --data_dir dir Directory for downloaded files (default: .)
6
+ -i, --input_spec spec Filespec (or '-' for STDIN) containing Ruby Tapas download list page HTML
7
+ -0, --min_episode_num number Minimum episode number to download
8
+ -9, --max_episode_num number Maximum episode number to download
9
+ -n, --[no-]no_op Don't download anything, just show what would have been downloaded.
10
+ -r, --[no-]reverse Reverse the list of input files to download most recent first
11
+ -z, --sleep_interval seconds Sleep this many seconds between files
12
+ -h, --help Show this message
13
+ =end
14
+
15
+
16
+ require 'awesome_print'
17
+ require 'fileutils'
18
+ require 'optparse'
19
+ require 'ostruct'
20
+ require 'shellwords'
21
+ require_relative '../lib/get_tapas/downloader'
22
+ require_relative '../lib/get_tapas/options'
23
+ require_relative '../lib/get_tapas/page_parser'
24
+
25
+ def pry
26
+ require 'pry'
27
+ binding.pry
28
+ end
29
+
30
+
31
+ def process_options
32
+
33
+ option_parser = nil
34
+ output_help_and_terminate = -> { puts option_parser; puts; exit(-1) }
35
+
36
+ options = Options.new
37
+
38
+ OptionParser.new do |opts|
39
+
40
+ option_parser = opts
41
+
42
+ opts.banner = "Usage: get-tapas [options]"
43
+
44
+ opts.on('-d', '--data_dir dir', 'Directory for downloaded files (default: .)') do |value|
45
+ options.data_dir = value
46
+ end
47
+
48
+ opts.on('-i', '--input_spec spec',
49
+ %q{Filespec (or '-' for STDIN) containing Ruby Tapas download list page HTML}) do |value|
50
+ options.input_spec = value
51
+ end
52
+
53
+ opts.on('-0', '--min_episode_num number',
54
+ %q{Minimum episode number to download}) do |value|
55
+ options.min_episode_num = value.to_i
56
+ end
57
+
58
+ opts.on('-9', '--max_episode_num number',
59
+ %q{Maximum episode number to download}) do |value|
60
+ options.max_episode_num = value.to_i
61
+ end
62
+
63
+ opts.on('-n', '--[no-]no_op', "Don't download anything, just show what would have been downloaded.") do |value|
64
+ options.no_op = value
65
+ end
66
+
67
+ opts.on('-r', '--[no-]reverse', 'Reverse the list of input files to download most recent first') do |value|
68
+ options.reverse = value
69
+ end
70
+
71
+ opts.on('-z', '--sleep_interval seconds', 'Sleep this many seconds between files') do |value|
72
+ options.sleep_interval = value.to_f
73
+ end
74
+
75
+ opts.on_tail("-h", "--help", "Show this message") do
76
+ output_help_and_terminate.()
77
+ end
78
+
79
+ end.parse!
80
+
81
+ if ARGV.empty?
82
+ output_help_and_terminate.()
83
+ end
84
+
85
+ options
86
+ end
87
+
88
+
89
+ def get_html(options)
90
+ input = options.input_spec
91
+ if input == '-'
92
+ STDIN.read
93
+ else
94
+ File.read(input)
95
+ end
96
+ end
97
+
98
+
99
+ options = process_options
100
+ puts "Running get-tapas with the following options:"
101
+ ap options.to_h; puts
102
+
103
+ html = get_html(options)
104
+ Downloader.new(html, options).call
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/get_tapas.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'get_tapas/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'get_tapas'
8
+ spec.version = GetTapas::VERSION
9
+ spec.authors = ['Keith Bennett']
10
+ spec.email = ['keithrbennett@gmail.com']
11
+
12
+ spec.summary = %q{Downloads Ruby Tapas screencasts.}
13
+ spec.description = %q{Downloads Ruby Tapas screencasts en masse for paid subscribers.}
14
+ spec.homepage = 'https://github.com/keithrbennett/get-tapas'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+
37
+ spec.add_dependency 'awesome_print', '~> 1.7'
38
+ spec.add_dependency 'nokogiri', '~> 1.7'
39
+ end
@@ -0,0 +1,12 @@
1
+ class DownloadLink < Struct.new(
2
+ :url,
3
+ :filename,
4
+ :description,
5
+ :filespec,
6
+ :episode_num)
7
+
8
+ def to_s
9
+ to_h.to_s
10
+ end
11
+ end
12
+
@@ -0,0 +1,108 @@
1
+ class Downloader
2
+
3
+ attr_accessor :html, :options
4
+
5
+ def initialize(html, options)
6
+ @html = html
7
+ @options = options
8
+ end
9
+
10
+ def episode_num_ok(episode_num)
11
+ min = options.min_episode_num
12
+ max = options.max_episode_num
13
+
14
+ return false if min && episode_num < min
15
+ return false if max && episode_num > max
16
+ true
17
+ end
18
+
19
+
20
+ def ensure_output_dir_exists(dir)
21
+ return if Dir.exists?(dir)
22
+ begin
23
+ FileUtils.mkdir_p(dir)
24
+ puts "Created output data directory #{dir}."
25
+ rescue Errno::EACCES
26
+ puts "Unable to create directory #{dir}. Exiting..."
27
+ exit(-1)
28
+ end
29
+ end
30
+
31
+
32
+ def filespecs_available(data_dir, links)
33
+ filenames = links.map { |link| link.filename}
34
+ filenames.map { |fn| File.join(data_dir, fn) }
35
+ end
36
+
37
+
38
+ def filespecs_present(data_dir)
39
+ Dir["#{data_dir}/*"]
40
+ end
41
+
42
+
43
+ def filespecs_needing_download(available, present)
44
+ available - present
45
+ end
46
+
47
+
48
+ def download_file_info(links)
49
+ present, absent = links.partition do |link|
50
+ File.file?(link.filespec)
51
+ end
52
+ [links, present, absent]
53
+ end
54
+
55
+
56
+ def get_link_info(html, dir)
57
+ links = PageParser.parse(html)
58
+ links.each do |link|
59
+ link.filespec = File.join(dir, link.filename)
60
+ link.episode_num = link.filename.split('-').first.to_i
61
+ end
62
+ end
63
+
64
+
65
+ def download_file(link, data_dir)
66
+ puts "Downloading #{link.filespec}..."
67
+ tempfilespec = File.join(data_dir, 'tempfile')
68
+ `curl -o #{tempfilespec} #{Shellwords.shellescape(link.url)}`
69
+ if $?.exitstatus == 0
70
+ FileUtils.mv(tempfilespec, link.filespec)
71
+ puts "Finished downloading #{link.filename}\n\n"
72
+ else
73
+ raise "Curl Download failed with exit status #{$0.exitstatus}."
74
+ end
75
+ end
76
+
77
+
78
+ def call
79
+ data_dir = options.data_dir
80
+
81
+ ensure_output_dir_exists(data_dir)
82
+ links = get_link_info(html, data_dir)
83
+ _available, _present, absent = download_file_info(links)
84
+
85
+ absent.reverse! if options.reverse
86
+
87
+ absent_within_desired_range = absent.select { |a| episode_num_ok(a.episode_num)}
88
+
89
+ num_downloads_to_do = absent_within_desired_range.size
90
+
91
+ if num_downloads_to_do == 0
92
+ puts "No files within the selection criteria needed to be downloaded."
93
+ elsif options.no_op
94
+ puts "No-op mode requested. The following #{num_downloads_to_do} file(s) would have been downloaded without it:\n\n"
95
+ puts absent_within_desired_range.map(&:filename).join("\n"); puts
96
+ exit(0)
97
+ else
98
+ absent_within_desired_range.each_with_index do |link, index|
99
+ download_file(link, data_dir)
100
+ this_was_the_last_one = (index == num_downloads_to_do - 1)
101
+ if options.sleep_interval && (! this_was_the_last_one)
102
+ puts "Sleeping #{options.sleep_interval} seconds..."
103
+ sleep(options.sleep_interval)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,25 @@
1
+ class Options < Struct.new( \
2
+ :data_dir,
3
+ :input_spec,
4
+ :min_episode_num,
5
+ :max_episode_num,
6
+ :no_op,
7
+ :reverse,
8
+ :sleep_interval
9
+ )
10
+
11
+ DEFAULT_TAPAS_DIR = '~/ruby-tapas'
12
+
13
+ def initialize
14
+ super
15
+ self.data_dir ||= DEFAULT_TAPAS_DIR
16
+ end
17
+
18
+ # Convert '~' to $HOME if necessary.
19
+ def data_dir=(dir)
20
+ if dir.is_a?(String) && dir[0,2] == '~/'
21
+ dir = File.join(ENV['HOME'], dir[2..-1])
22
+ end
23
+ super(dir)
24
+ end
25
+ end
@@ -0,0 +1,27 @@
1
+ require 'mechanize'
2
+ require 'nokogiri'
3
+
4
+ require_relative 'download_link'
5
+
6
+ module PageParser
7
+
8
+
9
+ # Example Input: "https://rubytapas-media.s3.amazonaws.com/298-file-find.mp4?response-content-disposition=...
10
+ # Example Return: '298-file-find.mp4'
11
+ RUBY_TAPAS_URL_TO_FILENAME = ->(url) { url.split('?').first.split('/').last }
12
+
13
+
14
+ # @param html_string an HTML string from https://www.rubytapas.com/download-list/
15
+ # @return an array of DownloadLink instances.
16
+ def self.parse(html_string, fn_url_to_filename = RUBY_TAPAS_URL_TO_FILENAME)
17
+ html_doc = Nokogiri::HTML(html_string)
18
+ html_links = html_doc.xpath("//*[contains(@class, 'video-download-link')]")
19
+
20
+ html_links.map do |link|
21
+ url = link.children.first.attributes['href'].value
22
+ description = link.children.first.text.strip
23
+ filename = fn_url_to_filename.(url)
24
+ DownloadLink.new(url, filename, description)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module GetTapas
2
+ VERSION = "0.9.0"
3
+ end
data/lib/get_tapas.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "get_tapas/version"
2
+
3
+ module GetTapas
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: get_tapas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Keith Bennett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: awesome_print
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.7'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.7'
83
+ description: Downloads Ruby Tapas screencasts en masse for paid subscribers.
84
+ email:
85
+ - keithrbennett@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/get-tapas
99
+ - bin/setup
100
+ - get_tapas.gemspec
101
+ - lib/get_tapas.rb
102
+ - lib/get_tapas/download_link.rb
103
+ - lib/get_tapas/downloader.rb
104
+ - lib/get_tapas/options.rb
105
+ - lib/get_tapas/page_parser.rb
106
+ - lib/get_tapas/version.rb
107
+ homepage: https://github.com/keithrbennett/get-tapas
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.6.8
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Downloads Ruby Tapas screencasts.
131
+ test_files: []