Class | Yapra::Plugin::Publish::FileDownload |
In: |
lib-plugins/yapra/plugin/publish/file_download.rb
|
Parent: | Yapra::Plugin::Publish::OnMemoryDownload |
download file with WWW::Mechanize.
example:
- module: Publish::FileDownload config: regexp: http://www\.yahoo\.co\.jp/* dir: '/Users/yuanying/Downloads/' auto_suffix: true before_hook: "agent.get('http://www.yahoo.co.jp/'); sleep 1" url: attribute: link replace: index(\d*?)\.html to: file\1.zip filename: attribute: title replace: 'Yahoo' to: '' referrer: attribute: link replace: 'zip' to: 'html'
# File lib-plugins/yapra/plugin/publish/file_download.rb, line 32 32: def discover_extensions page 33: require 'mime/types' 34: types = MIME::Types[page.content_type] 35: if types.size > 0 && types[0].extensions.size > 0 36: return type[0].extensions[0] 37: else 38: logger.info 'suitable extention is not found.' 39: return nil 40: end 41: rescue LoadError => ex 42: logger.warn 'require mime-types is failed.' 43: end
# File lib-plugins/yapra/plugin/publish/file_download.rb, line 45 45: def save config, item, page 46: filename = construct_data(config['filename'], item) 47: filename = page.filename unless filename 48: 49: if config['auto_suffix'] 50: ext = discover_extensions(page) 51: filename = "#{filename}.#{ext}" if ext 52: end 53: 54: path = File.join(dir, filename) 55: open(path, 'w') do |io| 56: io.write page.body 57: end 58: end