modown 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 05885cecd3c920f2bc8a15d85c1c496396d73f30
4
+ data.tar.gz: a524581f2e8a951badb0270ef1490bd443444e48
5
+ SHA512:
6
+ metadata.gz: 2259fd12daa91ad316c1664ea2e5b395893a24ba9f3708f946dd5b3b53ca608eb12997307d335517465f52a13bf95dfdc75a2eff636a86e21385a4e0293392e7
7
+ data.tar.gz: a77e8aed0287116080316a78822e4dce3882da63141f6c134fb130d446165d0af106bd97cda2668c146753bb1176767a13497b240f064023327c4ba9f514a325
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in modown.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Pulkit Sharma
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # Modown
2
+
3
+ Modown is CLI app for downloading 3D models from archive3D.net
4
+
5
+ ## Installation
6
+
7
+ $ gem install modown
8
+
9
+ ## Usage
10
+
11
+ $ modown OBJECT
12
+
13
+ where OBJECT is the name of the thing you want to download.
14
+
15
+ $ modown maze
16
+ $ modown boy
17
+ $ modown guitar
18
+
19
+ Supports Options are
20
+ * `--count,-c` The number of different models you want, Defaults to `1`
21
+ * `--format,-f` The file format you want, supported formats are `[3ds,max,gsm,mtl]`, Defaults to `*`
22
+
23
+
24
+ ## Development
25
+
26
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
27
+
28
+ ## Contributing
29
+
30
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pulkitsharma07/modown. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
31
+
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
36
+
37
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "modown"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
4
+ require 'modown'
5
+
6
+ cli = Modown::CLI.new
7
+ cli.run
@@ -0,0 +1,147 @@
1
+ require 'modown/version'
2
+ require 'modown/options'
3
+ require 'net/http'
4
+ require 'nokogiri'
5
+ require 'zip'
6
+
7
+ # The main module.
8
+ module Modown
9
+ # The CLI is a class responsible of handling all the command line interface
10
+ # logic.
11
+ class CLI
12
+ def initialize
13
+ @options = {}
14
+ end
15
+
16
+ # This method is the entry point for the command-line app
17
+ def run(args = ARGV)
18
+ @options = Options.new.parse(args)
19
+
20
+ get_models(@options[:search_term], @options[:count], @options[:format])
21
+ end
22
+
23
+ # This method integrates the {download_model} , {search_models} and {get_model_from_zip} methods
24
+ #
25
+ # @param name [String] the name of the thing you want to download 3D models.example "cat","bottle",etc
26
+ # @param count [Integer] the number of models you want
27
+ # @param format [String] the glob pattern of the desired 3D model file format
28
+ # @return [void]
29
+ def get_models(name, count = 1, format = '*.3[Dd][Ss]')
30
+ Modown::search_models(name, count).each do |id|
31
+ Modown::download_model(id)
32
+ Modown::get_model_from_zip(id + '.zip', name + '_' + id, format)
33
+ end
34
+ end
35
+
36
+ end
37
+ # Downloads a file at a given url and writes it to disk.
38
+ # Taken from - https://gist.github.com/Burgestrand/454926
39
+ # @param url [URL] the url of the file to download
40
+ # @param save_as [string] the name/path of the file to save, along with the extension
41
+ # @return [void]
42
+ def self.download(url, save_as)
43
+ Thread.new do
44
+ thread = Thread.current
45
+ url = URI.parse(url)
46
+ Net::HTTP.new(url.host, url.port).request_get(url.path) do |response|
47
+ thread[:length] = response['Content-Length'].to_i
48
+ puts " #{(thread[:length] / 1_000_000.0)} MB "
49
+ open(save_as, 'wb') do |file|
50
+ response.read_body do |fragment|
51
+ file.write(fragment)
52
+ thread[:done] = (thread[:done] || 0) + fragment.length
53
+ thread[:progress] = thread[:done].quo(thread[:length]) * 100
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ # Downloads the model from archive3D and saves it in a zip file.
61
+ #
62
+ # @param model_id [String] The `id` of the model to download
63
+ # @param save_to [String] Where to save the file.
64
+ # @return [void]
65
+ def self.download_model(model_id,save_to="")
66
+ puts 'Please wait downloading Model'
67
+ download_url = 'http://www.archive3d.net/?a=download&do=get&id='
68
+ begin
69
+
70
+ response = Net::HTTP.get_response(URI.parse(download_url + model_id))
71
+ file_location = response['location']
72
+ thread = download(file_location, save_to + model_id + '.zip')
73
+ print "#{thread[:progress].to_i}% \r" until thread.join 1
74
+
75
+ rescue
76
+ puts "Can't download model", $!
77
+ 0
78
+ else
79
+ puts model_id + ' downloaded !'
80
+ 1
81
+ end
82
+ end
83
+
84
+ # The zip file of the downloaded model contains different 3D formats
85
+ # This method extracts a file with the given format from the zip file.
86
+ # More specifically, it gets the files which matches the format (glob pattern) and extracts them
87
+ # and saves it with the name output_file and their respective format
88
+ #
89
+ # @param input_zip [String] the name of the zip file
90
+ # @param output_file [String] the desired name of the model file.
91
+ # @param format [String] glob pattern for any of the 3D file formats ( for example <tt>".3[Dd][Ss]"</tt>)
92
+ # @return [void]
93
+ def self.get_model_from_zip(input_zip, output_file, format = '*')
94
+ Zip::File.open(input_zip) do |zip_file|
95
+ matches = zip_file.glob(format)
96
+ matches.each do |entry|
97
+ begin
98
+
99
+ file_complete_name = entry.name.split('.')
100
+ file_name = file_complete_name[0]
101
+ extension = file_complete_name[1..-1].join('.').downcase
102
+ entry.extract(output_file + "_" + file_name + '.' + extension)
103
+ rescue Zip::DestinationFileExistsError
104
+ end
105
+ end
106
+ end
107
+ 1
108
+ rescue
109
+ puts 'Error opening ZIP file'
110
+ 0
111
+ end
112
+
113
+ # This methods searches for 3D models on archive3D.net and returns a list of model_ids
114
+ #
115
+ # @param name [String] something you want to search , for example "dog", "table", etc
116
+ # @param count [Integer] the number of models you want. The number of actual models returned are always <= count
117
+ # @return [Array<String>] the list of the model_ids
118
+ def self.search_models(name, count = 1)
119
+ id_list = []
120
+ search_url = 'http://www.archive3d.net/?tag='
121
+ begin
122
+
123
+ page = Nokogiri::HTML(Net::HTTP.get_response(URI.parse(search_url + name)).body)
124
+
125
+ rescue
126
+ puts 'There was problem contacting archive3d', $!
127
+ else
128
+ count.times do |x|
129
+ x += 1
130
+ begin
131
+ element = page.css("#prevtable > div:nth-child(#{x}) > div > a")[0]
132
+ model_id = element['href'].split('id=')[1]
133
+ rescue
134
+
135
+ else
136
+ id_list << model_id
137
+ end
138
+ end
139
+ end
140
+
141
+ puts "Found #{id_list.size} models ! \n"
142
+ id_list
143
+ end
144
+
145
+
146
+
147
+ end
@@ -0,0 +1,54 @@
1
+ require 'optparse'
2
+
3
+ module Modown
4
+ # This class handles command line options
5
+ class Options
6
+
7
+ def initialize
8
+ @options = { input: nil, count: 1, format: '*' }
9
+
10
+ # Dont know how to do case-insensitive glob matching
11
+ @formats_glob = {}
12
+ @formats_glob['3ds'] = '*.3[Dd][Ss]'
13
+ @formats_glob['max'] = '*.[Mm][Aa][Xx]'
14
+ @formats_glob['gsm'] = '*.[Gg][Ss][Mm]'
15
+ @formats_glob['mtl'] = '*.[Mm][Tt][Ll]'
16
+ @formats_glob['*'] = '*'
17
+ end
18
+
19
+ # Parse the command line arguments
20
+ # @param args [command_line_arguments]
21
+ # @return [Hash]
22
+ def parse(args)
23
+ parser = OptionParser.new do |opts|
24
+ opts.banner = 'Usage: modown [options] Object'
25
+
26
+ opts.on('-c', '--count COUNT', Integer, 'Number of different models you want') do |count|
27
+ @options[:count] = count
28
+ end
29
+
30
+ opts.on('-f', '--format FORMAT',
31
+ 'The file format you want.',
32
+ "Supported formats are [#{@formats_glob.keys.join(',')}].",
33
+ 'All files are stored if no FORMAT is provided') do |format|
34
+ @options[:format] = @formats_glob[format]
35
+ end
36
+
37
+ opts.on('-h', '--help', 'Displays Help') do
38
+ puts opts
39
+ exit
40
+ end
41
+ end
42
+
43
+ parser.parse!(args)
44
+
45
+ raise '[ERROR] Please provide the object to search' if args.length == 0
46
+ raise "[ERROR] Please provide a valid format for the -f flag.
47
+ Supported formats are [#{@formats_glob.keys.join(',')}]" if @options[:format].nil?
48
+
49
+ @options[:search_term] = args[0]
50
+
51
+ @options
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module Modown
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'modown/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "modown"
8
+ spec.version = Modown::VERSION
9
+ spec.authors = ["Pulkit Sharma"]
10
+ spec.email = ["avastpulkit@gmail.com"]
11
+
12
+ spec.summary = %q{Download 3D models.}
13
+ spec.homepage = "https://github.com/pulkitsharma07/modown"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency('nokogiri')
22
+ spec.add_runtime_dependency('rubyzip')
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: modown
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pulkit Sharma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubyzip
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - avastpulkit@gmail.com
86
+ executables:
87
+ - modown
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - exe/modown
102
+ - lib/modown.rb
103
+ - lib/modown/options.rb
104
+ - lib/modown/version.rb
105
+ - modown.gemspec
106
+ homepage: https://github.com/pulkitsharma07/modown
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.2.3
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Download 3D models.
130
+ test_files: []