yandex_cleanweb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/lib/yandex_cleanweb.rb +113 -0
- data/lib/yandex_cleanweb/version.rb +3 -0
- data/yandex_cleanweb.gemspec +21 -0
- metadata +69 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kir Shatrov
|
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,43 @@
|
|
1
|
+
# Yandex Cleanweb
|
2
|
+
|
3
|
+
Ruby wrapper for [Yandex Cleanweb](http://api.yandex.ru/cleanweb/) spam detector.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'yandex_cleanweb'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install yandex_cleanweb
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Get the api key: [http://api.yandex.ru/cleanweb/getkey.xml](http://api.yandex.ru/cleanweb/getkey.xml)
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
YandexCleanweb.api_key = "your_key"
|
25
|
+
YandexCleanweb.spam?("just phrase")
|
26
|
+
=> false
|
27
|
+
|
28
|
+
YandexCleanweb.spam?(body_plain: "my text", ip: "80.80.40.3")
|
29
|
+
=> false
|
30
|
+
|
31
|
+
YandexCleanweb.spam?(body_html: "some spam <a href='http://spam.com'>spam link</a>")
|
32
|
+
=> { id: "request id", links: [ ['http://spam.com', true] ] }
|
33
|
+
```
|
34
|
+
|
35
|
+
If you use Yandex Cleanweb in Rails app, we recommend to set up the api key in `config/initializers/yandex_cleanweb.rb`
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require "yandex_cleanweb/version"
|
2
|
+
|
3
|
+
require "uri"
|
4
|
+
require "nokogiri"
|
5
|
+
require "net/http"
|
6
|
+
|
7
|
+
module YandexCleanweb
|
8
|
+
API_URL = 'http://cleanweb-api.yandex.ru/1.0/'
|
9
|
+
mattr_accessor :api_key
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def spam?(*options)
|
13
|
+
response = api_check_spam(options)
|
14
|
+
puts response
|
15
|
+
doc = Nokogiri::XML(response)
|
16
|
+
request_id_tag = doc.xpath('//check-spam-result/id')
|
17
|
+
spam_flag_tag = doc.xpath('//check-spam-result/text')
|
18
|
+
puts spam_flag_tag.inspect
|
19
|
+
|
20
|
+
request_id = request_id_tag[0]
|
21
|
+
spam_flag = spam_flag_tag[0].attributes["spam-flag"].content
|
22
|
+
|
23
|
+
if spam_flag == 'yes'
|
24
|
+
links = doc.xpath('//check-spam-result/links').map { |el|
|
25
|
+
[attributes["url"], attributes["spam_flag"] == 'yes']
|
26
|
+
}
|
27
|
+
|
28
|
+
{ id: request_id, links: links }
|
29
|
+
else
|
30
|
+
false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_captcha(request_id)
|
35
|
+
response = api_get_captcha(request_id)
|
36
|
+
doc = Nokogiri::XML(response)
|
37
|
+
|
38
|
+
url = doc.xpath('//get-captcha-result/url').text
|
39
|
+
captcha_id = doc.xpath('//get-captcha-result/captcha').text
|
40
|
+
|
41
|
+
{
|
42
|
+
url: url,
|
43
|
+
captcha: captcha_id
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
def valid_captcha?(request_id, captcha_id, value)
|
48
|
+
response = api_check_captcha(request_id, captcha_id, value)
|
49
|
+
doc = Nokogiri::XML(response)
|
50
|
+
doc.xpath('//check-captcha-result/ok').any?
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def api_check_captcha(request_id, captcha_id, value)
|
56
|
+
check_captcha_url = "#{API_URL}/check-captcha"
|
57
|
+
params = {
|
58
|
+
key: api_key,
|
59
|
+
id: request_id,
|
60
|
+
captcha: captcha_id,
|
61
|
+
value: value
|
62
|
+
}
|
63
|
+
|
64
|
+
uri = URI.parse(check_captcha_url)
|
65
|
+
uri.query = URI.encode_www_form(params)
|
66
|
+
|
67
|
+
Net::HTTP.get(uri)
|
68
|
+
end
|
69
|
+
|
70
|
+
def api_get_captcha(request_id)
|
71
|
+
get_captcha_url = "#{API_URL}/get-captcha"
|
72
|
+
params = {key: api_key, id: request_id}
|
73
|
+
|
74
|
+
uri = URI.parse(get_captcha_url)
|
75
|
+
uri.query = URI.encode_www_form(params)
|
76
|
+
|
77
|
+
Net::HTTP.get(uri)
|
78
|
+
end
|
79
|
+
|
80
|
+
def api_check_spam(*options)
|
81
|
+
cleanweb_options = {}
|
82
|
+
|
83
|
+
if options[0].is_a?(String) # quick check
|
84
|
+
cleanweb_options[:body_plain] = options[0]
|
85
|
+
else
|
86
|
+
options = options[0][0]
|
87
|
+
|
88
|
+
cleanweb_options.merge!({
|
89
|
+
"key" => api_key,
|
90
|
+
|
91
|
+
"body-plain" => options[:body_plain],
|
92
|
+
"body-html" => options[:body_html],
|
93
|
+
"body-bbcode" => options[:body_bbcode],
|
94
|
+
|
95
|
+
"subject-html" => options[:subject_html],
|
96
|
+
"subject-plain" => options[:subject_plain],
|
97
|
+
"subject-bbcode" => options[:subject_bbcode],
|
98
|
+
|
99
|
+
"ip" => options[:ip],
|
100
|
+
"email" => options[:email],
|
101
|
+
"name" => options[:name],
|
102
|
+
"login" => options[:login],
|
103
|
+
"realname" => options[:realname]
|
104
|
+
})
|
105
|
+
end
|
106
|
+
|
107
|
+
check_spam_url = "#{API_URL}/check-spam"
|
108
|
+
uri = URI.parse(check_spam_url)
|
109
|
+
response = Net::HTTP.post_form(uri, cleanweb_options)
|
110
|
+
response.body
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'yandex_cleanweb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "yandex_cleanweb"
|
8
|
+
gem.version = YandexCleanweb::VERSION
|
9
|
+
gem.authors = ["Kir Shatrov"]
|
10
|
+
gem.email = ["shatrov@me.com"]
|
11
|
+
gem.description = %q{Ruby wrapper for Yandex.Cleanweb}
|
12
|
+
gem.summary = %q{Ruby wrapper for Yandex.Cleanweb spam detector}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency 'nokogiri'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yandex_cleanweb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kir Shatrov
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Ruby wrapper for Yandex.Cleanweb
|
31
|
+
email:
|
32
|
+
- shatrov@me.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/yandex_cleanweb.rb
|
43
|
+
- lib/yandex_cleanweb/version.rb
|
44
|
+
- yandex_cleanweb.gemspec
|
45
|
+
homepage: ''
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.23
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Ruby wrapper for Yandex.Cleanweb spam detector
|
69
|
+
test_files: []
|