subdl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/subdl +11 -0
  2. data/lib/crawler.rb +115 -0
  3. metadata +47 -0
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/crawler'
4
+
5
+ itasa = Itasa.new
6
+ Credentials.new.read_to itasa
7
+ crawler = Crawler.new itasa
8
+
9
+ until ARGV.empty? do
10
+ crawler.download_sub_for ARGV.shift
11
+ end
@@ -0,0 +1,115 @@
1
+ require 'mechanize'
2
+ require 'cgi'
3
+ require 'json'
4
+ require 'zipruby'
5
+
6
+ class Crawler
7
+ def initialize itasa
8
+ @itasa = itasa
9
+ end
10
+
11
+ def download_sub_for path
12
+ movie_file = MovieFile.new(path)
13
+ @itasa.each_id movie_file.search_term do |id, showname|
14
+ @itasa.unpack_subtitle_to id, movie_file.srt_file
15
+ end
16
+ end
17
+ end
18
+
19
+ class MovieFile
20
+ attr_reader :episode, :season, :show
21
+ def initialize filename
22
+ @filename = filename
23
+ text = File.basename filename
24
+ remove_year_from text
25
+
26
+ if m = /^(.*)\.S(\d\d)E(\d\d)/.match(text)
27
+ @show = m[1].gsub '.', ' '
28
+ @season = remove_leading_zeros m[2]
29
+ @episode = remove_leading_zeros m[3]
30
+ end
31
+ end
32
+
33
+ def remove_year_from text
34
+ text.gsub! /\.20\d\d/, ''
35
+ end
36
+
37
+ def remove_leading_zeros text
38
+ text.gsub /^0*/, ''
39
+ end
40
+ def search_term
41
+ "%s %dx%02d" % [show, season, episode]
42
+ end
43
+ def srt_file
44
+ WritableFile.new "#{@filename}.itasa.srt"
45
+ end
46
+ end
47
+
48
+ class WritableFile
49
+ def initialize path
50
+ @path = path
51
+ end
52
+ def save contents
53
+ File.open @path, 'w' do |f|
54
+ f.write contents
55
+ end
56
+ end
57
+ end
58
+
59
+ class Itasa
60
+ def initialize
61
+ @agent = Mechanize.new do |a|
62
+ a.user_agent_alias = 'Mac FireFox'
63
+ end
64
+ @page = @agent.get "http://#{host}"
65
+ end
66
+
67
+ def login username, password
68
+ login_form = @page.form 'login'
69
+ login_form.username = username
70
+ login_form.passwd = password
71
+ @page = @agent.submit(login_form)
72
+ end
73
+
74
+ def logged_in?
75
+ link_that_exists_only_once_logged = @page.search(
76
+ "//a[@href='forum/index.php?action=unreadreplies']")
77
+ link_that_exists_only_once_logged.first
78
+ end
79
+
80
+ def each_id text
81
+ url = URI.parse "http://#{host}/modules/mod_itasalivesearch/search.php"
82
+ url.query = "term=#{CGI.escape text}"
83
+ response = @agent.get url
84
+ JSON.parse(response.body).each do |episode|
85
+ yield episode['id'], episode['value']
86
+ end
87
+ nil
88
+ end
89
+
90
+ def unpack_subtitle_to id, dest
91
+ url = "http://#{host}/index.php?option=com_remository&Itemid=6&func=fileinfo&id=#{id}"
92
+ page = @agent.get url
93
+ download_link = page.search("//a[img[contains(@src,'download2.gif')]]").first
94
+ zipped_subtitle = @agent.get download_link[:href]
95
+ zip_contents = zipped_subtitle.body
96
+ Zip::Archive.open_buffer(zip_contents) do |archive|
97
+ archive.each do |entry|
98
+ dest.save entry.read
99
+ end
100
+ end
101
+ end
102
+
103
+ def host
104
+ 'www.italiansubs.net'
105
+ end
106
+ end
107
+
108
+ class Credentials
109
+ def read_to itasa
110
+ contents = File.read(File.expand_path('~/.itasa-credentials')).lines
111
+ username = contents.next.chomp
112
+ password = contents.next.chomp
113
+ itasa.login username, password
114
+ end
115
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: subdl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrea Francia
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple hello world gem
15
+ email: andrea@andreafrancia.it
16
+ executables:
17
+ - subdl
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/crawler.rb
22
+ - bin/subdl
23
+ homepage: https://github.com/andreafrancia/subdl
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Download subtitles for your favorite show.
47
+ test_files: []