quora-client 0.0.2 → 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -7
- data/lib/quora.rb +1 -1
- data/lib/quora/client.rb +4 -21
- data/test/test_quora_client.rb +2 -15
- metadata +4 -6
- data/lib/quora/auth.rb +0 -69
data/README.md
CHANGED
@@ -4,10 +4,9 @@
|
|
4
4
|
Quora client enables the communication with Quora API via REST
|
5
5
|
interface.
|
6
6
|
|
7
|
-
Actually there's no API security mechanism so interaction with API is based on authentication cookie.
|
8
|
-
You can provide either a valid cookie or a valid pair user - password.
|
7
|
+
Actually there's no API security mechanism so interaction with API is based on authentication cookie. You should get all the Quora cookies value from you browser and use it as argument while creating the Quora client. I know this is unfriendly but I didn't get any other option right now.
|
9
8
|
|
10
|
-
|
9
|
+
You can use a local proxy, sniffer, etc to get the correct value, that
|
11
10
|
should be something similar to:
|
12
11
|
|
13
12
|
"m-b=<m-b-value>; m-f=<m-f-value>; m-s=<m-s-value>; ..."
|
@@ -28,10 +27,6 @@ Just install the gem:
|
|
28
27
|
>
|
29
28
|
> client = Quora::Client.new(cookie)
|
30
29
|
>
|
31
|
-
> # or...
|
32
|
-
>
|
33
|
-
> client = Quora::Client.new({:user => <user>, :password => <password>})
|
34
|
-
>
|
35
30
|
> user_data = client.get_all
|
36
31
|
|
37
32
|
# Support methods
|
data/lib/quora.rb
CHANGED
data/lib/quora/client.rb
CHANGED
@@ -2,7 +2,6 @@ require 'rubygems'
|
|
2
2
|
require 'net/http'
|
3
3
|
require 'uri'
|
4
4
|
require 'json'
|
5
|
-
require 'quora/auth'
|
6
5
|
|
7
6
|
#
|
8
7
|
# Quora client enables the communication with Quora API via REST interface
|
@@ -26,8 +25,6 @@ module Quora
|
|
26
25
|
#
|
27
26
|
|
28
27
|
class Client
|
29
|
-
include Quora::Auth
|
30
|
-
|
31
28
|
QUORA_URI = "http://api.quora.com"
|
32
29
|
|
33
30
|
RESP_PREFIX = "while(1);"
|
@@ -37,25 +34,11 @@ module Quora
|
|
37
34
|
SUPPORTED_FIELDS = %W{inbox followers following notifs}
|
38
35
|
|
39
36
|
#
|
40
|
-
# Initialize the client.
|
41
|
-
# @param [required, string|Hash] User identification. Can be either a valid cookie
|
42
|
-
# previously authenticated or an Hash with :user and :password
|
43
|
-
#
|
44
|
-
# client = Client.new(valid_cookie)
|
45
|
-
# client = Client.new({:user => valid_user, :password => valid_password})
|
37
|
+
# Initialize the client. Cookie value must be provided
|
46
38
|
#
|
47
|
-
def initialize(
|
48
|
-
if
|
39
|
+
def initialize(cookie)
|
40
|
+
if cookie.nil? or !cookie.instance_of?(String)
|
49
41
|
raise ArgumentError, "Cookie value must be provided"
|
50
|
-
else
|
51
|
-
if params.instance_of?(String)
|
52
|
-
cookie = params
|
53
|
-
elsif params.instance_of?(Hash)
|
54
|
-
user = params[:user]
|
55
|
-
password = params[:password]
|
56
|
-
user.nil? or password.nil? and raise ArgumentError, "user and password must be provided"
|
57
|
-
cookie = login(user, password)
|
58
|
-
end
|
59
42
|
end
|
60
43
|
@cookie = cookie
|
61
44
|
end
|
@@ -70,7 +53,7 @@ module Quora
|
|
70
53
|
|
71
54
|
#
|
72
55
|
# Base method to send a request to Quora API.
|
73
|
-
# @param [
|
56
|
+
# @param [reuired, string] supported field (or multiple fields CSV) to retrieve
|
74
57
|
# @param [optional, bool] filter if field is a key in result hash, only this
|
75
58
|
# value is returned
|
76
59
|
#
|
data/test/test_quora_client.rb
CHANGED
@@ -15,16 +15,11 @@ require 'quora'
|
|
15
15
|
#
|
16
16
|
class TestQuoraClient < Test::Unit::TestCase
|
17
17
|
|
18
|
-
include Quora::Auth
|
19
|
-
|
20
18
|
def setup
|
21
19
|
if ARGV.length == 0
|
22
20
|
@cookie = "invalid value"
|
23
|
-
elsif ARGV.length == 1
|
24
|
-
@cookie = ARGV[0]
|
25
21
|
else
|
26
|
-
@
|
27
|
-
@password = ARGV[1]
|
22
|
+
@cookie = ARGV[0]
|
28
23
|
end
|
29
24
|
|
30
25
|
end
|
@@ -47,18 +42,10 @@ class TestQuoraClient < Test::Unit::TestCase
|
|
47
42
|
}
|
48
43
|
end
|
49
44
|
|
50
|
-
def test_login
|
51
|
-
assert_equal login(@user, @password).length > 0, true
|
52
|
-
end
|
53
|
-
|
54
45
|
private
|
55
46
|
|
56
47
|
def client
|
57
|
-
|
58
|
-
client = Quora::Client.new({:user => @user, :password => @password})
|
59
|
-
else
|
60
|
-
client = Quora::Client.new(@cookie)
|
61
|
-
end
|
48
|
+
client = Quora::Client.new(@cookie)
|
62
49
|
end
|
63
50
|
end
|
64
51
|
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: quora-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
|
10
|
-
version: 0.0.2
|
8
|
+
- 1
|
9
|
+
version: "0.1"
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Juan de Bravo
|
@@ -15,7 +14,7 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-11 00:00:00 +01:00
|
19
18
|
default_executable:
|
20
19
|
dependencies:
|
21
20
|
- !ruby/object:Gem::Dependency
|
@@ -60,7 +59,6 @@ extra_rdoc_files:
|
|
60
59
|
- README.md
|
61
60
|
- LICENSE.LGPLv3
|
62
61
|
files:
|
63
|
-
- lib/quora/auth.rb
|
64
62
|
- lib/quora/client.rb
|
65
63
|
- lib/quora.rb
|
66
64
|
- README.md
|
data/lib/quora/auth.rb
DELETED
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'net/http'
|
3
|
-
require 'uri'
|
4
|
-
require 'cgi'
|
5
|
-
|
6
|
-
|
7
|
-
module Quora
|
8
|
-
module Auth
|
9
|
-
|
10
|
-
QUORA_URI = "http://www.quora.com"
|
11
|
-
|
12
|
-
def login(user, password)
|
13
|
-
endpoint = URI.parse(QUORA_URI)
|
14
|
-
|
15
|
-
http = Net::HTTP.new(endpoint.host, endpoint.port)
|
16
|
-
resp = http.get('/login/')
|
17
|
-
cookie = resp["set-cookie"]
|
18
|
-
|
19
|
-
# TODO: improve this rubbish
|
20
|
-
# get formkey value
|
21
|
-
start = resp.body.index("Q.formkey")
|
22
|
-
formkey = resp.body[start..start+200].split("\"")[1]
|
23
|
-
|
24
|
-
# get window value
|
25
|
-
start = resp.body.index("webnode2.windowId")
|
26
|
-
window = resp.body[start..start+200].split("\"")[1]
|
27
|
-
|
28
|
-
# get __vcon_json value
|
29
|
-
start = resp.body.index("InlineLogin")
|
30
|
-
vcon_json = resp.body[start..start+200]
|
31
|
-
start = vcon_json.index("live")
|
32
|
-
vcon_json = vcon_json[start..-1]
|
33
|
-
vcon_json = vcon_json.split("\"")[0]
|
34
|
-
vcon_json = vcon_json.split(":")
|
35
|
-
vcon_json.map! { |value| "\"#{value}\"" }
|
36
|
-
|
37
|
-
vcon_json = "[#{vcon_json.join(",")}]"
|
38
|
-
vcon_json = CGI::escape(vcon_json)
|
39
|
-
|
40
|
-
user = CGI::escape(user)
|
41
|
-
password = CGI::escape(password)
|
42
|
-
|
43
|
-
body = "json=%7B%22args%22%3A%5B%22#{user}%22%2C%22#{password}%22%2Ctrue%5D%2C%22kwargs%22%3A%7B%7D%7D&formkey=#{formkey}&window_id=#{window}&__vcon_json=#{vcon_json}&__vcon_method=do_login"
|
44
|
-
|
45
|
-
headers = {
|
46
|
-
"Content-Type" => "application/x-www-form-urlencoded",
|
47
|
-
"X-Requested-With" => "XMLHttpRequest",
|
48
|
-
"Accept" => "application/json, text/javascript, */*",
|
49
|
-
"Cookie" => cookie,
|
50
|
-
"User-Agent" => "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10",
|
51
|
-
"Content-Length" => body.length.to_s,
|
52
|
-
"Accept-Charset" => "ISO-8859-1,utf-8;q=0.7,*;q=0.3",
|
53
|
-
"Accept-Language" => "es-ES,es;q=0.8",
|
54
|
-
"Accept-Encoding" => "gzip,deflate,sdch",
|
55
|
-
"Origin" => "http://www.quora.com",
|
56
|
-
"Host" => "www.quora.com",
|
57
|
-
"Referer" => "http://www.quora.com/login/"
|
58
|
-
}
|
59
|
-
|
60
|
-
resp = http.post("/webnode2/server_call_POST", body, headers)
|
61
|
-
|
62
|
-
if resp.code == "200"
|
63
|
-
cookie
|
64
|
-
else
|
65
|
-
""
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|