Class: Lady::Download

Inherits:
Command
  • Object
show all
Defined in:
lib/Lady/command/download.rb

Instance Method Summary collapse

Methods inherited from Command

options

Constructor Details

#initialize(argv) ⇒ Download

Returns a new instance of Download.



18
19
20
21
22
# File 'lib/Lady/command/download.rb', line 18

def initialize(argv)
  @link = argv.shift_argument
  # @save_path = argv.flag?('save-path')
  super
end

Instance Method Details

#match_url(string) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/Lady/command/download.rb', line 48

def match_url(string)
  urls = []
  regular = Regexp.new('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
  string.scan(regular) do |url|
    urls << url
  end
  urls
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/Lady/command/download.rb', line 24

def run
  # 1.匹配下载链接
  urls = match_url(@link)
  unless urls.empty?
    # 2.解析下载链接
    response = RestClient.get(ANALYSE_PREFIX + urls[0] , HEADERS)
    json_obj =  JSON.parse(response)
    nickname = json_obj['nickname'] ||= 'temp'
    #puts json_obj
    video_url =  json_obj['video']
    if video_url.length > 0
      # 3.下载
      video_data = RestClient.get(video_url)
      mkdir = FileUtils.mkdir_p('~/Desktop/video')[0]
      # 4.写入本地
      file = File.open("#{mkdir}/#{nickname}.mp4", 'a+')
      if file
        puts "保存成功!".green
        file.syswrite(video_data)
      end
    end
  end
end

#validate!Object



57
58
59
60
61
62
# File 'lib/Lady/command/download.rb', line 57

def validate!
  super
  unless @link
    help! "link is necessary!"
  end
end