suby 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/suby +35 -14
  2. metadata +2 -2
data/bin/suby CHANGED
@@ -1,8 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- if ARGV.include? '-h'
4
- puts "#{File.basename $0} video"
5
- puts " Find and download subtitles for the given video file"
3
+ require 'optparse'
4
+
5
+ options = {
6
+ lang: 'en'
7
+ }
8
+
9
+ option_parser = OptionParser.new do |opts|
10
+ opts.banner = "#{File.basename $0} [options] video"
11
+ opts.separator ' Find and download subtitles for the given video file'
12
+ opts.separator "\nOptions:"
13
+
14
+ opts.on '-l', '--lang LANG = en', 'Lang for subtitles' do |lang|
15
+ options[:lang] = lang
16
+ end
17
+
18
+ opts.on '-h', '--help', 'Show usage' do
19
+ puts opts
20
+ exit
21
+ end
22
+ end
23
+
24
+ option_parser.parse!
25
+
26
+ if ARGV.empty?
27
+ puts option_parser
6
28
  exit
7
29
  end
8
30
 
@@ -13,7 +35,6 @@ SUB_EXTENSIONS = %w[srt sub]
13
35
  archive = '__archive__'
14
36
  site = 'www.tvsubtitles.net'
15
37
  search_url = '/search.php'
16
- lang = 'en'
17
38
 
18
39
  def extract_subs_from_archive(archive)
19
40
  case `file #{archive}`
@@ -41,29 +62,29 @@ ARGV.each { |file|
41
62
  next(puts "Skipping: #{file}") if SUB_EXTENSIONS.any? { |ext|
42
63
  File.exist? File.basename(file, File.extname(file)) + ".#{ext}" }
43
64
 
44
- unless /^(?<tvshow>.+) (?<season>\d{1,2})x(?<episode>\d{1,2}) - .+\.[a-z]+?$/ =~ file
45
- next puts "wrong file format (#{file}). Must be:\n<tvshow> <season>x<episode> - <title>.<ext>"
65
+ unless /^(?<show>.+) (?<season>\d{1,2})x(?<episode>\d{1,2}) - .+\.[a-z]+?$/ =~ file
66
+ next puts "wrong file format (#{file}). Must be:\n<show> <season>x<episode> - <title>.<ext>"
46
67
  end
47
68
  season, episode = [season, episode].map(&:to_i)
48
69
  puts "Searching subtitles for #{file}:"
49
- puts "Show: #{tvshow}, Season: #{season}, Episode: #{episode}"
70
+ puts "Show: #{show}, Season: #{season}, Episode: #{episode}"
50
71
 
51
72
  Net::HTTP.start(site) { |tvsubtitles|
52
- # search tvshow
53
- tvshow_url = (show_urls[tvshow] ||= begin
73
+ # search show
74
+ show_url = (show_urls[show] ||= begin
54
75
  post = Net::HTTP::Post.new(search_url)
55
- post.form_data = { 'q' => tvshow }
76
+ post.form_data = { 'q' => show }
56
77
  results = Nokogiri tvsubtitles.request(post).body
57
78
  url = results.css('ul li div a').first[:href]
58
79
 
59
80
  raise 'could not find the show' unless /^\/tvshow-(\d+)\.html$/ =~ url
60
81
  "/tvshow-#{$1}-#{season}.html"
61
82
  end)
62
- puts "tvshow url: #{tvshow_url}"
83
+ puts "show url: #{show_url}"
63
84
 
64
85
  # search episode
65
- tvshow = (show_pages[tvshow] ||= Nokogiri tvsubtitles.get(tvshow_url).body)
66
- tr = tvshow.css('div.left_articles table tr').find { |tr|
86
+ show = (show_pages[show] ||= Nokogiri tvsubtitles.get(show_url).body)
87
+ tr = show.css('div.left_articles table tr').find { |tr|
67
88
  tr.children.find { |td| td.name == 'td' && td.text =~ /\A#{season}x0?#{episode}\z/ }
68
89
  }
69
90
  episode_url = nil
@@ -74,7 +95,7 @@ ARGV.each { |file|
74
95
  }
75
96
 
76
97
  raise "invalid episode url: #{episode_url}" unless episode_url =~ /^episode-(\d+)\.html$/
77
- episode_url = "/episode-#{$1}-#{lang}.html"
98
+ episode_url = "/episode-#{$1}-#{options[:lang]}.html"
78
99
  puts "episode url: #{episode_url}"
79
100
 
80
101
  # subtitles
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: suby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-15 00:00:00.000000000 Z
12
+ date: 2011-06-19 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Find and download subtitles
15
15
  email: