Module: Subdb::ClientUtils

Defined in:
lib/subdb/client_utils.rb

Constant Summary collapse

VIDEO_EXTENSIONS =
['.avi', '.mkv', '.mp4', '.mov', '.mpg', '.wmv', '.rm', '.rmvb', '.divx']
SUB_EXTESNIONS =
['.srt', '.sub']

Class Method Summary collapse

Class Method Details

.cache_file_pathObject



129
130
131
# File 'lib/subdb/client_utils.rb', line 129

def cache_file_path
  File.join((ENV["HOME"] || ENV["USERPROFILE"]), ".subdb_cache")
end

.find_subtitle(path) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/subdb/client_utils.rb', line 117

def find_subtitle(path)
  base = File.dirname(path) + File::SEPARATOR + File.basename(path, File.extname(path))

  for subext in SUB_EXTESNIONS
    subpath = base + subext

    return subpath if File.exists?(subpath)
  end

  nil
end

.scan_paths(paths) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/subdb/client_utils.rb', line 27

def scan_paths(paths)
  video_ext = VIDEO_EXTENSIONS.join(",")

  files = []

  for path in paths
    if File.directory?(path)
      path = path.chomp(File::SEPARATOR)
      globpath = "#{path.gsub("\\", "/")}/**/*{#{video_ext}}"

      yield globpath if block_given?

      files = files.concat(Dir.glob(globpath))
    else
      files << path if VIDEO_EXTENSIONS.include?(File.extname(path))
    end
  end

  files.sort
end

.sync(paths, languages = ["en"]) {|:loading_cache| ... } ⇒ Object

Yields:

  • (:loading_cache)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/subdb/client_utils.rb', line 48

def sync(paths, languages = ["en"])
  yield :loading_cache
  cache = Subdb::UploadCache.new(cache_file_path)

  results = {:download => [], :upload => []}
  i = 0

  for path in paths
    base = File.dirname(path) + File::SEPARATOR + File.basename(path, File.extname(path))
    sub  = find_subtitle(path)

    yield :scan, [path, i]

    begin
      subdb = Video.new(path)

      yield :scanned, subdb

      if sub and !cache.uploaded?(subdb.hash, sub)
        yield :uploading, subdb

        begin
          subdb.upload(sub)
          cache.push(subdb.hash, sub)
          results[:upload].push(sub)
          yield :upload_ok, subdb
        rescue
          yield :upload_failed, [subdb, $!]
        end
      end

      if !sub
        yield :downloading, subdb

        begin
          downloaded = subdb.download(languages)

          if downloaded
            sub = base + ".srt"

            File.open(sub, "wb") do |f|
              f << downloaded
            end

            cache.push(subdb.hash, sub)
            results[:download].push(sub)
            yield :download_ok, [subdb, sub]
          else
            yield :download_not_found, subdb
          end
        rescue
          yield :download_failed, [subdb, $!]
        end
      end
    rescue
      yield :scan_failed, path, $!
    end

    i += 1

    yield :file_done, [subdb, i]
  end

  yield :storing_cache
  cache.store!

  results
end