nerdz 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/nerdz.rb +85 -0
- data/lib/nerdz/config.rb +29 -0
- data/lib/nerdz/errors.rb +49 -0
- data/lib/nerdz/http.rb +97 -0
- data/lib/nerdz/rebinds.rb +100 -0
- data/lib/nerdz/structs.rb +40 -0
- data/lib/nerdz/version.rb +22 -0
- metadata +82 -0
data/lib/nerdz.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
require 'json'
|
21
|
+
require 'nerdz/http'
|
22
|
+
require 'nerdz/errors'
|
23
|
+
require 'nerdz/structs'
|
24
|
+
|
25
|
+
class Nerdz
|
26
|
+
def initialize
|
27
|
+
@http = HTTP.new
|
28
|
+
end
|
29
|
+
|
30
|
+
def request(*args)
|
31
|
+
res = JSON.parse(@http.request(*args).body)
|
32
|
+
error = res['error']
|
33
|
+
error = ERRORS[error ? error.to_i : error]
|
34
|
+
raise error if error
|
35
|
+
res
|
36
|
+
end
|
37
|
+
|
38
|
+
def login(username, password)
|
39
|
+
request(:login, {username: username, password: password})
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
def logout
|
44
|
+
request(:logout)
|
45
|
+
true
|
46
|
+
end
|
47
|
+
|
48
|
+
def user(id_or_name)
|
49
|
+
case id_or_name
|
50
|
+
when Integer
|
51
|
+
User.new(id_or_name, nil)
|
52
|
+
when String, Symbol
|
53
|
+
User.new(request(:get_id, {username: id_or_name})['id'], id_or_name.to_s)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def nerdz(message, to = nil)
|
58
|
+
opts = {message: message}
|
59
|
+
opts[:to] = self.user(to).id if to
|
60
|
+
request(:nerdz_it, opts)
|
61
|
+
true
|
62
|
+
end
|
63
|
+
|
64
|
+
def share(url, message = "", to = nil)
|
65
|
+
opts = {url: url, message: message}
|
66
|
+
opts[:to] = self.user(to).id if to
|
67
|
+
request(:share_it, opts)
|
68
|
+
true
|
69
|
+
end
|
70
|
+
|
71
|
+
def nerdzs(what, args = {})
|
72
|
+
opts = {what: what}
|
73
|
+
opts[:limit] = args[:from] ? "#{args[:from]},#{args[:max]}" : args[:max].to_s if args[:max]
|
74
|
+
opts[:id] = self.user(args[:user]).id if args[:user]
|
75
|
+
opts[:pid] = args[:pid] if args[:pid]
|
76
|
+
res = request(:nerdzs, opts)
|
77
|
+
if res.values.first.is_a?(Hash)
|
78
|
+
res.values.map {|nerdz|
|
79
|
+
Post.new(nerdz)
|
80
|
+
}
|
81
|
+
else
|
82
|
+
Post.new(res)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/nerdz/config.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
class String
|
21
|
+
def esc_split(s)
|
22
|
+
return if s.size != 1
|
23
|
+
e = Regexp.escape(s.to_s)
|
24
|
+
|
25
|
+
self.scan(/(?:((?:\\#{e}|[^#{e}])*)(?:$|#{e}))/).flatten.tap {|res|
|
26
|
+
break res[0..-2] if !self.end_with?(s.to_s)
|
27
|
+
}
|
28
|
+
end
|
29
|
+
end
|
data/lib/nerdz/errors.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
def exception
|
21
|
+
Class.new(Exception)
|
22
|
+
end
|
23
|
+
|
24
|
+
class Nerdz
|
25
|
+
NotLoggedIn = exception
|
26
|
+
UserNotFound = exception
|
27
|
+
GETQueryNotFound = exception
|
28
|
+
POSTQueryNotFound = exception
|
29
|
+
UndefinedError = exception
|
30
|
+
LoginError = exception
|
31
|
+
InsertError = exception
|
32
|
+
ShareError = exception
|
33
|
+
InvalidUrl = exception
|
34
|
+
InvalidAction = exception
|
35
|
+
NotOnline = exception
|
36
|
+
|
37
|
+
ERRORS = Class.new {
|
38
|
+
def initialize
|
39
|
+
@errors = [nil, NotLoggedIn, UserNotFound, GETQueryNotFound,
|
40
|
+
POSTQueryNotFound, UndefinedError, LoginError, InsertError,
|
41
|
+
ShareError, InvalidUrl, InvalidAction, NotOnline].freeze
|
42
|
+
end
|
43
|
+
|
44
|
+
def [](x)
|
45
|
+
x = x.to_s.to_i unless x.is_a?(Integer)
|
46
|
+
@errors[x.to_i]
|
47
|
+
end
|
48
|
+
}.new
|
49
|
+
end
|
data/lib/nerdz/http.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
require 'cookiejar'
|
21
|
+
require 'nerdz/rebinds'
|
22
|
+
|
23
|
+
module CookieJar
|
24
|
+
class Jar
|
25
|
+
def self.load_ns(file)
|
26
|
+
self.from_ns(File.read(file))
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.from_ns(string)
|
30
|
+
jar = self.new
|
31
|
+
|
32
|
+
string.each_line {|l|
|
33
|
+
if l =~ (/^((?:^[^^]+))\s+(TRUE|FALSE)\s+(\S+)\s+(TRUE|FALSE)\s+(\d+)\s+(\S+)\s+(\S+)\s*$/)
|
34
|
+
jar.add_cookie Cookie.new(domain: $1, path: $3, secure: $4 == 'TRUE' ? true : false,
|
35
|
+
expires_at: Time.at($5.to_i), name: $6, value: $7)
|
36
|
+
end
|
37
|
+
}
|
38
|
+
|
39
|
+
jar
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_ns
|
43
|
+
"# Netscape HTTP Cookie File\n" +
|
44
|
+
"# http://curl.haxx.se/rfc/cookie_spec.html\n" +
|
45
|
+
"# This file was generated by nerdz! Edit at your own risk.\n\n" +
|
46
|
+
to_a.map {|cookie|
|
47
|
+
"#{cookie.domain}\t#{cookie.domain[0] == ?. ? 'TRUE' : 'FALSE'}\t#{cookie.path} #{cookie.secure ? 'TRUE' : 'FALSE'}"+
|
48
|
+
"\t#{cookie.expires_at.to_i}\t#{cookie.name}\t#{cookie.value}\n" if !cookie.session?
|
49
|
+
}.compact.join
|
50
|
+
end
|
51
|
+
|
52
|
+
def save_ns(file)
|
53
|
+
File.open(file, 'w') {|f|
|
54
|
+
f.write(self.to_ns)
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def [](name)
|
59
|
+
self.get_cookies('http://www.megaupload.com/').select {|cookie|
|
60
|
+
cookie.name == name
|
61
|
+
}.first
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Nerdz
|
67
|
+
class HTTP
|
68
|
+
attr_reader :cookies
|
69
|
+
|
70
|
+
def initialize
|
71
|
+
@cookies = CookieJar::Jar.new
|
72
|
+
end
|
73
|
+
|
74
|
+
def post(path, params = {}, data = {}, initheader = nil, &blk)
|
75
|
+
self.__request(initheader) {|http, headers|
|
76
|
+
http.post(path, params, data, headers, nil, &blk)
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
def request(action, data = {})
|
81
|
+
self.post('/api.php', {action: action}, data)
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
def __request(initheader)
|
86
|
+
return unless block_given?
|
87
|
+
initheader = {} unless initheader.is_a?(Hash)
|
88
|
+
|
89
|
+
initheader['Cookie'] = @cookies.get_cookie_header('http://www.nerdz.eu/')
|
90
|
+
|
91
|
+
response, * = yield(Net::HTTP.start('www.nerdz.eu', 80), initheader)
|
92
|
+
|
93
|
+
@cookies.set_cookies_from_headers('http://www.nerdz.eu/', response)
|
94
|
+
response
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
require 'cgi'
|
21
|
+
require 'net/http'
|
22
|
+
require 'nerdz/version'
|
23
|
+
|
24
|
+
class URI::Generic
|
25
|
+
def request
|
26
|
+
x = self.dup
|
27
|
+
x.component.take_while {|y| y != :path }.each {|y| x.send("#{y}=".to_sym, nil) }
|
28
|
+
x.to_s
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Hash
|
33
|
+
def to_param
|
34
|
+
self.to_a.map {|kv|
|
35
|
+
kv.map {|v| CGI.escape(v.to_s) }.join(?=)
|
36
|
+
}.join(?&)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.from_param(par)
|
40
|
+
Hash[par.split('&').map {|x| x.split('=', 2).map {|y| CGI.unescape(y) } }]
|
41
|
+
rescue
|
42
|
+
{}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class String
|
47
|
+
def merge_param(par)
|
48
|
+
uri = URI.parse(self)
|
49
|
+
uri.query = Hash.from_param(uri.query).merge(par).to_param
|
50
|
+
uri.to_s
|
51
|
+
end
|
52
|
+
|
53
|
+
def merge_param!(par)
|
54
|
+
self.replace(self.merge_param(par))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module Net
|
59
|
+
class HTTPResponse
|
60
|
+
def [](key)
|
61
|
+
@header[key.to_s.downcase].join(', ')
|
62
|
+
rescue
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def keys
|
67
|
+
@header.keys
|
68
|
+
end
|
69
|
+
|
70
|
+
def values
|
71
|
+
@header.values
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class HTTP
|
76
|
+
alias __old_post__ post
|
77
|
+
def post(path, params = {}, data = {}, initheader = nil, dest = nil, &block)
|
78
|
+
path.merge_param!(params)
|
79
|
+
data = data.to_param if data.is_a?(Hash)
|
80
|
+
|
81
|
+
__old_post__(path, data, initheader, dest, &block)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class HTTPGenericRequest
|
86
|
+
def initialize(m, reqbody, resbody, path, initheader = nil)
|
87
|
+
@method = m
|
88
|
+
@request_has_body = reqbody
|
89
|
+
@response_has_body = resbody
|
90
|
+
raise ArgumentError, "no HTTP request path given" unless path
|
91
|
+
raise ArgumentError, "HTTP request path is empty" if path.empty?
|
92
|
+
@path = path
|
93
|
+
initialize_http_header initheader
|
94
|
+
self['Accept'] ||= '*/*'
|
95
|
+
self['User-Agent'] ||= "Ruby Nerdz #{Nerdz::VERSION}"
|
96
|
+
@body = nil
|
97
|
+
@body_stream = nil
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
class Nerdz
|
21
|
+
class User
|
22
|
+
attr_reader :id, :name
|
23
|
+
def initialize(id, name)
|
24
|
+
@id = id.to_i
|
25
|
+
@name = name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Post
|
30
|
+
attr_reader :from, :to, :pid, :message, :time
|
31
|
+
|
32
|
+
def initialize(h)
|
33
|
+
@to = User.new(h['to'], h['to_user'])
|
34
|
+
@from = User.new(h['from'], h['from_user'])
|
35
|
+
@pid = h['pid'].to_i rescue nil
|
36
|
+
@message = h['message']
|
37
|
+
@time = Time.mktime(*(h['date'].split(?/).reverse + (h['time'] || '').split(?:))) if h['date']
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
#--
|
2
|
+
# Copyleft shura. [ shura1991@gmail.com ]
|
3
|
+
#
|
4
|
+
# This file is part of nerdz.
|
5
|
+
#
|
6
|
+
# nerdz is free software: you can redistribute it and/or modify
|
7
|
+
# it under the terms of the GNU Affero General Public License as published
|
8
|
+
# by the Free Software Foundation, either version 3 of the License, or
|
9
|
+
# (at your option) any later version.
|
10
|
+
#
|
11
|
+
# nerdz is distributed in the hope that it will be useful,
|
12
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14
|
+
# GNU Affero General Public License for more details.
|
15
|
+
#
|
16
|
+
# You should have received a copy of the GNU Affero General Public License
|
17
|
+
# along with nerdz. If not, see <http://www.gnu.org/licenses/>.
|
18
|
+
#++
|
19
|
+
|
20
|
+
class Nerdz
|
21
|
+
VERSION = '0.0.1'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nerdz
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- shura
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-05-10 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: cookiejar
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
description: pretty as tiny library to play with nerdz.eu API
|
34
|
+
email: shura1991@gmail.com
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files: []
|
40
|
+
|
41
|
+
files:
|
42
|
+
- lib/nerdz.rb
|
43
|
+
- lib/nerdz/structs.rb
|
44
|
+
- lib/nerdz/errors.rb
|
45
|
+
- lib/nerdz/version.rb
|
46
|
+
- lib/nerdz/rebinds.rb
|
47
|
+
- lib/nerdz/config.rb
|
48
|
+
- lib/nerdz/http.rb
|
49
|
+
has_rdoc: true
|
50
|
+
homepage: http://github.com/shurizzle/nerdz
|
51
|
+
licenses: []
|
52
|
+
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.7
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: pretty as tiny library to play with nerdz.eu API
|
81
|
+
test_files: []
|
82
|
+
|