magento_remote 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjYyOTdkMDFhMjU5ZGRjMDQzMzA1NTIxZDJiNjI1MmIyM2RlOTA4NA==
4
+ MTY4MzBjMGViZWVjZTQwMGVhNjBkZDViNjRhYzNlNzVmYzUwZjc3ZQ==
5
5
  data.tar.gz: !binary |-
6
- YWZhYjkyZmZmM2QzZGVmNjhiMWViYTA5YjYzZDc1N2QxZDVlZDQ2YQ==
6
+ ZTUwYzU3MzllMmE0MjQ0ZjFlYjhiN2VlYzUwMWNhOGQ2OTg2Y2E4YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjFlMWZkYWMyZTIwMzViZTE4MTY1OGViNzZjMTc1NDk4MWQ4ZmFkN2I4NjBh
10
- ZjgxNzg5YTA0M2E4NmFmZWNlYzQ4ZTU5YjE1ZWNiNjFkMTNmMzQ1NDEyYzA2
11
- MWJhZTE1MzllMWE4NzNlMGM1ZDViY2U5ZTM4YTQxMzIyOGNiYzE=
9
+ YWRkOTg3MTY5YjVmZGFiNDdjNjA1NjA0ZTcwMmRkNzQxODg3NzU3ZmY0ZDI5
10
+ YmE4NzM5YmI0MTM3YmNkNTRjMjgxZmIxMjVhNDhiYTY1NjEwYzJhZTg3MDcz
11
+ MmVkYmI1YmQ0YmM1ZmVkMzM5NGIwNGU1NzFkNWY2ZjUxNzU2MzU=
12
12
  data.tar.gz: !binary |-
13
- MzVmMzQzMDI3MTJlNDZhOTI4YTM5ZjUxNTE5ZDg3ZmNjNWM1ZmM3YTJjZGM0
14
- ZTg4ZjVmNmQyYTNiYzE3MDI0NmU0M2M3NzI3MzEwOGUyM2VmZDdjYzcxZjBi
15
- Yzk0Yjc2MDQ0MTQ0NzZjMDA4MWRmZTAyMzRkYzhhMTYwYWI0MDk=
13
+ MTQ1NGUxMGQ0NDBmNTc5OGVlZWM0ZGQ4ZWNlMTMyODY0OWRhMjg2ZDMxMDhh
14
+ MWI5YjVmNDExMjMzMTE5OTU4Y2JhNGM3OTRlYTU0ZWVkNDNhN2JmN2RhNDMy
15
+ MjJjYzEyNmE5YTgyZjgxMDM1M2NhMTAwZTg1OTM2MWY5MzhjYjU=
data/README.md CHANGED
@@ -28,7 +28,11 @@ to put *quantity* of the product with *productid* in your shopping cart.
28
28
 
29
29
  to display a table of matching products.
30
30
 
31
- Note that both scripts show you information about possible parameters when invoked with `--help`.
31
+ magento_scrape -b https://theshop -l limit -s startpid
32
+
33
+ to display *limit* number of products, starting with product id of *startpid*.
34
+
35
+ Note that all scripts show you information about possible parameters when invoked with `--help`.
32
36
 
33
37
  Note that you should work with
34
38
 
@@ -81,6 +81,7 @@ if options[:quantity].to_i < 1
81
81
  STDERR.puts "Error: Quantity to put in cart has to be > 0."
82
82
  exit 1
83
83
  end
84
+ options[:quantity] = options[:quantity].to_i
84
85
 
85
86
  mech = MagentoMech.from_config options
86
87
  if options[:debug] == true
@@ -95,7 +96,7 @@ mech.login
95
96
 
96
97
  if options[:amap]
97
98
  qty = mech.add_to_cart! options[:product_id], options[:quantity]
98
- if qty == options[:quantity]
99
+ if qty == options[:quantity].to_i
99
100
  puts "INFO: Succeeded in adding to cart"
100
101
  elsif qty > 0
101
102
  puts "INFO: #{options[:quantity]} items not available, added #{qty}."
