Skip to content

Commit

Permalink
add getUserHashID
Browse files Browse the repository at this point in the history
  • Loading branch information
starkwang committed Mar 15, 2016
1 parent 8f4abf1 commit af3777d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
28 changes: 28 additions & 0 deletions getUserHashID.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var request = require('request');
var Promise = require('bluebird');

function getUserHashID(userPageUrl) {
return new Promise(function(resolve, reject) {
request({
method: 'GET',
url: userPageUrl,
headers: {
'cookie': '_za=c392e6c0-5bdb-4b01-a06d-84ffb9836a61; _ga=GA1.2.378714859.1433690880; _xsrf=44f011b01f29816fc257fae1770a9ece; q_c1=fb660ee5c15b4c97ae2b4b075373e5b6|1457189679000|1433392648000; udid="AIAAQIMSlAmPTohRXib_bmZtsg_JbPW-tC8=|1457502059"; cap_id="MGE2NDZmOTY3MDY1NDdlZmJiNDk4NjBmOGY2ZjhiMTY=|1457847388|27a0720e2f3c9580f52f982b99a7a34d4d902bee"; z_c0="QUFBQTFuTWRBQUFYQUFBQVlRSlZUWHFKREZjQXItUVVZRkRhV0k0TEpUV182SUp5TlA1UUtBPT0=|1457847418|d5d52a10b95375f90c3c34c1393314cf323aca84"; n_c=1; __utmt=1; __utma=51854390.378714859.1433690880.1457863920.1457863920.1; __utmb=51854390.12.9.1457864042502; __utmc=51854390; __utmz=51854390.1457863920.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmv=51854390.100-1|2=registration_date=20130824=1^3=entry_date=20130824=1',
}
}, function(err, res, body) {
if (err) {
reject(err);
} else {
resolve(parseHashID(body));
}
})
});
}

function parseHashID(html) {
var reg = /data-name=\"current_people\">\[.*\"(\S*)\"\]<\/script>/g;
reg.exec(html);
return RegExp.$1;
}

module.exports = getUserHashID;
33 changes: 26 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
var Spider = require('./Spider');
Spider().request({
followees: true,
hash_id: 'd965f32a168564f9e58ad3a48a1585a4'
}).then(function(result) {
console.log("success!!!");
console.log(result);
})
var getUserHashID = require('./getUserHashID');
var Promise = require('bluebird');
getUserHashID('https://www.zhihu.com/people/avit4799')
.then(function(hashID) {
var works = [Spider().request({
followees: true,
hash_id: hashID
}), Spider().request({
hash_id: hashID
})];
return Promise.all(works);
})
.then(function(result) {
console.log("fetch success!!!");
var followees = result[0].data;
var followers = result[1].data;
var friends = [];
followers.forEach(function(follower, index) {
followees.forEach(function(followee, index) {
if (follower.hash_id === followee.hash_id) {
friends.push(follower);
}
});
});
console.log(friends);
});

0 comments on commit af3777d

Please sign in to comment.