addic7ed 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,106 @@
1
+ require 'nokogiri'
2
+ require 'net/http'
3
+ require 'open-uri'
4
+
5
+ module Addic7ed
6
+ class Episode
7
+
8
+ attr_reader :filename
9
+
10
+ def initialize(filename)
11
+ @filename = Addic7ed::Filename.new(filename)
12
+ end
13
+
14
+ def url(lang = 'fr')
15
+ raise LanguageNotSupported unless LANGUAGES[lang]
16
+ @localized_urls ||= {}
17
+ @localized_urls[lang] ||= "http://www.addic7ed.com/serie/#{@filename.showname.gsub(' ', '_')}/#{@filename.season}/#{@filename.episode}/#{LANGUAGES[lang][:id]}"
18
+ end
19
+
20
+ def subtitles(lang = 'fr')
21
+ raise LanguageNotSupported unless LANGUAGES[lang]
22
+ unless @subtitles and @subtitles[lang]
23
+ @subtitles ||= {}
24
+ @subtitles[lang] ||= []
25
+ response = Net::HTTP.get_response(URI(url(lang)))
26
+ raise EpisodeNotFound if response.body == " "
27
+ doc = Nokogiri::HTML(response.body)
28
+ raise NoSubtitleFound unless doc.css('select#filterlang ~ font[color="yellow"]').empty?
29
+ sublist_node = doc.css('#container95m table.tabel95 table.tabel95')
30
+ raise NoSubtitleFound if sublist_node.size == 0
31
+ sublist_node.each do |sub_node|
32
+ @subtitles[lang] << parse_subtitle_node(sub_node, lang)
33
+ end
34
+ end
35
+ @subtitles[lang]
36
+ end
37
+
38
+ def best_subtitle(lang = 'fr')
39
+ raise LanguageNotSupported unless LANGUAGES[lang]
40
+ unless @best_subtitle and @best_subtitle[lang]
41
+ @best_subtitle ||= {}
42
+ subtitles(lang).each do |sub|
43
+ if sub.status == 'Completed' and (sub.version == @filename.group or COMPATIBILITY_720P[sub.version] == @filename.group)
44
+ @best_subtitle[lang] = sub unless @best_subtitle[lang] and @best_subtitle[lang].downloads > sub.downloads
45
+ end
46
+ end
47
+ raise NoSubtitleFound unless @best_subtitle[lang]
48
+ end
49
+ return @best_subtitle[lang]
50
+ end
51
+
52
+ def download_best_subtitle!(lang = 'fr', redirect_limit = 8)
53
+ raise WTFError.new('Too many HTTP redirects') if redirect_limit == 0
54
+ begin
55
+ uri = URI(best_subtitle(lang).url)
56
+ response = Net::HTTP.start(uri.hostname, uri.port) do |http|
57
+ request = Net::HTTP::Get.new(uri.request_uri)
58
+ # Addic7ed needs the Referer to be correct. User-agent is just here to fake a real browser request, because we never know...
59
+ request['Referer'] = url(lang)
60
+ request['User-Agent'] = USER_AGENTS.sample
61
+ http.request(request)
62
+ end
63
+ rescue Errno::ECONNREFUSED
64
+ raise DownloadLimitReached
65
+ rescue
66
+ raise DownloadError
67
+ end
68
+ if response.kind_of?(Net::HTTPRedirection)
69
+ # Addic7ed is serving redirection URL not-encoded.
70
+ # Ruby does not support it yet (see http://bugs.ruby-lang.org/issues/7396)
71
+ best_subtitle(lang).url = URI.escape(response['location'])
72
+ download_best_subtitle!(lang, redirect_limit - 1)
73
+ else
74
+ begin
75
+ open "#{filename}".gsub(/\.\w{3}$/, '.srt'), 'w' do |f|
76
+ f << response.body
77
+ end
78
+ rescue
79
+ raise SubtitleCannotBeSaved
80
+ end
81
+ end
82
+ end
83
+
84
+ protected
85
+
86
+ def parse_subtitle_node(sub_node, lang)
87
+ begin
88
+ version_node = sub_node.css('.NewsTitle').first
89
+ version = version_node.content.gsub(/ \nVersion /, '').gsub(/,.*/, '')
90
+ language_node = sub_node.css('.language').first
91
+ language = language_node.content.gsub(/\A\W*/, '').gsub(/[^\w\)]*\z/, '')
92
+ raise WTFError.new("We're asking for #{LANGUAGES[lang][:name].capitalize} subtitles and Addic7ed gives us #{language.capitalize} subtitles") if LANGUAGES[lang][:name].downcase != language.downcase
93
+ status_node = sub_node.css('tr:nth-child(3) td:nth-child(4) b').first
94
+ status = status_node.content.strip
95
+ url_node = sub_node.css('a.buttonDownload').first
96
+ url = 'http://www.addic7ed.com' + url_node['href']
97
+ downloads_node = sub_node.css('tr:nth-child(4) td.newsDate').first
98
+ downloads = /(?<downloads>\d*) Downloads/.match(downloads_node.content)[:downloads]
99
+ Addic7ed::Subtitle.new(version, language, status, url, downloads)
100
+ rescue
101
+ raise ParsingError
102
+ end
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,22 @@
1
+ module Addic7ed
2
+ class InvalidFilename < StandardError
3
+ end
4
+ class ShowNotFound < StandardError
5
+ end
6
+ class EpisodeNotFound < StandardError
7
+ end
8
+ class LanguageNotSupported < StandardError
9
+ end
10
+ class ParsingError < StandardError
11
+ end
12
+ class NoSubtitleFound < StandardError
13
+ end
14
+ class DownloadError < StandardError
15
+ end
16
+ class DownloadLimitReached < StandardError
17
+ end
18
+ class SubtitleCannotBeSaved < StandardError
19
+ end
20
+ class WTFError < StandardError
21
+ end
22
+ end
@@ -0,0 +1,50 @@
1
+ module Addic7ed
2
+ class Filename
3
+
4
+ TVSHOW_REGEX = /\A(?<showname>.*\w)[\[\. ]+S?(?<season>\d{1,2})[-\. ]?[EX]?(?<episode>\d{2})[\]\. ]+(?<tags>.*)-(?<group>\w*)(\.\w{3})?\z/i
5
+
6
+ attr_reader :filename, :showname, :season, :episode, :tags, :group
7
+
8
+ def initialize(filename)
9
+ @filename = filename
10
+ match = TVSHOW_REGEX.match basename
11
+ if match
12
+ @showname = match[:showname].gsub('.', ' ')
13
+ @season = match[:season].to_i
14
+ @episode = match[:episode].to_i
15
+ @tags = match[:tags].upcase.split(/[\. ]/)
16
+ @group = match[:group].upcase
17
+ else
18
+ raise InvalidFilename
19
+ end
20
+ end
21
+
22
+ # Lazy getters
23
+
24
+ def basename
25
+ @basename ||= File.basename(@filename)
26
+ end
27
+
28
+ def dirname
29
+ @dirname ||= File.dirname(@filename)
30
+ end
31
+
32
+ def extname
33
+ @extname ||= File.extname(@filename)
34
+ end
35
+
36
+ def to_s
37
+ @filename
38
+ end
39
+
40
+ def inspect
41
+ "Guesses for #{@filename}:
42
+ show: #{@showname}
43
+ season: #{@season}
44
+ episode: #{@episode}
45
+ tags: #{@tags}
46
+ group: #{@group}"
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,20 @@
1
+ module Addic7ed
2
+ class Subtitle
3
+
4
+ attr_reader :version, :language, :status, :downloads
5
+ attr_accessor :url
6
+
7
+ def initialize(version, language, status, url, downloads)
8
+ @version = version.upcase
9
+ @language = language
10
+ @status = status
11
+ @url = url
12
+ @downloads = downloads.to_i
13
+ end
14
+
15
+ def to_s
16
+ "#{url}\t->\t#{@version} (#{language}, #{status}) [#{@downloads} downloads]"
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require './lib/addic7ed'
3
+
4
+ describe Addic7ed do
5
+ it 'should define SHOWS_URL' do
6
+ Addic7ed::SHOWS_URL.should_not be_nil
7
+ end
8
+ it 'should define EPISODES_URL' do
9
+ Addic7ed::EPISODES_URL.should_not be_nil
10
+ end
11
+ it 'should define EPISODE_REDIRECT_URL' do
12
+ Addic7ed::EPISODE_REDIRECT_URL.should_not be_nil
13
+ end
14
+ it 'should define LANGUAGES' do
15
+ Addic7ed::LANGUAGES.should_not be_nil
16
+ Addic7ed::LANGUAGES['fr'].should == {name: 'French', id: 8}
17
+ end
18
+ end
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+ require './lib/addic7ed'
3
+
4
+ describe Addic7ed::Episode do
5
+ before :all do
6
+ @filename = 'Californication.S06E07.720p.HDTV.x264-2HD.mkv'
7
+ @filename_show_not_found = 'Show.Not.Found.S06E07.720p.HDTV.x264-2HD.mkv'
8
+ @filename_episode_not_found = 'Californication.S06E42.720p.HDTV.x264-2HD.mkv'
9
+ end
10
+
11
+ it 'should create valid instance given valid argument' do
12
+ expect{
13
+ @episode = Addic7ed::Episode.new(@filename)
14
+ }.to_not raise_error
15
+ end
16
+
17
+ describe '#url' do
18
+ before :all do
19
+ @episode = Addic7ed::Episode.new(@filename)
20
+ end
21
+ it 'should return a show localized URL given existing episode' do
22
+ @episode.url('fr').should == 'http://www.addic7ed.com/serie/Californication/6/7/8'
23
+ @episode.url('es').should == 'http://www.addic7ed.com/serie/Californication/6/7/4'
24
+ end
25
+ it 'should use French as default language' do
26
+ @episode.url.should == @episode.url('fr')
27
+ end
28
+ it 'should raise LanguageNotSupported given an unsupported language code' do
29
+ expect{
30
+ @episode.url('aa')
31
+ }.to raise_error(Addic7ed::LanguageNotSupported)
32
+ end
33
+ end
34
+
35
+ describe '#subtitles' do
36
+ before :all do
37
+ @filename = 'The.Walking.Dead.S03E02.720p.HDTV.x264-IMMERSE.mkv'
38
+ @episode = Addic7ed::Episode.new(@filename)
39
+ end
40
+ it 'should return an array of Addic7ed::Subtitle given valid episode and language' do
41
+ @episode.subtitles('fr').size.should == 4
42
+ @episode.subtitles('en').size.should == 3
43
+ @episode.subtitles('it').size.should == 1
44
+ end
45
+ it 'should use French as default language' do
46
+ @episode.subtitles.should == @episode.subtitles('fr')
47
+ end
48
+ it 'should raise LanguageNotSupported given an unsupported language code' do
49
+ expect{
50
+ @episode.subtitles('aa')
51
+ }.to raise_error(Addic7ed::LanguageNotSupported)
52
+ end
53
+ it 'should raise EpisodeNotFound given not existing episode' do
54
+ expect{
55
+ Addic7ed::Episode.new(@filename_episode_not_found).subtitles
56
+ }.to raise_error(Addic7ed::EpisodeNotFound)
57
+ end
58
+ it 'should raise NoSubtitleFound given valid episode which has no subtitle on Addic7ed' do
59
+ expect{
60
+ @episode.subtitles('es')
61
+ }.to raise_error(Addic7ed::NoSubtitleFound)
62
+ end
63
+ it 'may raise a ParsingError, but I don\'t know how to test it :-('
64
+ it 'may raise a WTFError, but it\'s unsure when, so it stays untested'
65
+ end
66
+
67
+ describe '#best_subtitle' do
68
+ before :all do
69
+ @filename = 'The.Walking.Dead.S03E03.720p.HDTV.x264-EVOLVE.mkv'
70
+ @filename_compatible_group = 'The.Walking.Dead.S03E06.720p.HDTV.x264-IMMERSE.mkv'
71
+ @episode = Addic7ed::Episode.new(@filename)
72
+ end
73
+ it 'should find the subtitle with status completed and same group name' do
74
+ @episode.best_subtitle('fr').url.should == 'http://www.addic7ed.com/updated/8/68290/2'
75
+ end
76
+ it 'should find the subtitle with status completed, compatible group name and as many downloads as possible' do
77
+ @episode = Addic7ed::Episode.new(@filename_compatible_group)
78
+ @episode.best_subtitle('fr').url.should == 'http://www.addic7ed.com/updated/8/68980/2'
79
+ end
80
+ it 'should use French as default language' do
81
+ @episode.best_subtitle.should == @episode.best_subtitle('fr')
82
+ end
83
+ it 'should raise LanguageNotSupported given an unsupported language code' do
84
+ expect{
85
+ @episode.best_subtitle('aa')
86
+ }.to raise_error(Addic7ed::LanguageNotSupported)
87
+ end
88
+ it 'should raise EpisodeNotFound given not existing episode' do
89
+ expect{
90
+ Addic7ed::Episode.new(@filename_episode_not_found).best_subtitle
91
+ }.to raise_error(Addic7ed::EpisodeNotFound)
92
+ end
93
+ it 'should raise NoSubtitleFound given valid episode which has no subtitle on Addic7ed' do
94
+ expect{
95
+ @episode.best_subtitle('es')
96
+ }.to raise_error(Addic7ed::NoSubtitleFound)
97
+ end
98
+ end
99
+
100
+ describe '#download_best_subtitle!' do
101
+ it 'should be tested, but I\'m not sure how to do it properly'
102
+ end
103
+ end
@@ -0,0 +1,286 @@
1
+ require 'spec_helper'
2
+ require './lib/addic7ed'
3
+
4
+ describe Addic7ed::Filename do
5
+
6
+ before :all do
7
+ # Valid filenames
8
+ @filename = 'Californication.S06E07.720p.HDTV.x264-2HD.mkv'
9
+ @filename_x = 'Californication.06x07.720p.HDTV.x264-2HD.mkv'
10
+ @filename_3digits = 'Californication.607.720p.HDTV.x264-2HD.mkv'
11
+ @filename_brackets = 'Californication.[S06E07].720p.HDTV.x264-2HD.mkv'
12
+ @filename_x_brackets = 'Californication.[6x07].720p.HDTV.x264-2HD.mkv'
13
+ @filename_3digits_brackets = 'Californication.[607].720p.HDTV.x264-2HD.mkv'
14
+ @filename_brackets_spaces = 'Californication [607] 720p.HDTV.x264-2HD.mkv'
15
+ @filename_x_brackets_spaces = 'Californication [6x07] 720p.HDTV.x264-2HD.mkv'
16
+ @filename_3digits_brackets_spaces = 'Californication [607] 720p.HDTV.x264-2HD.mkv'
17
+ @filename_lowercase = 'californication.s06e07.720p.hdtv.x264-2hd.mkv'
18
+ @filename_lowercase_x = 'californication.6x07.720p.hdtv.x264-2hd.mkv'
19
+ @filename_multiple_words = 'The.Walking.Dead.S03E11.720p.HDTV.x264-EVOLVE.mkv'
20
+ @filename_multiple_words_spaces = 'The Walking Dead S03E11 720p.HDTV.x264-EVOLVE.mkv'
21
+ @filename_numbers_only = '90210.S05E12.720p.HDTV.X264-DIMENSION.mkv'
22
+ @filename_showname_year = 'The.Americans.2013.S01E04.720p.HDTV.X264-DIMENSION.mkv'
23
+ @filename_full_path = '/data/public/Series/Californication/Saison 06/Californication.S06E07.720p.HDTV.x264-2HD.mkv'
24
+ @filename_relative_path = '../Saison 06/Californication.S06E07.720p.HDTV.x264-2HD.mkv'
25
+ @filename_no_extension = 'Californication.S06E07.720p.HDTV.x264-2HD'
26
+ # Invalid filenames
27
+ @filename_no_showname = '.S06E07.720p.HDTV.x264-2HD.mkv'
28
+ @filename_no_season = 'Californication.E07.720p.HDTV.x264-2HD.mkv'
29
+ @filename_no_episode = 'Californication.S06.720p.HDTV.x264-2HD.mkv'
30
+ @filename_no_tags = 'Californication.S06E07.2HD.mkv'
31
+ @filename_no_group = 'Californication.S06E07.720p.HDTV.x264.mkv'
32
+ end
33
+
34
+ it 'should succeed given valid argument' do
35
+ expect {
36
+ @file = Addic7ed::Filename.new(@filename)
37
+ }.to_not raise_error
38
+ @file.showname.should == 'Californication'
39
+ @file.season.should == 6
40
+ @file.episode.should == 7
41
+ @file.tags.should == ['720P', 'HDTV', 'X264']
42
+ @file.group.should == '2HD'
43
+ end
44
+
45
+ it 'should succeed given filename with x notation' do
46
+ expect {
47
+ @file = Addic7ed::Filename.new(@filename_x)
48
+ }.to_not raise_error
49
+ @file.showname.should == 'Californication'
50
+ @file.season.should == 6
51
+ @file.episode.should == 7
52
+ @file.tags.should == ['720P', 'HDTV', 'X264']
53
+ @file.group.should == '2HD'
54
+ end
55
+
56
+ it 'should succeed given filename with 3-digits notation' do
57
+ expect {
58
+ @file = Addic7ed::Filename.new(@filename_3digits)
59
+ }.to_not raise_error
60
+ @file.showname.should == 'Californication'
61
+ @file.season.should == 6
62
+ @file.episode.should == 7
63
+ @file.tags.should == ['720P', 'HDTV', 'X264']
64
+ @file.group.should == '2HD'
65
+ end
66
+
67
+ it 'should succeed given filename with brackets notation' do
68
+ expect {
69
+ @file = Addic7ed::Filename.new(@filename_brackets)
70
+ }.to_not raise_error
71
+ @file.showname.should == 'Californication'
72
+ @file.season.should == 6
73
+ @file.episode.should == 7
74
+ @file.tags.should == ['720P', 'HDTV', 'X264']
75
+ @file.group.should == '2HD'
76
+ end
77
+
78
+ it 'should succeed given filename with brackets and x notation' do
79
+ expect {
80
+ @file = Addic7ed::Filename.new(@filename_x_brackets)
81
+ }.to_not raise_error
82
+ @file.showname.should == 'Californication'
83
+ @file.season.should == 6
84
+ @file.episode.should == 7
85
+ @file.tags.should == ['720P', 'HDTV', 'X264']
86
+ @file.group.should == '2HD'
87
+ end
88
+
89
+ it 'should succeed given filename with brackets and 3-digits notation' do
90
+ expect {
91
+ @file = Addic7ed::Filename.new(@filename_3digits_brackets)
92
+ }.to_not raise_error
93
+ @file.showname.should == 'Californication'
94
+ @file.season.should == 6
95
+ @file.episode.should == 7
96
+ @file.tags.should == ['720P', 'HDTV', 'X264']
97
+ @file.group.should == '2HD'
98
+ end
99
+
100
+ it 'should succeed given filename with brackets notation and space separator' do
101
+ expect {
102
+ @file = Addic7ed::Filename.new(@filename_brackets_spaces)
103
+ }.to_not raise_error
104
+ @file.showname.should == 'Californication'
105
+ @file.season.should == 6
106
+ @file.episode.should == 7
107
+ @file.tags.should == ['720P', 'HDTV', 'X264']
108
+ @file.group.should == '2HD'
109
+ end
110
+
111
+ it 'should succeed given filename with brackets and x notation and space separator' do
112
+ expect {
113
+ @file = Addic7ed::Filename.new(@filename_x_brackets_spaces)
114
+ }.to_not raise_error
115
+ @file.showname.should == 'Californication'
116
+ @file.season.should == 6
117
+ @file.episode.should == 7
118
+ @file.tags.should == ['720P', 'HDTV', 'X264']
119
+ @file.group.should == '2HD'
120
+ end
121
+
122
+ it 'should succeed given filename with brackets and 3-digits notation and space separator' do
123
+ expect {
124
+ @file = Addic7ed::Filename.new(@filename_3digits_brackets_spaces)
125
+ }.to_not raise_error
126
+ @file.showname.should == 'Californication'
127
+ @file.season.should == 6
128
+ @file.episode.should == 7
129
+ @file.tags.should == ['720P', 'HDTV', 'X264']
130
+ @file.group.should == '2HD'
131
+ end
132
+
133
+ it 'should succeed given lowercase filename' do
134
+ expect {
135
+ @file = Addic7ed::Filename.new(@filename_lowercase)
136
+ }.to_not raise_error
137
+ @file.showname.should == 'californication'
138
+ @file.season.should == 6
139
+ @file.episode.should == 7
140
+ @file.tags.should == ['720P', 'HDTV', 'X264']
141
+ @file.group.should == '2HD'
142
+ end
143
+
144
+ it 'should succeed given lowercase filename with x notation' do
145
+ expect {
146
+ @file = Addic7ed::Filename.new(@filename_lowercase_x)
147
+ }.to_not raise_error
148
+ @file.showname.should == 'californication'
149
+ @file.season.should == 6
150
+ @file.episode.should == 7
151
+ @file.tags.should == ['720P', 'HDTV', 'X264']
152
+ @file.group.should == '2HD'
153
+ end
154
+
155
+ it 'should succeed given filename with showname containing multiple words' do
156
+ expect {
157
+ @file = Addic7ed::Filename.new(@filename_multiple_words)
158
+ }.to_not raise_error
159
+ @file.showname.should == 'The Walking Dead'
160
+ @file.season.should == 3
161
+ @file.episode.should == 11
162
+ @file.tags.should == ['720P', 'HDTV', 'X264']
163
+ @file.group.should == 'EVOLVE'
164
+ end
165
+
166
+ it 'should succeed given filename with showname containing multiple words with space separator' do
167
+ expect {
168
+ @file = Addic7ed::Filename.new(@filename_multiple_words_spaces)
169
+ }.to_not raise_error
170
+ @file.showname.should == 'The Walking Dead'
171
+ @file.season.should == 3
172
+ @file.episode.should == 11
173
+ @file.tags.should == ['720P', 'HDTV', 'X264']
174
+ @file.group.should == 'EVOLVE'
175
+ end
176
+
177
+ it 'should succeed given filename with showname containing only numbers' do
178
+ expect {
179
+ @file = Addic7ed::Filename.new(@filename_numbers_only)
180
+ }.to_not raise_error
181
+ @file.showname.should == '90210'
182
+ @file.season.should == 5
183
+ @file.episode.should == 12
184
+ @file.tags.should == ['720P', 'HDTV', 'X264']
185
+ @file.group.should == 'DIMENSION'
186
+ end
187
+
188
+ it 'should succeed given filename with showname containing production year' do
189
+ expect {
190
+ @file = Addic7ed::Filename.new(@filename_showname_year)
191
+ }.to_not raise_error
192
+ @file.showname.should == 'The Americans 2013'
193
+ @file.season.should == 1
194
+ @file.episode.should == 4
195
+ @file.tags.should == ['720P', 'HDTV', 'X264']
196
+ @file.group.should == 'DIMENSION'
197
+ end
198
+
199
+ it 'should succeed given filename containing full path' do
200
+ expect {
201
+ @file = Addic7ed::Filename.new(@filename_full_path)
202
+ }.to_not raise_error
203
+ @file.showname.should == 'Californication'
204
+ @file.season.should == 6
205
+ @file.episode.should == 7
206
+ @file.tags.should == ['720P', 'HDTV', 'X264']
207
+ @file.group.should == '2HD'
208
+ end
209
+
210
+ it 'should succeed given filename containing relative path' do
211
+ expect {
212
+ @file = Addic7ed::Filename.new(@filename_relative_path)
213
+ }.to_not raise_error
214
+ @file.showname.should == 'Californication'
215
+ @file.season.should == 6
216
+ @file.episode.should == 7
217
+ @file.tags.should == ['720P', 'HDTV', 'X264']
218
+ @file.group.should == '2HD'
219
+ end
220
+
221
+ it 'should succeed given filename containing no extension' do
222
+ expect {
223
+ @file = Addic7ed::Filename.new(@filename_no_extension)
224
+ }.to_not raise_error
225
+ @file.showname.should == 'Californication'
226
+ @file.season.should == 6
227
+ @file.episode.should == 7
228
+ @file.tags.should == ['720P', 'HDTV', 'X264']
229
+ @file.group.should == '2HD'
230
+ end
231
+
232
+ it 'should fail given filename with no showname' do
233
+ expect {
234
+ @file = Addic7ed::Filename.new(@filename_no_showname)
235
+ }.to raise_error
236
+ end
237
+
238
+ it 'should fail given filename with no season number' do
239
+ expect {
240
+ @file = Addic7ed::Filename.new(@filename_no_season)
241
+ }.to raise_error
242
+ end
243
+
244
+ it 'should raise InvalidFilename given filename with no episode number' do
245
+ expect {
246
+ @file = Addic7ed::Filename.new(@filename_no_episode)
247
+ }.to raise_error(Addic7ed::InvalidFilename)
248
+ end
249
+
250
+ it 'should raise InvalidFilename given filename with no tags' do
251
+ expect {
252
+ @file = Addic7ed::Filename.new(@filename_no_tags)
253
+ }.to raise_error(Addic7ed::InvalidFilename)
254
+ end
255
+
256
+ it 'should raise InvalidFilename given filename with no group' do
257
+ expect {
258
+ @file = Addic7ed::Filename.new(@filename_no_group)
259
+ }.to raise_error(Addic7ed::InvalidFilename)
260
+ end
261
+
262
+ describe '#basename' do
263
+ it 'should return only file name given a full path' do
264
+ Addic7ed::Filename.new(@filename_full_path).basename.should == 'Californication.S06E07.720p.HDTV.x264-2HD.mkv'
265
+ end
266
+ end
267
+
268
+ describe '#dirname' do
269
+ it 'should return only path given a full path' do
270
+ Addic7ed::Filename.new(@filename_full_path).dirname.should == '/data/public/Series/Californication/Saison 06'
271
+ end
272
+ end
273
+
274
+ describe '#extname' do
275
+ it 'should return only file extension given a full path' do
276
+ Addic7ed::Filename.new(@filename_full_path).extname.should == '.mkv'
277
+ end
278
+ end
279
+
280
+ describe '#to_s' do
281
+ it 'should return file name as a string' do
282
+ Addic7ed::Filename.new(@filename_full_path).to_s.should == '/data/public/Series/Californication/Saison 06/Californication.S06E07.720p.HDTV.x264-2HD.mkv'
283
+ end
284
+ end
285
+
286
+ end