comer_de_tapas 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 581ba0c2dd6e3afcccaa2f54d488abf8bb7039d7
4
- data.tar.gz: b7f0040150ca17fac7d81580a844901003558ece
3
+ metadata.gz: c97e323c1838461bc221ef30b5549c6fb1bc52c8
4
+ data.tar.gz: 02b0a3e9814c081c618d8d2ac2a70a5a5a6a1eeb
5
5
  SHA512:
6
- metadata.gz: df5f387b94fcb6c1b36860e27787c040b39c6141e77e27895105b800074fb7cb2ee947df0d46f09664b2c2eb873b10c1ac6cf79b6a12cbfdead7961b0711fcb9
7
- data.tar.gz: d4979d6e54fd92bf1dc2e46e2b77f2c8cabec22208b447922c7d60031fb8251c24cef320cd69075f3772c2570e20b437d621b3547e718848a614dd6faf611859
6
+ metadata.gz: 7812e1ee92b1ef2cbaf6c4897dc2f4ae91c775d474f456750660d63e6ddb817ef2cfaf0708bf0ec2a9f46f7b7ac14d816a5ae537342eeba863dde87b21d8a630
7
+ data.tar.gz: 370a445ca90681db21e53430f416346f7075a77cd2f46610f4ed9007e7e389ed3f9bfef520c8fe7a2dd9d65629839977b368eab3a1e24679bddfb1c3c0134c4f
data/README.md CHANGED
@@ -46,6 +46,8 @@ If you want to save all Ruby Tapas episodes in your home folder. Please write it
46
46
  Usage
47
47
  -----
48
48
 
49
+ After you filled subscription information, you can download episodes via this command:
50
+
49
51
  $ comer_de_tapas download
50
52
 
51
53
  Will fetch all episodes, then start to download it.
@@ -16,7 +16,8 @@ module ComerDeTapas
16
16
  desc 'init', 'Create config folder and files'
17
17
  def init
18
18
  @client.init!
19
- say 'Please fill in your subscription info in ~/.rubytapas/.credentials'
19
+ say 'Please fill in your subscription info in ~/.rubytapas/.credentials.'
20
+ say 'When you finished, type `comer_de_tapas download` to download.'
20
21
  end
21
22
 
22
23
  # -f, --force, options[:force] => true, otherwise nil.
@@ -10,7 +10,7 @@ module ComerDeTapas
10
10
  # $ mkdir -p ~/.rubytapas/
11
11
  # $ touch ~/.rubytapas/.credentials
12
12
  def init!
13
- if File.exist?(RUBYTAPAS_DIR) && File.exist?(CREDENTIAL_FILE)
13
+ if RUBYTAPAS_DIR.exist? && CREDENTIAL_FILE.exist?
14
14
  abort 'Credentials found. type `comer_de_tapas download` to download.'
15
15
  end
16
16
  create_rubytapas_files!
@@ -21,17 +21,19 @@ module ComerDeTapas
21
21
  # Parse it to episode, save episodes data as json to ~/.rubytapas.json
22
22
  def fetch_episodes! force=nil
23
23
  return puts 'Use cached episode data.' if fresh? && force.nil?
24
+ puts 'Force fetching. Getting latest Ruby Tapas...' if force
24
25
  puts 'Fetching episodes...'
25
- get_feed_with_basic_auth
26
- save_feed_data parse_xml_feed
27
- puts 'Episodes successfully fetched.'
26
+ if get_feed_with_basic_auth
27
+ save_feed_data parse_xml_feed
28
+ puts 'Episodes successfully fetched and saved.'
29
+ end
28
30
  end
29
31
 
30
32
  # Create user specified folder: credentials[:save_path]
31
33
  def prepare_save_folder!
32
- return puts "#{save_folder} found." if File.exist? save_folder
34
+ return puts "#{save_folder} found." if save_folder.exist?
33
35
 
34
- FileUtils.mkdir_p save_folder.expand_path
36
+ save_folder.mkpath
35
37
  puts "#{save_folder} created."
36
38
  end
37
39
 
@@ -42,26 +44,26 @@ module ComerDeTapas
42
44
 
43
45
  # Load episodes json from EPISODES_JSON_FILE
44
46
  def load_episodes
45
- @episodes ||= JSON.parse(File.read EPISODES_JSON_FILE)
47
+ @episodes ||= JSON.parse(EPISODES_JSON_FILE.read)
46
48
  end
47
49
 
48
50
  # User spefified folder to save episodes.
49
51
  # @return [Pathname]
50
52
  def save_folder
51
- Pathname credentials[:save_path]
53
+ Pathname(credentials[:save_path]).expand_path
52
54
  end
53
55
 
54
56
  # Download episode in parallel using Actors
55
57
  # Powered by Celluloid::IO
56
58
  def download_all_tapas!
57
59
  episodes.each do |episode|
58
- FileUtils.cd(save_folder.expand_path) do
60
+ FileUtils.cd(save_folder) do
59
61
  episode_title = episode['title']
60
62
  puts "Downloading Epsiode #{episode_title}..."
61
63
 
62
- episode_folder = save_folder.join(sanitized episode_title).expand_path
64
+ episode_folder = save_folder.join(sanitized episode_title)
63
65
 
64
- FileUtils.mkdir_p episode_folder unless File.exist? episode_folder
66
+ FileUtils.mkdir_p episode_folder unless episode_folder.exist?
65
67
 
