download_tv 2.2.1 → 2.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,87 +1,79 @@
1
1
  module DownloadTV
2
-
3
- class MyEpisodes
4
-
5
- def initialize(user=nil, save_cookie)
6
- @agent = Mechanize.new
7
- @user = user
8
- @save_cookie = save_cookie
9
- @cookie_path = File.join(ENV["HOME"], ".config", "download_tv", "cookie")
10
- end
11
-
12
- def login
13
- if !@user || @user==""
14
- print "Enter your MyEpisodes username: "
15
- @user = STDIN.gets.chomp
16
- end
17
-
18
- print "Enter your MyEpisodes password: "
19
- pass = STDIN.noecho(&:gets).chomp
20
- puts
21
-
22
- page = @agent.get "https://www.myepisodes.com/login.php"
23
-
24
- login_form = page.forms[1]
25
- login_form.username = @user
26
- login_form.password = pass
27
-
28
- page = @agent.submit(login_form, login_form.buttons.first)
29
-
30
- raise InvalidLoginError if page.filename == "login.php"
31
-
32
- save_cookie() if @save_cookie
33
-
34
- @agent
35
-
36
- end
37
-
38
- def load_cookie
39
- if File.exist? @cookie_path
40
- @agent.cookie_jar.load @cookie_path
41
- page = @agent.get "https://www.myepisodes.com/login.php"
42
- if page.links[1].text == "Register"
43
- puts "The cookie is invalid/has expired."
44
- login
45
- end
46
- @agent
47
- else
48
- puts "Cookie file not found"
49
- login
50
- end
51
-
52
- end
53
-
54
- def save_cookie
55
- @agent.cookie_jar.save(@cookie_path, session: true)
56
- @agent
57
-
58
- end
59
-
60
- def get_shows(last)
61
- page = @agent.get "https://www.myepisodes.com/ajax/service.php?mode=view_privatelist"
62
- shows = page.parser.css('tr.past')
63
-
64
- s = shows.select do |i|
65
- airdate = i.css('td.date')[0].text
66
- Date.parse(airdate) >= last
67
- end
68
-
69
- s.map do |i|
70
- name = i.css('td.showname').text
71
- ep = i.css('td.longnumber').text
72
-
73
- ep.insert(0, "S")
74
- ep.sub!("x", "E")
75
-
76
- "#{name} #{ep}"
77
- end
78
-
79
- end
80
-
81
- end
82
-
83
- class InvalidLoginError < StandardError
84
-
85
- end
86
-
87
- end
2
+ ##
3
+ # API wrapper for MyEpisodes
4
+ class MyEpisodes
5
+ def initialize(user, save_cookie)
6
+ @agent = Mechanize.new
7
+ @user = user
8
+ @save_cookie = save_cookie
9
+ @cookie_path = File.join(ENV['HOME'], '.config', 'download_tv', 'cookie')
10
+ end
11
+
12
+ def login
13
+ if !@user || @user == ''
14
+ print 'Enter your MyEpisodes username: '
15
+ @user = STDIN.gets.chomp
16
+ end
17
+
18
+ print 'Enter your MyEpisodes password: '
19
+ pass = STDIN.noecho(&:gets).chomp
20
+ puts
21
+
22
+ page = @agent.get 'https://www.myepisodes.com/login.php'
23
+
24
+ login_form = page.forms[1]
25
+ login_form.username = @user
26
+ login_form.password = pass
27
+
28
+ page = @agent.submit(login_form, login_form.buttons.first)
29
+
30
+ raise InvalidLoginError if page.filename == 'login.php'
31
+
32
+ save_cookie if @save_cookie
33
+
34
+ @agent
35
+ end
36
+
37
+ def load_cookie
38
+ if File.exist? @cookie_path
39
+ @agent.cookie_jar.load @cookie_path
40
+ page = @agent.get 'https://www.myepisodes.com/login.php'
41
+ if page.links[1].text == 'Register'
42
+ puts 'The cookie is invalid/has expired.'
43
+ login
44
+ end
45
+ @agent
46
+ else
47
+ puts 'Cookie file not found'
48
+ login
49
+ end
50
+ end
51
+
52
+ def save_cookie
53
+ @agent.cookie_jar.save(@cookie_path, session: true)
54
+ @agent
55
+ end
56
+
57
+ def get_shows(last)
58
+ page = @agent.get 'https://www.myepisodes.com/ajax/service.php?mode=view_privatelist'
59
+ shows = page.parser.css('tr.past')
60
+
61
+ s = shows.select do |i|
62
+ airdate = i.css('td.date')[0].text
63
+ Date.parse(airdate) >= last
64
+ end
65
+
66
+ s.map do |i|
67
+ name = i.css('td.showname').text
68
+ ep = i.css('td.longnumber').text
69
+
70
+ ep.insert(0, 'S')
71
+ ep.sub!('x', 'E')
72
+
73
+ "#{name} #{ep}"
74
+ end
75
+ end
76
+ end
77
+
78
+ class InvalidLoginError < StandardError; end
79
+ end
@@ -1,20 +1,15 @@
1
1
  module DownloadTV
