quora-client 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
@@ -0,0 +1,49 @@
1
+
2
+ # Introduction
3
+
4
+ Quora client enables the communication with Quora API via REST
5
+ interface.
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.
9
+
10
+ If you want to get the cookie value, you can use a local proxy, sniffer, etc to get the correct value, that
11
+ should be something similar to:
12
+
13
+ "m-b=<m-b-value>; m-f=<m-f-value>; m-s=<m-s-value>; ..."
14
+
15
+ # Install
16
+
17
+ Just install the gem:
18
+
19
+ > gem install quora-client
20
+
21
+ # How to use Quora Client
22
+
23
+ > require 'rubygems'
24
+ >
25
+ > require 'quora-client'
26
+ >
27
+ > cookie ="<valid_value>"
28
+ >
29
+ > client = Quora::Client.new(cookie)
30
+ >
31
+ > # or...
32
+ >
33
+ > client = Quora::Client.new({:user => <user>, :password => <password>})
34
+ >
35
+ > user_data = client.get_all
36
+
37
+ # Support methods
38
+
39
+ Currently Quora supported fields include inbox, followers, following and notifs. Each field has an associated method in the client:
40
+
41
+ > client.get_inbox
42
+ >
43
+ > client.get_followers
44
+ >
45
+ > client.get_following
46
+ >
47
+ > client.get_notifs
48
+ >
49
+
@@ -0,0 +1,10 @@
1
+ require 'quora/client'
2
+
3
+ #
4
+ # Quora client enables the communication with Quora API via REST interface
5
+ #
6
+ # (c) Juan de Bravo <juandebravo@gmail.com>
7
+ #
8
+ module Quora
9
+ VERSION = "0.0.2"
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
@@ -0,0 +1,164 @@
1
+ require 'rubygems'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'quora/auth'
6
+
7
+ #
8
+ # Quora client enables the communication with Quora API via REST interface
9
+ #
10
+ # (c) Juan de Bravo <juandebravo@gmail.com>
11
+ #
12
+
13
+ module Quora
14
+ #
15
+ # This is the main class to interact with Quora API.
16
+ #
17
+ # Actually there's no API security mechanism so interaction with API is
18
+ # based on authentication cookie. You should get all the Quora cookies value
19
+ # from you browser and use it as argument while creating the Quora client.
20
+ #
21
+ # cookie = "m-b=<m-b-value>; m-f=<m-f-value>; m-s=<m-s-value>; ..."
22
+ #
23
+ # client = Quora::Client.new(cookie)
24
+ #
25
+ # values = client.get_all
26
+ #
27
+
28
+ class Client
29
+ include Quora::Auth
30
+
31
+ QUORA_URI = "http://api.quora.com"
32
+
33
+ RESP_PREFIX = "while(1);"
34
+
35
+ BASEPATH = "/api/logged_in_user"
36
+
37
+ SUPPORTED_FIELDS = %W{inbox followers following notifs}
38
+
39
+ #
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})
46
+ #
47
+ def initialize(params)
48
+ if params.nil?
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
59
+ end
60
+ @cookie = cookie
61
+ end
62
+
63
+ #
64
+ # Get all the user information available
65
+ #
66
+ def get_all
67
+ fields = SUPPORTED_FIELDS.join(",")
68
+ get(fields)
69
+ end
70
+
71
+ #
72
+ # Base method to send a request to Quora API.
73
+ # @param [required, string] supported field (or multiple fields CSV) to retrieve
74
+ # @param [optional, bool] filter if field is a key in result hash, only this
75
+ # value is returned
76
+ #
77
+ def get(field, filter = true)
78
+ if field.nil? or !field.instance_of?(String)
79
+ raise ArgumentError, "Field value must be a string"
80
+ end
81
+ resp = http.get("#{BASEPATH}?fields=#{field}", headers)
82
+ data = resp.body[RESP_PREFIX.length..-1]
83
+ data = JSON.parse(data)
84
+ if filter && data.has_key?(field)
85
+ data[field]
86
+ else
87
+ data
88
+ end
89
+ end
90
+
91
+ #
92
+ # Define a method for each of the supported fields
93
+ #
94
+ SUPPORTED_FIELDS.each{|field|
95
+ define_method("get_#{field}"){
96
+ get(field)
97
+ }
98
+ }
99
+
100
+ #
101
+ # Any method that starts with "get_" will be defined so if new fields
102
+ # are supported there's no need to fix the client
103
+ #
104
+ def respond_to?(method_id, include_private = false)
105
+ if method_id.to_s =~ /^get_[\w]+/
106
+ true
107
+ else
108
+ super
109
+ end
110
+ end
111
+
112
+ #
113
+ # Override method_missing so any method that starts with "get_" will be
114
+ # defined.
115
+ #
116
+ # i.e.
117
+ # client.get_profile
118
+ #
119
+ # will generate =>
120
+ #
121
+ # def get_profile
122
+ # get("profile")
123
+ # end
124
+ #
125
+ def method_missing(method_id, *arguments, &block)
126
+ if method_id.to_s =~ /^get_[\w]+/
127
+ self.class.send :define_method, method_id do
128
+ field = method_id.to_s[4..-1]
129
+ get(field)
130
+ end
131
+ self.send(method_id)
132
+ else
133
+ super
134
+ end
135
+ end
136
+
137
+ private
138
+
139
+ #
140
+ # Create (if not done before) and return the instance HTTP client
141
+ #
142
+ def http
143
+ @http ||= Net::HTTP.new(endpoint.host, endpoint.port)
144
+ end
145
+
146
+ #
147
+ # Create (if not done before) and return the Quora API endpoint
148
+ #
149
+ def endpoint
150
+ @@endpoint ||= URI.parse(QUORA_URI)
151
+ end
152
+
153
+ #
154
+ # Set headers
155
+ #
156
+ def headers
157
+ @cookie.nil? and raise ArgumentError, "cookie must be provided"
158
+ {
159
+ "Cookie" => @cookie
160
+ }
161
+ end
162
+
163
+ end
164
+ end
@@ -0,0 +1,79 @@
1
+ #!/opt/local/bin/ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
4
+
5
+ require 'test/unit'
6
+ require 'quora'
7
+
8
+
9
+ #
10
+ # Quora client enables the communication with Quora API via REST interface
11
+ #
12
+ # (c) Juan de Bravo <juandebravo@gmail.com>
13
+ #
14
+ # Methods to test Quora client
15
+ #
16
+ class TestQuoraClient < Test::Unit::TestCase
17
+
18
+ include Quora::Auth
19
+
20
+ def setup
21
+ if ARGV.length == 0
22
+ @cookie = "invalid value"
23
+ elsif ARGV.length == 1
24
+ @cookie = ARGV[0]
25
+ else
26
+ @user = ARGV[0]
27
+ @password = ARGV[1]
28
+ end
29
+
30
+ end
31
+
32
+ def test_get_all
33
+ p client.get_all
34
+ end
35
+
36
+
37
+ def test_get_client_methods
38
+ Quora::Client::SUPPORTED_FIELDS.each{|field|
39
+ assert_equal client.class.instance_methods.index("get_#{field}") > 0, true
40
+ }
41
+
42
+ end
43
+
44
+ def test_respond_to?
45
+ client.class.instance_methods(false).each{|method|
46
+ assert_equal client.respond_to?(method), true
47
+ }
48
+ end
49
+
50
+ def test_login
51
+ assert_equal login(@user, @password).length > 0, true
52
+ end
53
+
54
+ private
55
+
56
+ def client
57
+ if !@user.nil?
58
+ client = Quora::Client.new({:user => @user, :password => @password})
59
+ else
60
+ client = Quora::Client.new(@cookie)
61
+ end
62
+ end
63
+ end
64
+
65
+ #
66
+ # Define a test helper to retrieve per each supported field
67
+ # test_get_inbox
68
+ # test_get_notifs
69
+ # test_get_followers
70
+ # test_get_following
71
+ #
72
+ Quora::Client::SUPPORTED_FIELDS.each{|field|
73
+ TestQuoraClient.send :define_method, "test_get_#{field}" do
74
+ p client.send "get_#{field}".to_s
75
+ end
76
+ }
77
+
78
+
79
+
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quora-client
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Juan de Bravo
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-17 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json_pure
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 3
34
+ version: 1.4.3
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: test-unit
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 11
46
+ segments:
47
+ - 2
48
+ - 1
49
+ - 0
50
+ version: 2.1.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: This GEM provides an easy way to access Quora Alpha API
54
+ email: juandebravo@gmail.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.md
61
+ - LICENSE.LGPLv3
62
+ files:
63
+ - lib/quora/auth.rb
64
+ - lib/quora/client.rb
65
+ - lib/quora.rb
66
+ - README.md
67
+ - LICENSE.LGPLv3
68
+ - test/test_quora_client.rb
69
+ has_rdoc: true
70
+ homepage: http://github.com/juandebravo/quora-client
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.4.2
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: This GEM provides an easy way to access Quora Alpha API
103
+ test_files:
104
+ - test/test_quora_client.rb