module Iget

Constants

VERSION

Public Class Methods

get() click to toggle source
# File lib/iget.rb, line 18
def self.get
  if ARGV.count.positive?
    app = ARGV[0]
  else
    print '请输入要查找的APP: '
    app = $stdin.gets.chomp
  end

  url = URI('https://itunes.apple.com/search?')
  parmas = { term: app,
             country: 'us',
             media: 'software',
             entity: 'software',
             limit: '10' }
  response = Net::HTTP.post_form(url, parmas)
  json = JSON.parse(response.body)
  count = json['resultCount']

  if count.zero?
    puts '查无此项,请重新输入。'
    return
  end

  json['results'].each_with_index do |item, index|
    puts "#{index} : #{item['trackCensoredName']}"
  end

  print "请输入要查找的项目(0到#{count - 1}): "
  select_num = $stdin.gets.chomp
  json = json['results'][select_num.to_i]
  puts "当前选中的是 : #{json['trackCensoredName']}"
  sizes = %w[60x60 100x100 512x512]

  sizes.each_with_index do |size, index|
    puts "#{index} : #{size}"
  end

  print '请选择要下载的大小(0到2): '
  select_num = $stdin.gets
  link = json["artworkUrl#{sizes[select_num.to_i].split('x')[0]}"]

  url = URI(link)
  URI.open(url) do |image|
    image_name = json['trackCensoredName']
    image_size = sizes[select_num.to_i]
    dir_desktop = Dir.home + '/Desktop/'
    filename = "#{dir_desktop}#{image_name}-#{image_size}.jpg"
    f = File.new(filename, 'w')
    f.write(image.read)
    f.close
    puts '🍺 下载完成'
  end
end
main() click to toggle source

Your code goes here…

# File lib/iget.rb, line 10
def self.main
  begin
    self.get
  rescue Interrupt
    exit 0
  end
end