quora-client 0.1 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,9 +4,10 @@
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. 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.
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.
8
9
 
9
- You can use a local proxy, sniffer, etc to get the correct value, that
10
+ If you want to get the cookie value, you can use a local proxy, sniffer, etc to get the correct value, that
10
11
  should be something similar to:
11
12
 
12
13
  "m-b=<m-b-value>; m-f=<m-f-value>; m-s=<m-s-value>; ..."
@@ -27,6 +28,10 @@ Just install the gem:
27
28
  >
28
29
  > client = Quora::Client.new(cookie)
29
30
  >
31
+ > # or...
32
+ >
33
+ > client = Quora::Client.new({:user => <user>, :password => <password>})
34
+ >
30
35
  > user_data = client.get_all
31
36
 
32
37
  # Support methods
@@ -6,5 +6,5 @@ require 'quora/client'
6
6
  # (c) Juan de Bravo <juandebravo@gmail.com>
7
7
  #
8
8
  module Quora
9
- VERSION = "0.0.1"
9
+ VERSION = "0.0.2"
10
10
  end
@@ -0,0 +1,69 @@
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
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'net/http'
3
3
  require 'uri'
4
4
  require 'json'
5
+ require 'quora/auth'
5
6
 
6
7
  #
7
8
  # Quora client enables the communication with Quora API via REST interface
@@ -25,6 +26,8 @@ module Quora
25
26
  #
26
27
 
27
28
  class Client
29
+ include Quora::Auth
30
+
28
31
  QUORA_URI = "http://api.quora.com"
29
32
 
30
33
  RESP_PREFIX = "while(1);"
@@ -34,11 +37,25 @@ module Quora
34
37
  SUPPORTED_FIELDS = %W{inbox followers following notifs}
35
38
 
36
39
  #
37
- # Initialize the client. Cookie value must be provided
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})
38
46
  #
39
- def initialize(cookie)
40
- if cookie.nil? or !cookie.instance_of?(String)
47
+ def initialize(params)
48
+ if params.nil?
41
49
  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
42
59
  end
43
60
  @cookie = cookie
44
61
  end
@@ -53,7 +70,7 @@ module Quora
53
70
 
54
71
  #
55
72
  # Base method to send a request to Quora API.
56
- # @param [reuired, string] supported field (or multiple fields CSV) to retrieve
73
+ # @param [required, string] supported field (or multiple fields CSV) to retrieve
57
74
  # @param [optional, bool] filter if field is a key in result hash, only this
58
75
  # value is returned
59
76
  #
@@ -15,11 +15,16 @@ require 'quora'
15
15
  #
16
16
  class TestQuoraClient < Test::Unit::TestCase
17
17
 
18
+ include Quora::Auth
19
+
18
20
  def setup
19
21
  if ARGV.length == 0
20
22
  @cookie = "invalid value"
21
- else
23
+ elsif ARGV.length == 1
22
24
  @cookie = ARGV[0]
25
+ else
26
+ @user = ARGV[0]
27
+ @password = ARGV[1]
23
28
  end
24
29
 
25
30
  end
@@ -42,10 +47,18 @@ class TestQuoraClient < Test::Unit::TestCase
42
47
  }
43
48
  end
44
49
 
50
+ def test_login
51
+ assert_equal login(@user, @password).length > 0, true
52
+ end
53
+
45
54
  private
46
55
 
47
56
  def client
48
- client = Quora::Client.new(@cookie)
57
+ if !@user.nil?
58
+ client = Quora::Client.new({:user => @user, :password => @password})
59
+ else
60
+ client = Quora::Client.new(@cookie)
61
+ end
49
62
  end
50
63
  end
51
64
 
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quora-client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- version: "0.1"
9
+ - 1
10
+ version: 0.1.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Juan de Bravo
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-01-11 00:00:00 +01:00
18
+ date: 2011-01-17 00:00:00 +01:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -59,6 +60,7 @@ extra_rdoc_files:
59
60
  - README.md
60
61
  - LICENSE.LGPLv3
61
62
  files:
63
+ - lib/quora/auth.rb
62
64
  - lib/quora/client.rb
63
65
  - lib/quora.rb
64
66
  - README.md