2
-
3
- class Subtitles
4
-
5
- def initialize
6
- @a = Addic7ed.new
7
-
8
- end
9
-
10
- def get_subs(show)
11
- @a.get_subs(show)
12
-
13
- rescue NoSubtitlesError
14
- puts "No subtitles found for #{show}"
15
-
16
- end
17
-
18
- end
19
-
20
- end
2
+ ##
3
+ # Manages the subtitles (WIP)
4
+ class Subtitles
5
+ def initialize
6
+ @a = Addic7ed.new
7
+ end
8
+
9
+ def get_subs(show)
10
+ @a.get_subs(show)
11
+ rescue NoSubtitlesError
12
+ puts "No subtitles found for #{show}"
13
+ end
14
+ end
15
+ end
@@ -1,84 +1,70 @@
1
1
  module DownloadTV
2
-
3
- class Torrent
4
-
5
- attr_reader :g_names, :g_instances, :n_grabbers
6
-
7
- def grabbers
8
- ["Eztv", "KAT", "ThePirateBay", "TorrentAPI"]
9
- end
10
-
11
- def initialize(default_grabber=nil)
12
- @g_names = grabbers
13
- @g_instances = Array.new
14
- @n_grabbers = @g_names.size # Initial size
15
- @tries = @n_grabbers - 1
16
-
17
- # Silently ignores bad names
18
- @g_names.rotate! @g_names.find_index(default_grabber).to_i+1
19
-
20
- change_grabbers
21
-
22
- end
23
-
24
-
25
- def change_grabbers
26
- if !@g_names.empty?
27
- # Instantiates the last element from g_names, popping it
28
- newt = (DownloadTV.const_get @g_names.pop).new
29
- newt.test_connection
30
-
31
- @g_instances.unshift newt
32
-
33
- else
34
- # Rotates the instantiated grabbers
35
- @g_instances.rotate!
36
-
37
- end
38
-
39
- rescue Mechanize::ResponseCodeError, Net::HTTP::Persistent::Error
40
-
41
- warn "Problem accessing #{newt.class.name}"
42
- # We won't be using this grabber
43
- @n_grabbers = @n_grabbers-1
44
- @tries = @n_grabbers - 1
45
-
46
- change_grabbers
47
-
48
- rescue SocketError, Errno::ECONNRESET, Net::OpenTimeout
49
- warn "Connection error."
50
- exit 1
51
-
52
- end
53
-
54
-
55
- def get_links(show)
56
- links = @g_instances.first.get_links(show)
57
-
58
- # Reset the counter
59
- @tries = @n_grabbers - 1
60
-
61
- links
62
-
63
- rescue NoTorrentsError
64
- puts "No torrents found for #{show} using #{@g_instances.first.class.name}"
65
-
66
- # Use next grabber
67
- if @tries > 0
68
- @tries-=1
69
- change_grabbers
70
- retry
71
-
72
- else # Reset the counter
73
- @tries = @n_grabbers - 1
74
- # Handle show not found here!!
75
- return []
76
-
77
- end
78
-
79
- end
80
-
81
-
82
- end
83
-
84
- end
2
+ ##
3
+ # Class in charge of managing the link grabbers
4
+ class Torrent
5
+ attr_reader :g_names, :g_instances, :n_grabbers
6
+
7
+ def grabbers
8
+ %w[Eztv KAT ThePirateBay TorrentAPI]
9
+ end
10
+
11
+ def initialize(default_grabber = nil)
12
+ @g_names = grabbers
13
+ @g_instances = []
14
+ @n_grabbers = @g_names.size # Initial size
15
+ @tries = @n_grabbers - 1
16
+
17
+ # Silently ignores bad names
18
+ @g_names.rotate! @g_names.find_index(default_grabber).to_i + 1
19
+
20
+ change_grabbers
21
+ end
22
+
23
+ def change_grabbers
24
+ if !@g_names.empty?
25
+ # Instantiates the last element from g_names, popping it
26
+ newt = (DownloadTV.const_get @g_names.pop).new
27
+ newt.test_connection
28
+
29
+ @g_instances.unshift newt
30
+
31
+ else
32
+ # Rotates the instantiated grabbers
33
+ @g_instances.rotate!
34
+ end
35
+ rescue Mechanize::ResponseCodeError, Net::HTTP::Persistent::Error
36
+ warn "Problem accessing #{newt.class.name}"
37
+ # We won't be using this grabber
38
+ @n_grabbers -= 1
39
+ @tries = @n_grabbers - 1
40
+
41
+ change_grabbers
42
+ rescue SocketError, Errno::ECONNRESET, Net::OpenTimeout
43
+ warn 'Connection error.'
44
+ exit 1
45
+ end
46
+
47
+ def get_links(show)
48
+ links = @g_instances.first.get_links(show)
49
+
50
+ # Reset the counter
51
+ @tries = @n_grabbers - 1
52
+
53
+ links
54
+ rescue NoTorrentsError
55
+ puts "No torrents found for #{show} using #{@g_instances.first.class.name}"
56
+
57
+ # Use next grabber
58
+ if @tries > 0
59
+ @tries -= 1
60
+ change_grabbers
61
+ retry
62
+
63
+ else # Reset the counter
64
+ @tries = @n_grabbers - 1
65
+ # Handle show not found here!!
66
+ return []
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module DownloadTV
2
- VERSION = "2.2.1"
2
+ VERSION = '2.2.2'.freeze
3
3
  end
