|
1 | 1 | var constants = require('../custom_modules/constants') |
2 | 2 | var headers = require('../custom_modules/headers') |
3 | 3 | var Requester = require('./requester') |
4 | | -var cheerio = require('cheerio') |
5 | 4 |
|
6 | 5 | var URLS = constants.URLS |
7 | | -var SELECTORS = constants.SELECTORS |
8 | 6 |
|
9 | 7 | function getProxyString(proxy){ |
10 | 8 | return `http://${(proxy.username && proxy.password) ? `${proxy.username}:${proxy.password}@` : ''}${proxy.ip}:${proxy.port}` |
11 | 9 | } |
12 | 10 |
|
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 | | - |
44 | 11 | var OKCupid = function(proxy = null){ |
45 | 12 | this.requester = new Requester(proxy) |
46 | 13 | this.proxy = proxy; |
@@ -109,36 +76,10 @@ OKCupid.prototype.getUserProfile = function(username, callback){ |
109 | 76 | var user_profile_url = URLS.user_profile.replace('{username}', username) |
110 | 77 | this.requester.getRequest(user_profile_url, callback) |
111 | 78 | } |
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 | | -} |
131 | 79 |
|
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) |
142 | 83 | } |
143 | 84 |
|
144 | 85 | OKCupid.prototype.getVisitors = function(callback){ |
|
0 commit comments