lbc_api 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d9ddda9d8eb82d5771421aea92681be98e62164
4
+ data.tar.gz: 5358c4fa83fd8309bc0a05d8f357668eee225b6f
5
+ SHA512:
6
+ metadata.gz: 01759075ef28e14dc9dc600d1cc5da2e0df65f182b42b4d4b4b981b35704a0b01fe2a8ac141d7852fe5ce684ae21bd6ffb332b78ee6b2802c6452e37d2f4c07f
7
+ data.tar.gz: 2067c62b95cfe0a2a7df116205a5329fea02511005afdb88c1808b3b0e7b84a88433836d51b091c4f00093a70c4c0b0abd1ae19dbba96b0a09d38dc47a88ca5e
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ /.idea
19
+ main.rb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lbc_api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Thibault Couraud
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # lbc_api - Leboncoin.fr Ruby API wrapper.
2
+
3
+ Manage & search classified ads, create users accounts.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'lbc_api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install lbc_api
18
+
19
+ ## Informations
20
+
21
+ This is a beta api wrapper. No errors will be catch neither exceptions thrown.
22
+ Please consider to take a look to this example of real usage if you want to use this script in production:
23
+
24
+ [https://github.com/sweetdub/lbc_website](https://github.com/sweetdub/lbc_website)
25
+
26
+ ## Usage
27
+
28
+ ### Classified
29
+
30
+ #### Create
31
+
32
+ params = {
33
+ 'region' => 14,
34
+ 'dpt_code' => 75,
35
+ 'zipcode' => 75015,
36
+ 'category' => 2,
37
+ 'name' => 'Lbc User',
38
+ 'phone' => '0641112554',
39
+ 'phone_hidden' => false,
40
+ 'subject' => 'Brand new Leboncoin.fr api',
41
+ 'body' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
42
+
43
+ Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
44
+
45
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
46
+ 'price' => 150
47
+ }
48
+
49
+ files = [
50
+ '/mnt/media/LBC/classified/1.jpg'
51
+ ]
52
+
53
+ LbcApi::Classified.create('lolypops@lover.com', 'password', params, files)
54
+
55
+ #### Delete
56
+
57
+ Delete a classified ad using the id and the password
58
+
59
+ LbcApi::Classified.delete('755158944', 'password')
60
+
61
+ #### Search
62
+
63
+ Search for a classified using the title only:
64
+
65
+ LbcApi::Classified.exists?('Lorem ipsum') # => true
66
+
67
+ #### Activate
68
+
69
+ Activate a classified ad or an account using the activation mail.
70
+
71
+ Example using the mailgun routes feature:
72
+
73
+ if params['subject'].include?('Activez votre annonce') or params['subject'].include?('Confirmez la demande de création de votre Compte')
74
+ LbcApi::Classified.activate(params['body-plain'])
75
+ end
76
+
77
+
78
+ #### Get the id
79
+
80
+ Get the id of a classified ad using the activation's mail.
81
+
82
+ Example using mailgun routes feature:
83
+
84
+ if params['subject'].include?('est en ligne')
85
+ LbcApi::Classified.get_id(params['body-plain']) # => "717792666"
86
+ end
87
+
88
+ ### User
89
+
90
+ #### Create
91
+
92
+ params = {
93
+ 'gender' => 1,
94
+ 'lastname' => 'LBC',
95
+ 'firstname' => 'User',
96
+ 'pseudo' => 'lbc_user',
97
+ 'region' => 1,
98
+ 'dpt_code' => 75,
99
+ 'billing_address' => '1 rue de Paris',
100
+ 'billing_zipcode' => 75001,
101
+ 'billing_postal_city' => 'Paris',
102
+ 'phone' => '0616223802'
103
+ }
104
+
105
+ # 8 characters with at least one digit and one letter.
106
+ # Accepted special characters are: . - = _ + !
107
+ password = '01234567a'
108
+
109
+ LbcApi::User.create('love@ads.fr', password, params)
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/lbc_api.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'lbc_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'lbc_api'
8
+ spec.version = LbcApi::VERSION
9
+ spec.authors = ['Thibault Couraud']
10
+ spec.email = ['contact@sweetdub.com']
11
+ spec.summary = %q{Leboncoin.fr Ruby API wrapper.}
12
+ spec.description = %q{Manage & search classified ads, create users accounts.}
13
+ spec.homepage = 'https://github.com/sweetdub/lbc_api'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'rake', '~> 10.2'
23
+ spec.add_dependency 'mechanize', '~> 2.7'
24
+ end
@@ -0,0 +1,10 @@
1
+ module LbcApi
2
+ class Agent
3
+ def self.mecha
4
+ Mechanize.new do |agent|
5
+ agent.user_agent_alias = 'Mac Safari'
6
+ agent.follow_meta_refresh = true
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,50 @@
1
+ module LbcApi
2
+ class Classified
3
+ class << self
4
+ def create(email, password, data, files = nil)
5
+ page = Agent.mecha.get('http://www2.leboncoin.fr/ai')
6
+ data = Sanitize.params(data).merge({'email' => email})
7
+ if files && files.count
8
+ files.each_with_index do |img, i|
9
+ form = page.form_with(name: 'formular') do |f|
10
+ f.file_uploads[i].file_name = img
11
+ end
12
+ if defined?(files[i + 1])
13
+ page = form.submit
14
+ end
15
+ end
16
+ end
17
+ page = page.form_with(name: 'formular') do |f|
18
+ data.each do |k, v|
19
+ f[k] = v
20
+ end
21
+ end.submit
22
+ page.form_with(name: 'formular') do |f|
23
+ f['passwd'] = password
24
+ f['passwd_ver'] = password
25
+ end.submit
26
+ end
27
+
28
+ def delete(id, password)
29
+ page = Agent.mecha.get("http://www2.leboncoin.fr/ai?id=#{id}&cmd=delete")
30
+ page = page.forms[current_page.forms.count - 2].submit
31
+ page.forms.last do |f|
32
+ f['password'] = password
33
+ end.submit
34
+ end
35
+
36
+ def exists?(title)
37
+ page = Agent.mecha.get("http://www.leboncoin.fr/annonces/?f=a&th=1&q=#{CGI::escape(title)}&it=1")
38
+ !page.search('.content-border').at("h2:contains('Aucune annonce trouvée!')")
39
+ end
40
+
41
+ def activate(body)
42
+ Agent.mecha.get(URI.extract(body, 'http').first)
43
+ end
44
+
45
+ def get_id(body)
46
+ URI.extract(body, 'http').first.scan(/\d/).join('')
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,16 @@
1
+ module LbcApi
2
+ class Sanitize
3
+ def self.params(params)
4
+ params.each do |k, v|
5
+ if v.class == String
6
+ params[k] = v.encode('iso-8859-15')
7
+ elsif v.class == TrueClass
8
+ params[k] = 1
9
+ elsif v.class == FalseClass
10
+ params[k] = 0
11
+ end
12
+ end
13
+ params
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module LbcApi
2
+ class User
3
+ def self.create(email, password, data)
4
+ page = Agent.mecha.get('https://compteperso.leboncoin.fr/account/index.html')
5
+ page = page.form_with(id: 'createform') do |f|
6
+ f['username_create'] = email
7
+ end.submit
8
+ params = Sanitize.params(data)
9
+ page.form_with(id: 'account_create') do |f|
10
+ params.each do |k, v|
11
+ f[k] = v
12
+ end
13
+ f['st_passwd'] = password
14
+ f['verify_passwd'] = password
15
+ f.checkbox_with('cgv').check
16
+ end.submit
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module LbcApi
2
+ VERSION = '0.0.1'
3
+ end
data/lib/lbc_api.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'mechanize'
2
+ require 'open-uri'
3
+ require 'uri'
4
+
5
+ Dir[File.dirname(__FILE__) + '/lbc_api/*.rb'].each do |file|
6
+ require file
7
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lbc_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Thibault Couraud
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mechanize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.7'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.7'
55
+ description: Manage & search classified ads, create users accounts.
56
+ email:
57
+ - contact@sweetdub.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lbc_api.gemspec
68
+ - lib/lbc_api.rb
69
+ - lib/lbc_api/agent.rb
70
+ - lib/lbc_api/classified.rb
71
+ - lib/lbc_api/sanitize.rb
72
+ - lib/lbc_api/user.rb
73
+ - lib/lbc_api/version.rb
74
+ homepage: https://github.com/sweetdub/lbc_api
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.2
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Leboncoin.fr Ruby API wrapper.
98
+ test_files: []