Module: XLiveServices::Media

Defined in:
lib/xlive_services/media.rb

Defined Under Namespace

Classes: DownloadError, DownloadForbidden

Class Method Summary collapse

Class Method Details

.Download(urls, target, overwrite = false, keep = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xlive_services/media.rb', line 10

def self.Download(urls, target, overwrite = false, keep = false)
    uris = []
    urls.each do |url|
        uris << [URI.parse(url), url]
    end
    uris.each do |uri|
        path = File::expand_path(File.basename(uri.first.path), target)
        request = HTTPI::Request.new(uri.last)
        response = nil
        file = nil
        begin
            if overwrite or not File.exist?(path) or File::size(path).zero?
                response = nil
                file = File.open(path, 'wb')
                request.on_body do |data|
                    file.write(data)
                end
                yield(request, path)
                response = HTTPI.get(request)
                if response.error?
                    file.truncate(0)
                    if response.code == 403
                        raise DownloadForbidden.new(response.body)
                    else
                        raise DownloadError.new(response.code)
                    end
                end
                file.close
                file = nil
            else
                yield(nil, path)
            end
            urls.delete(uri.last)
        rescue Exception => e
            file.close if file
            FileUtils.remove_file(path, true) unless keep
            raise e
        end
    end
end


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
# File 'lib/xlive_services/media.rb', line 61

def self.GetManifestLinks(manifest_cab)
    links = []
    decompressor = LibMsPack::CabDecompressor.new
    decompressor.setParam(LibMsPack::MSCABD_PARAM_FIXMSZIP, 1)
    cab = decompressor.open(manifest_cab)
    begin
        file = cab.files
        begin
            if file.getFilename.casecmp('Content\OfferManifest.xml').zero?
                xmlFile = Tempfile.new('manifest_xml')
                decompressor.extract(file, xmlFile.path)
                begin
                    xmlFile.open
                    data = MultiXml.parse(xmlFile)
                    items = data['OfferManifest']['Items']['Item']
                    items = [items] unless items.is_a?(Array)
                    items.each do |item|
                        links << item['Link']['Url']
                    end
                ensure
                    xmlFile.close
                    xmlFile.unlink
                end
                break
            end
        end until (file = file.next).nil?
    ensure
        decompressor.close(cab)
        decompressor.destroy
    end
    links
end

.IsManifest?(path) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
55
# File 'lib/xlive_services/media.rb', line 51

def self.IsManifest?(path)
    parts = path.split('.')
    return false if parts.length < 2
    parts[-2].end_with?('_manifest')
end

.IsSupportedManifest?(path) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/xlive_services/media.rb', line 57

def self.IsSupportedManifest?(path)
    path.end_with?('.cab')
end