Skip to content

Commit 99d3a1e

Browse files
committed
Fix getUserQuestions(), use OAuth API instead of parsing HTML
1 parent 3ab3157 commit 99d3a1e

File tree

4 files changed

+10
-77
lines changed

4 files changed

+10
-77
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Working endpoints as of 9/21/18
5454
[Like](#like) |`.like(user_id, callback)` |✅|
5555
[Unlike](#unlike) |`.unlike(user_id, callback)` |✅|
5656
[Get User's Profile](#get-users-profile) |`.getUserProfile(username, callback)` |❌| Always returns html, newline if okc_api
57-
[Get User's Questions](#get-users-questions) |`.getUserQuestions(username, low, callback)` |❌| always returns html
57+
[Get User's Questions](#get-users-questions) |`.getUserQuestions(username, options, callback)` |✅|
5858
[Get Visitors](#get-visitors) |`.getVisitors(callback)` |❌| get JSON OKC developer/recruiting message
5959
[Send Message](#send-message) |`.sendMessage(user_id, message_body, callback)`|✅|
6060
[Get Recent Messages](#get-recent-messages) |`.getRecentMessages(callback)` |❌| route changed
@@ -117,11 +117,13 @@ Returns a json of the user profile. Contains all the information as you would se
117117

118118
---
119119
### Get User's Questions
120-
`.getUserQuestions(username, low, callback)`
120+
`.getUserQuestions(username, options, callback)`
121121

122-
Returns a json of user question data, beginning with the "low" question.
122+
Returns a json of user question/answer data.
123123

124-
The OkCupid API enforces pagination and won't return more than 10 questions per request, so to fetch all question data for a user, you need to make multiple calls and increment the "low" value. The index of a user's first answered question is 1. (Passing a value of 0 returns nothing.) For example:
124+
The results come back with a page hash. To get the next page results, this method can be called again with an additional `after` field set in the options object. The value should be set to the value stored in `body.paging.after`. This process can iterated to produce all search results.
125+
126+
The same process can also be applied in reverse with `before`.
125127

126128
```
127129
okc.getUserQuestions(username, 1, cb) // First 10 questions

custom_modules/constants.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ module.exports = {
44
rate: 'http://www.okcupid.com/quickmatch',
55
visit_user: 'http://www.okcupid.com/profile/{username}',
66
user_profile: 'http://www.okcupid.com/profile/{username}?okc_api=1',
7-
user_questions: 'http://www.okcupid.com/profile/{username}/questions?okc_api=1&low={low}',
8-
user_questions_no_api: 'http://www.okcupid.com/profile/{username}/questions?low={low}',
97
get_visitors: 'http://www.okcupid.com/visitors?okc_api=1',
108
quickmatch: 'http://www.okcupid.com/quickmatch?okc_api=1',
119

@@ -19,14 +17,7 @@ module.exports = {
1917
search: 'https://www.okcupid.com/1/apitun/match/search',
2018
edit_profile: 'https://www.okcupid.com/1/apitun/profile/save_essay',
2119
connections: 'https://www.okcupid.com/1/apitun/connections/outgoing',
20+
user_questions: 'http://www.okcupid.com/1/apitun/profile/{username}/answers',
2221
},
23-
SELECTORS: {
24-
current_page_selector : ".count > .curpage",
25-
total_pages_selector : ".count > .last",
26-
question_not_answered_selector : "div.question.not_answered",
27-
question_answered_selector : "div.question.public",
28-
question_text_selector : ".qtext > p",
29-
question_answer_selector : "p.answer.target"
30-
}
3122
}
3223

lib/client.js

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,13 @@
11
var constants = require('../custom_modules/constants')
22
var headers = require('../custom_modules/headers')
33
var Requester = require('./requester')
4-
var cheerio = require('cheerio')
54

65
var URLS = constants.URLS
7-
var SELECTORS = constants.SELECTORS
86

97
function getProxyString(proxy){
108
return `http://${(proxy.username && proxy.password) ? `${proxy.username}:${proxy.password}@` : ''}${proxy.ip}:${proxy.port}`
119
}
1210

13-
function parseQuestions(html, low, maxPageCount = null) {
14-
var $ = cheerio.load(html);
15-
var questionsData = {
16-
questions: {
17-
public: [],
18-
hidden: []
19-
},
20-
pagination: {
21-
low: low,
22-
next_page_low: low + 10,
23-
done: false,
24-
current_page: $(SELECTORS.current_page_selector).text(),
25-
total_num_pages: $(SELECTORS.total_pages_selector).text()
26-
}
27-
};
28-
questionsData.pagination.done = questionsData.pagination.current_page == questionsData.pagination.total_num_pages || questionsData.pagination.current_page == maxPageCount;
29-
const answeredQuestions = $(SELECTORS.question_answered_selector);
30-
const notAnsweredQuestions = $(SELECTORS.question_not_answered_selector);
31-
answeredQuestions.each(function(i, elem) {
32-
const question = $(this).find(SELECTORS.question_text_selector).text().trim();
33-
const answer = $(this).find(SELECTORS.question_answer_selector).text().trim();
34-
questionsData.questions.public.push({ question, answer })
35-
});
36-
notAnsweredQuestions.each(function(i, elem) {
37-
const question = $(this).find(SELECTORS.question_text_selector).text().trim();
38-
const answer = $(this).find(SELECTORS.question_answer_selector).text().trim();
39-
questionsData.questions.hidden.push({ question, answer })
40-
});
41-
return questionsData;
42-
}
43-
4411
var OKCupid = function(proxy = null){
4512
this.requester = new Requester(proxy)
4613
this.proxy = proxy;
@@ -109,36 +76,10 @@ OKCupid.prototype.getUserProfile = function(username, callback){
10976
var user_profile_url = URLS.user_profile.replace('{username}', username)
11077
this.requester.getRequest(user_profile_url, callback)
11178
}
112-
/*
113-
OKCupid.prototype.getUserQuestions = function(username, low, callback){
114-
var user_questions_url = URLS.user_questions.replace('{username}', username).replace('{low}', low)
115-
this.requester.getRequest(user_questions_url, callback)
116-
}*/
117-
118-
OKCupid.prototype.getUserQuestions = function(username, low, callback, maxPageCount = null){
119-
var user_questions_url = URLS.user_questions_no_api.replace('{username}', username).replace('{low}', low)
120-
this.requester.getRequestHtml(user_questions_url, function(err, res, body){
121-
if (err) callback(err, res, body)
122-
else {
123-
callback(err, res, parseQuestions(body, low, maxPageCount))
124-
}
125-
})
126-
}
127-
128-
OKCupid.prototype.getAllUserQuestions = function(username, callback, maxPageCount = null){
129-
this.getUserQuestionsUntilDone(username, 1, callback, maxPageCount, [])
130-
}
13179

132-
OKCupid.prototype.getUserQuestionsUntilDone = function(username, low, callback, maxPageCount, questionsSoFar) {
133-
var doAgain = (err, res, body) => {
134-
if (err) callback(err, res, questions)
135-
questionsSoFar = questionsSoFar.concat(body.questions)
136-
if(body.pagination.done) callback(err, res, questionsSoFar)
137-
else {
138-
this.getUserQuestionsUntilDone(username, body.pagination.next_page_low, callback, maxPageCount, questionsSoFar)
139-
}
140-
}
141-
this.getUserQuestions(username, low, doAgain, maxPageCount)
80+
OKCupid.prototype.getUserQuestions = function(username, options, callback){
81+
var user_questions_url = URLS.user_questions.replace('{username}', username)
82+
this.requester.postJsonRequest(user_questions_url, options, callback)
14283
}
14384

14485
OKCupid.prototype.getVisitors = function(callback){

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"node": "*"
2626
},
2727
"dependencies": {
28-
"cheerio": "^1.0.0-rc.2",
2928
"request": "~2.83.0"
3029
}
3130
}

0 commit comments

Comments
 (0)