data/test/config_test.rb CHANGED
@@ -1,172 +1,170 @@
1
- require "test_helper"
1
+ require 'test_helper'
2
2
 
3
3
  describe DownloadTV::Configuration do
4
- config_path = File.realdirpath("#{__dir__}/test_config")
5
-
6
- before do
7
- Dir.chdir(__dir__)
8
- end
9
-
10
- after do
11
- File.delete(config_path) if File.exist?(config_path)
12
- end
13
-
14
- describe "when the file already exists" do
15
- it "will load the existing configuration (blank)" do
16
- create_dummy_config(config_path)
17
-
18
- c = DownloadTV::Configuration.new(path: config_path)
19
- c.content.must_equal ({path: config_path, version: DownloadTV::VERSION})
20
- end
21
-
22
- it "will load the existing configuration (existing)" do
23
- create_dummy_config(config_path, auto: false, myepisodes_user: "dummy")
24
-
25
- c = DownloadTV::Configuration.new(path: config_path)
26
- c.content.must_equal ({path: config_path, auto: false, myepisodes_user: "dummy", version: DownloadTV::VERSION})
27
- end
28
-
29
- it "will get overwritten by the parameters given" do
30
- create_dummy_config(config_path, myepisodes_user: "dummy")
31
-
32
- c = DownloadTV::Configuration.new(path: config_path, myepisodes_user: "fake")
33
- c.content.must_equal ({path: config_path, myepisodes_user: "fake", version: DownloadTV::VERSION})
34
- end
35
-
36
- it "will downcase ignored shows" do
37
- create_dummy_config(config_path, ignored: ["duMMy", "String"])
38
-
39
- c = DownloadTV::Configuration.new(path: config_path)
40
- c.content[:ignored].must_equal ["dummy", "string"]
41
- end
42
- end
43
-
44
- describe "the breaking_changes method" do
45
- it "returns nil when both versions are equal" do
46
- create_dummy_config(config_path)
47
-
48
- c = DownloadTV::Configuration.new(path: config_path)
49
- c.breaking_changes?(DownloadTV::VERSION).must_be_nil
50
- end
51
-
52
- it "returns true when there's been a major update" do
53
- create_dummy_config(config_path)
54
-
55
- split = DownloadTV::VERSION.split(".")
56
- split[0] = (split[0].to_i+1).to_s
57
- new_version = split.join(".")
58
- c = DownloadTV::Configuration.new(path: config_path)
59
- c.breaking_changes?(new_version).must_equal true
60
- end
61
-
62
- it "returns true when there's been a minor update" do
63
- create_dummy_config(config_path)
64
-
65
- split = DownloadTV::VERSION.split(".")
66
- split[1] = (split[1].to_i+1).to_s
67
- new_version = split.join(".")
68
- c = DownloadTV::Configuration.new(path: config_path)
69
- c.breaking_changes?(new_version).must_equal true
70
- end
71
-
72
- it "returns false when it's a small patch" do
73
- create_dummy_config(config_path)
74
-
75
- split = DownloadTV::VERSION.split(".")
76
- split[2] = (split[2].to_i+1).to_s
77
- new_version = split.join(".")
78
- c = DownloadTV::Configuration.new(path: config_path)
79
- c.breaking_changes?(new_version).must_equal false
80
- end
81
- end
82
-
83
- describe "when the file doesn't exist" do
84
- it "will create a new one" do
85
- run_silently do
86
- STDIN.stub :gets, "myepisodes\ncookie\nignored" do
87
- DownloadTV::Configuration.new(path: config_path)
88
- end
89
- end
90
-
91
- File.exist?(config_path).must_equal true
92
- end
93
-
94
- it "will have the right values" do
95
- c = nil
96
- run_silently do
97
- STDIN.stub :gets, "anything" do
98
- c = DownloadTV::Configuration.new(path: config_path)
99
- end
100
- end
101
-
102
- c.content[:myepisodes_user].must_equal "anything"
103
- c.content[:cookie].must_equal true
104
- c.content[:ignored].must_equal ["anything"]
105
- c.content[:auto].must_equal true
106
- c.content[:subs].must_equal true
107
- c.content[:grabber].must_equal "TorrentAPI"
108
- c.content[:date].must_equal Date.today-1
109
- c.content[:version].must_equal DownloadTV::VERSION
110
-
111
- end
112
-
113
- it "will set the cookie value to false when explicitly told so" do
114
- c = nil
115
- run_silently do
116
- STDIN.stub :gets, "n" do
117
- c = DownloadTV::Configuration.new(path: config_path)
118
- end
119
- end
120
-
121
- c.content[:cookie].must_equal false
122
- end
123
-
124
- it "will separate the ignored values by commas" do
125
- c = nil
126
- run_silently do
127
- STDIN.stub :gets, "ignored1, itsgone, ignored 2" do
128
- c = DownloadTV::Configuration.new(path: config_path)
129
- end
130
- end
131
- c.content[:ignored].must_equal ["ignored1", "itsgone", "ignored 2"]
132
- end
133
- end
134
-
135
- describe "the serialize method" do
136
- it "stores the configuration in a Marshal'd file" do
137
- # Calls serialize
138
- run_silently do
139
- STDIN.stub :gets, "anything" do
140
- DownloadTV::Configuration.new(path: config_path)
141
- end
142
- end
143
- content = File.open(config_path, "rb") { |f| Marshal.load(f) }
144
-
145
- content[:cookie].must_equal true
146
- content[:myepisodes_user].must_equal "anything"
147
- content[:ignored].must_equal ["anything"]
148
- content[:auto].must_equal true
149
- content[:subs].must_equal true
150
- content[:grabber].must_equal "TorrentAPI"
151
- content[:date].must_equal Date.today-1
152
- content[:version].must_equal DownloadTV::VERSION
153
- end
154
- end
155
-
156
- describe "the constructor" do
157
- it "will trigger a configuration change when asked to" do
158
- create_dummy_config(config_path, auto: false)
159
- File.exist?(config_path).must_equal true
160
- c = nil
161
-
162
- run_silently do
163
- STDIN.stub :gets, "anything" do
164
- c = DownloadTV::Configuration.new(path: config_path)
165
- end
166
- end
167
-
168
- c.content[:auto].must_equal false
169
- end
170
- end
171
-
172
- end
4
+ config_path = File.realdirpath("#{__dir__}/test_config")
5
+
6
+ before do
7
+ Dir.chdir(__dir__)
8
+ end
9
+
10
+ after do
11
+ File.delete(config_path) if File.exist?(config_path)
12
+ end
13
+
14
+ describe 'when the file already exists' do
15
+ it 'will load the existing configuration (blank)' do
16
+ create_dummy_config(config_path)
17
+
18
+ c = DownloadTV::Configuration.new(path: config_path)
19
+ c.content.must_equal(path: config_path, version: DownloadTV::VERSION)
20
+ end
21
+
22
+ it 'will load the existing configuration (existing)' do
23
+ create_dummy_config(config_path, auto: false, myepisodes_user: 'dummy')
24
+
25
+ c = DownloadTV::Configuration.new(path: config_path)
26
+ c.content.must_equal(path: config_path, auto: false, myepisodes_user: 'dummy', version: DownloadTV::VERSION)
27
+ end
28
+
29
+ it 'will get overwritten by the parameters given' do
30
+ create_dummy_config(config_path, myepisodes_user: 'dummy')
31
+
32
+ c = DownloadTV::Configuration.new(path: config_path, myepisodes_user: 'fake')
33
+ c.content.must_equal(path: config_path, myepisodes_user: 'fake', version: DownloadTV::VERSION)
34
+ end
35
+
36
+ it 'will downcase ignored shows' do
37
+ create_dummy_config(config_path, ignored: %w[duMMy String])
38
+
39
+ c = DownloadTV::Configuration.new(path: config_path)
40
+ c.content[:ignored].must_equal %w[dummy string]
41
+ end
42
+ end
43
+
44
+ describe 'the breaking_changes method' do
45
+ it 'returns nil when both versions are equal' do
46
+ create_dummy_config(config_path)
47
+
48
+ c = DownloadTV::Configuration.new(path: config_path)
49
+ c.breaking_changes?(DownloadTV::VERSION).must_be_nil
50
+ end
51
+
52
+ it "returns true when there's been a major update" do
53
+ create_dummy_config(config_path)
54
+
55
+ split = DownloadTV::VERSION.split('.')
56
+ split[0] = (split[0].to_i + 1).to_s
57
+ new_version = split.join('.')
58
+ c = DownloadTV::Configuration.new(path: config_path)
59
+ c.breaking_changes?(new_version).must_equal true
60
+ end
61
+
62
+ it "returns true when there's been a minor update" do
63
+ create_dummy_config(config_path)
64
+
65
+ split = DownloadTV::VERSION.split('.')
66
+ split[1] = (split[1].to_i + 1).to_s
67
+ new_version = split.join('.')
68
+ c = DownloadTV::Configuration.new(path: config_path)
69
+ c.breaking_changes?(new_version).must_equal true
70
+ end
71
+
72
+ it "returns false when it's a small patch" do
73
+ create_dummy_config(config_path)
74
+
75
+ split = DownloadTV::VERSION.split('.')
76
+ split[2] = (split[2].to_i + 1).to_s
77
+ new_version = split.join('.')
78
+ c = DownloadTV::Configuration.new(path: config_path)
79
+ c.breaking_changes?(new_version).must_equal false
80
+ end
81
+ end
82
+
83
+ describe "when the file doesn't exist" do
84
+ it 'will create a new one' do
85
+ run_silently do
86
+ STDIN.stub :gets, 'myepisodes\ncookie\nignored' do
87
+ DownloadTV::Configuration.new(path: config_path)
88
+ end
89
+ end
90
+
91
+ File.exist?(config_path).must_equal true
92
+ end
93
+
94
+ it 'will have the right values' do
95
+ c = nil
96
+ run_silently do
97
+ STDIN.stub :gets, 'anything' do
98
+ c = DownloadTV::Configuration.new(path: config_path)
99
+ end
100
+ end
101
+
102
+ c.content[:myepisodes_user].must_equal 'anything'
103
+ c.content[:cookie].must_equal true
104
+ c.content[:ignored].must_equal ['anything']
105
+ c.content[:auto].must_equal true
106
+ c.content[:subs].must_equal true
107
+ c.content[:grabber].must_equal 'TorrentAPI'
108
+ c.content[:date].must_equal(Date.today - 1)
109
+ c.content[:version].must_equal DownloadTV::VERSION
110
+ end
111
+
112
+ it 'will set the cookie value to false when explicitly told so' do
113
+ c = nil
114
+ run_silently do
115
+ STDIN.stub :gets, 'n' do
116
+ c = DownloadTV::Configuration.new(path: config_path)
117
+ end
118
+ end
119
+
120
+ c.content[:cookie].must_equal false
121
+ end
122
+
123
+ it 'will separate the ignored values by commas' do
124
+ c = nil
125
+ run_silently do
126
+ STDIN.stub :gets, 'ignored1, itsgone, ignored 2' do
127
+ c = DownloadTV::Configuration.new(path: config_path)
128
+ end
129
+ end
130
+ c.content[:ignored].must_equal ['ignored1', 'itsgone', 'ignored 2']
131
+ end
132
+ end
133
+
134
+ describe 'the serialize method' do
135
+ it 'stores the configuration in a Marshalled file' do
136
+ # Calls serialize
137
+ run_silently do
138
+ STDIN.stub :gets, 'anything' do
139
+ DownloadTV::Configuration.new(path: config_path)
140
+ end
141
+ end
142
+ content = File.open(config_path, 'rb') { |f| Marshal.load(f) }
143
+
144
+ content[:cookie].must_equal true
145
+ content[:myepisodes_user].must_equal 'anything'
146
+ content[:ignored].must_equal ['anything']
147
+ content[:auto].must_equal true
148
+ content[:subs].must_equal true
149
+ content[:grabber].must_equal 'TorrentAPI'
150
+ content[:date].must_equal(Date.today - 1)
151
+ content[:version].must_equal DownloadTV::VERSION
152
+ end
153
+ end
154
+
155
+ describe 'the constructor' do
156
+ it 'will trigger a configuration change when asked to' do
157
+ create_dummy_config(config_path, auto: false)
158
+ File.exist?(config_path).must_equal true
159
+ c = nil
160
+
161
+ run_silently do
162
+ STDIN.stub :gets, 'anything' do
163
+ c = DownloadTV::Configuration.new(path: config_path)
164
+ end
165
+ end
166
+
167
+ c.content[:auto].must_equal false
168
+ end
169
+ end
170
+ end