@@ -0,0 +1,80 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'terminal-table'
5
+ require 'magento_remote'
6
+
7
+ # Sweet, sweet options.
8
+ options = {}
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = "Usage: magento_scrape [OPTIONS]\n Find product(s) in shop."
12
+
13
+ opts.separator ""
14
+ opts.separator "Magento shop options"
15
+
16
+ opts.on('-b', '--base-uri URI', 'base URI of shop.') do |b|
17
+ options[:base_uri] = b
18
+ end
19
+
20
+ opts.separator ""
21
+ opts.separator "Product options"
22
+
23
+ opts.on('-s', '--start PRODUCT_ID', 'Product ID from which to start scraping') do |s|
24
+ options[:start_pid] = s
25
+ end
26
+
27
+ opts.on('-l', '--limit LIMIT', 'Maximum number of product_ids to scrape') do |l|
28
+ options[:limit] = l
29
+ end
30
+
31
+ opts.separator ""
32
+ opts.separator "Output options"
33
+
34
+ opts.on('-d', '--debug FILE', 'enable debugging output, STDOUT, or FILE if given') do |d|
35
+ if d
36
+ options[:debug] = d
37
+ else
38
+ options[:debug] = true
39
+ end
40
+ end
41
+
42
+ opts.separator ""
43
+ opts.separator "General options"
44
+
45
+ opts.on_tail('--version', 'Show version.') do
46
+ puts "magento_scrape #{MagentoRemote::VERSION}"
47
+ exit 0
48
+ end
49
+ opts.on('-h', '--help', 'Show help.') do
50
+ puts opts
51
+ exit 0
52
+ end
53
+ end
54
+
55
+ optparse.parse!
56
+
57
+ if !options[:base_uri]
58
+ STDERR.puts "Error: You have to define base_uri"
59
+ exit 1
60
+ end
61
+
62
+ mech = MagentoMech.from_config options
63
+ if options[:debug] == true
64
+ mech.log_to! STDOUT
65
+ elsif options[:debug]
66
+ mech.log_to! options[:debug]
67
+ end
68
+
69
+ return_code = 0
70
+
71
+ options[:start_pid] ||= 0
72
+
73
+ matches = mech.scrape_products options[:start_pid].to_i, options[:limit].to_i
74
+ if matches.nil?
75
+ puts "Nothing found"
76
+ else
77
+ puts Terminal::Table.new :headings => ['Name', 'Product ID', 'In stock?'], :rows => matches
78
+ end
79
+
80
+ exit return_code
@@ -67,7 +67,7 @@ class MagentoMech
67
67
  while qty.to_i > 0 && !add_to_cart(product_id, qty)
68
68
  qty = qty.to_i - 1
69
69
  end
70
- qty
70
+ qty.to_i
71
71
  end
72
72
 
73
73
  # Get the current carts contents
@@ -121,6 +121,35 @@ class MagentoMech
121
121
  return products
122
122
  end
123
123
 
124
+ def find_product_id_from url
125
+ page = @mech.get url
126
+ r_pid = page.search(".//input[@name='product']")[0][:value]
127
+ r_name = page.search(".product-name .roundall")
128
+ [r_pid, r_name.text]
129
+ end
130
+
131
+ # Search/scrape products.
132
+ # Arguments
133
+ # limit: Maximum number of product_ids to check
134
+ # start_pid: With which product id to start scraping
135
+ # returns [[name1, product_id1, instock?1],[name2, p_id2...]...]
136
+ # or nil if not found.
137
+ def scrape_products start_pid, limit
138
+ products = []
139
+ limit.times do |idx|
140
+ url = relative_url("/catalog/product/view/id/#{start_pid + idx + 1}")
141
+ @mech.get url rescue next
142
+ #if @mech.response_code
143
+ product_name = @mech.page.search('.product-name .roundall')[0].text
144
+ wishlist_link = @mech.page.search(".link-wishlist")[0]
145
+ wishlist_link.attributes['href'].value[/product\/(\d+)/]
146
+ pid = $1
147
+ products << [product_name, pid]
148
+ end
149
+
150
+ return products
151
+ end
152
+
124
153
  private
125
154
 
126
155
  # Construct path relative to base uri.
@@ -1,3 +1,3 @@
1
1
  module MagentoRemote
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.1.1".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magento_remote
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felix Wolfsteller
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-10 00:00:00.000000000 Z
11
+ date: 2014-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mechanize
@@ -86,6 +86,7 @@ email:
86
86
  executables:
87
87
  - magento_add_to_cart
88
88
  - magento_find_product
89
+ - magento_scrape
89
90
  extensions: []
90
91
  extra_rdoc_files: []
91
92
  files:
@@ -99,6 +100,7 @@ files:
99
100
  - Rakefile
100
101
  - bin/magento_add_to_cart
101
102
  - bin/magento_find_product
103
+ - bin/magento_scrape
102
104
  - lib/magento_remote.rb
103
105
  - lib/magento_remote/magento_mech.rb
104
106
  - lib/magento_remote/version.rb