66
68
  FileUtils.cd episode_folder do
67
69
  fetcher = Fetcher.new
@@ -89,8 +91,8 @@ module ComerDeTapas
89
91
  # @param [Array] downloadables
90
92
  def download_parallelly! downloadables
91
93
  downloadables.compact.each do |file, future|
92
- response = future.value.to_s
93
94
  puts "Downloading #{file}..."
95
+ response = future.value.to_s
94
96
  IO.write file, response
95
97
  puts "#{file} saved."
96
98
  end
@@ -100,19 +102,30 @@ module ComerDeTapas
100
102
  # @param [Array] file_and_links
101
103
  def find_downloadables file_and_links, fetcher
102
104
  file_and_links.map do |file_and_link|
103
- next if File.exist? file_and_link['filename']
105
+ file_name = file_and_link['filename']
106
+
107
+ # mp4 less than 3MB considered as unfinished. Redownload it.
108
+ FileUtils.rm file_name if small_mp4? file_name
109
+
110
+ next if File.exist? file_name
104
111
 
105
112
  q, v = file_and_link['link'].split('?').last.split('=')
106
- [file_and_link['filename'], fetcher.future.fetch(DOWNLOAD_URL, cookie, { q => v })]
113
+ [file_name, fetcher.future.fetch(DOWNLOAD_URL, cookie, { q => v })]
107
114
  end
108
115
  end
109
116
 
117
+ # Return true if file is a mp4 and its size less than 3MB.
118
+ def small_mp4?(file)
119
+ return false unless File.exist? file
120
+ File.size(file) < 3*1024*1024 && File.extname(file) == '.mp4'
121
+ end
122
+
110
123
  # mkdir -p ~/.rubytapas
111
124
  # touch ~/.rubytapas/.credentials
112
125
  def create_rubytapas_files!
113
- FileUtils.mkdir_p RUBYTAPAS_DIR
126
+ RUBYTAPAS_DIR.mkpath
114
127
  FileUtils.touch CREDENTIAL_FILE
115
- IO.write CREDENTIAL_FILE, credential_template
128
+ CREDENTIAL_FILE.write credential_template
116
129
  end
117
130
 
118
131
  # Use to create empty credential file
@@ -124,8 +137,16 @@ module ComerDeTapas
124
137
  # Get raw feed data (XML), RSS
125
138
  def get_feed_with_basic_auth
126
139
  puts 'Authorizing...'
127
- @feed_xml = HTTP.auth(:basic, authenticate_params).get(FEED_URL).body.to_s
128
- puts 'Authroized.'
140
+ response = HTTP.auth(:basic, authenticate_params).get(FEED_URL).body.to_s
141
+
142
+ if response.empty?
143
+ abort "Authroized failed. Please check your email & password in #{CREDENTIAL_FILE}"
144
+ end
145
+
146
+ if @feed_xml = response
147
+ puts 'Authroized.'
148
+ return true
149
+ end
129
150
  end
130
151
 
131
152
  # Params for basic authentication
@@ -143,6 +164,7 @@ module ComerDeTapas
143
164
  # @return [Array<Hash>]
144
165
  def parse_xml_feed
145
166
  puts 'Parsing Episodes...'
167
+
146
168
  require 'nokogiri'
147
169
  items = Nokogiri::XML(feed_xml).xpath('//item')
148
170
 
@@ -176,7 +198,7 @@ module ComerDeTapas
176
198
  # Write episodes data to ~/.rubytapas/episodes.json
177
199
  def save_feed_data feed
178
200
  puts 'Saving episodes data to ~/.rubytapas/episodes.json...'
179
- IO.write EPISODES_JSON_FILE, feed.to_json
201
+ EPISODES_JSON_FILE.write feed.to_json
180
202
  puts 'Saved.'
181
203
  end
182
204
 
@@ -200,8 +222,8 @@ module ComerDeTapas
200
222
  # If the episodes json was made of 259_200.seconds.ago (3 days)
201
223
  # @return [Boolean] true if episodes.json creation time < 3 days
202
224
  def fresh?
203
- return false unless File.exist? EPISODES_JSON_FILE
204
- Time.now - File.ctime(EPISODES_JSON_FILE) < 259_200
225
+ return false unless EPISODES_JSON_FILE.exist?
226
+ Time.now - EPISODES_JSON_FILE.ctime < 259_200
205
227
  end
206
228
  end
207
229
  end
@@ -3,7 +3,7 @@ module ComerDeTapas
3
3
  KEYS = %w(email password save_path)
4
4
 
5
5
  def initialize
6
- abort 'Please run `comer_de_tapas init` first' unless File.exist? CREDENTIAL_FILE
6
+ abort 'Please run `comer_de_tapas init` first' unless CREDENTIAL_FILE.exist?
7
7
 
8
8
  set_subscription_data if subscription_data_valid?
9
9
  end
@@ -30,7 +30,7 @@ module ComerDeTapas
30
30
  # 65 is when you have a very short email, password, and save_path.
31
31
  # So when you filled in data, probably will > 65.
32
32
  if File.size(CREDENTIAL_FILE) < 65
33
- puts "Did you fill in your subscription data in #{CREDENTIAL_FILE}?"
33
+ abort "Did you fill in your subscription data in #{CREDENTIAL_FILE}?"
34
34
  return false
35
35
  end
36
36
 
@@ -1,3 +1,3 @@
1
1
  module ComerDeTapas
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: comer_de_tapas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juanito Fatas