Module: Modown

Defined in:
lib/modown.rb,
lib/modown/options.rb,
lib/modown/version.rb

Overview

The main module.

Defined Under Namespace

Classes: CLI, Options

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Class Method Summary collapse

Class Method Details

.download(url, save_as) ⇒ void

This method returns an undefined value.

Downloads a file at a given url and writes it to disk. Taken from - gist.github.com/Burgestrand/454926

Parameters:

  • url (URL)

    the url of the file to download

  • save_as (string)

    the name/path of the file to save, along with the extension



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/modown.rb', line 42

def self.download(url, save_as)
  Thread.new do
    thread = Thread.current
    url = URI.parse(url)
    Net::HTTP.new(url.host, url.port).request_get(url.path) do |response|
      thread[:length] = response['Content-Length'].to_i
      puts " #{(thread[:length] / 1_000_000.0)} MB "
      open(save_as, 'wb') do |file|
        response.read_body do |fragment|
          file.write(fragment)
          thread[:done] = (thread[:done] || 0) + fragment.length
          thread[:progress] = thread[:done].quo(thread[:length]) * 100
        end
      end
    end
  end
end

.download_model(model_id, save_to = "") ⇒ void

This method returns an undefined value.

Downloads the model from archive3D and saves it in a zip file.

Parameters:

  • model_id (String)

    The ‘id` of the model to download

  • save_to (String) (defaults to: "")

    Where to save the file.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/modown.rb', line 65

def self.download_model(model_id,save_to="")
  puts 'Please wait downloading Model'
  download_url = 'http://www.archive3d.net/?a=download&do=get&id='
  begin

    response = Net::HTTP.get_response(URI.parse(download_url + model_id))
    file_location = response['location']
    thread = download(file_location, save_to + model_id + '.zip')
    print "#{thread[:progress].to_i}% \r" until thread.join 1

  rescue
    puts "Can't download model", $!
    0
  else
    puts model_id + ' downloaded !'
    1
  end
end

.get_model_from_zip(input_zip, output_file, format = '*') ⇒ void

This method returns an undefined value.

The zip file of the downloaded model contains different 3D formats This method extracts a file with the given format from the zip file. More specifically, it gets the files which matches the format (glob pattern) and extracts them and saves it with the name output_file and their respective format

Parameters:

  • input_zip (String)

    the name of the zip file

  • output_file (String)

    the desired name of the model file.

  • format (String) (defaults to: '*')

    glob pattern for any of the 3D file formats ( for example ".3[Dd][Ss]")



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/modown.rb', line 93

def self.get_model_from_zip(input_zip, output_file, format = '*')
  Zip::File.open(input_zip) do |zip_file|
    matches = zip_file.glob(format)
    matches.each do |entry|
      begin

        file_complete_name = entry.name.split('.')
        file_name = file_complete_name[0]
        extension = file_complete_name[1..-1].join('.').downcase
        entry.extract(output_file + "_" + file_name + '.' + extension)
      rescue Zip::DestinationFileExistsError
      end
    end
  end
  1
rescue
  puts 'Error opening ZIP file'
  0
end

.search_models(name, count = 1) ⇒ Array<String>

This methods searches for 3D models on archive3D.net and returns a list of model_ids

Parameters:

  • name (String)

    something you want to search , for example “dog”, “table”, etc

  • count (Integer) (defaults to: 1)

    the number of models you want. The number of actual models returned are always <= count

Returns:

  • (Array<String>)

    the list of the model_ids



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/modown.rb', line 118

def self.search_models(name, count = 1)
  id_list = []
  search_url = 'http://www.archive3d.net/?tag='
  begin

    page = Nokogiri::HTML(Net::HTTP.get_response(URI.parse(search_url + name)).body)

  rescue
    puts 'There was problem contacting archive3d', $!
  else
    count.times do |x|
      x += 1
      begin
        element = page.css("#prevtable > div:nth-child(#{x}) > div > a")[0]
        model_id = element['href'].split('id=')[1]
      rescue

      else
        id_list << model_id
      end
    end
  end

  puts "Found #{id_list.size} models ! \n"
  id_list
end