我需要存储少数用户的关注者的关注者。推特followers/ids
给出了id
列表,而不是id_str
。我将 api 返回的整个 id 列表存储在一个列表中。然后对于每个id,我都会找到关注者列表。每当我使用大ID查找他们的关注者列表时,我都会收到错误Error: Sorry, that page does not exist
。我尝试将id转换为字符串,但错误仍然存在。我一定错过了什么。我正在使用 Twit 库。
这是我收集少数用户的关注者列表的脚本。
// store in mongoDB
var storeUserFollowerList = function ( screenName, followerList ) {
twitter_profile.update ({ screen_name : screenName }, {
follower_list : followerList
} , function ( err, doc ) {
if ( err ) {
console.log ( err );
console.log ( ' Error while updating ');
} else {
console.log ( 'Successfull updation ');
//console.log ( followerList );
}
});
}
var storeUserFollowerList = function ( screenName, followerList ) {
twitter_profile.update ({ screen_name : screenName }, {
follower_list : followerList
} , function ( err, doc ) {
if ( err ) {
console.log ( err );
console.log ( ' Error while updating ');
} else {
console.log ( 'Successfull updation ');
//console.log ( followerList );
}
});
}
var getUserFollowerList = function ( screenName, nextCursor, callback ) {
twits.get ( 'followers/ids', { screen_name : screenName,
cursor : nextCursor },
function ( err, data, response ) {
//console.log (data);
if ( err ) {
console.log ( err + ' for ' + screenName );
} else {
callback ( screenName, data['ids'] );
//console.log ( data['ids'] )
if( data["next_cursor_str"] != "0" ) {
getUserFollowerList ( screenName, data["next_cursor_str"], callback );
}
}
});
}
var getUsers = function ( callback ) {
screenNames.forEach ( function ( screenName ) {
/*
By default, an API endpoint that supports cursoring will assume
-1 was passed as cursor if you do not provide one
*/
callback ( screenName, "-1", storeUserFollowerList );
});
}
这是我如何获得这些关注者的关注者列表
var getFollowerListOfFollowers = function( followerList, i ) {
// if current follower is not in db then search for its follower list and update db
twitter_profile.find ( { id : followerList[i] }, function ( err, dbData ) {
if ( err ) {
console.log ( err );
} else {
if ( dbData.length > 0 ) {
console.log ( ' we have this user ... ignore ');
getFollowerListOfFollowers ( followerList, ++i );
} else {
get_follower_list ( ' ' + followerList[i], enter_in_db );
getFollowerListOfFollowers ( followerList, ++i );
}
}
});
}
var getFollowerOfFollowers = function () {
screenNames.forEach ( function ( screenName ) {
twitter_profile.find ( { screen_name : screenName },
function ( err, dbData ) {
if ( err ) {
console.log ( err );
} else {
if ( dbData.length > 0 ) {
// get follower list for each follower of this user
getFollowerListOfFollowers ( dbData[0]['follower_list'], 0 );
//console.log ( dbData[0]['follower_list'] );
} else {
console.log ( ' Data not found in DB ');
}
}
});
});
}
最后
var enterInDB = function ( id, followerList ) {
new twitter_profile ( {
_id: id,
id: id,
follower_list: followerList
}).save (function ( err, doc ) {
if ( err ) {
console.log ( err );
} else {
console.log ( ' -----------Entered Profile into database------------- ');
}
});
}
var getFollowerList = function ( id, callback ) {
twits.get ( 'followers/ids', { id : id }, function ( err, data, response ) {
//console.log (data);
if ( err ) {
console.log ( err + ' for ' + id );
} else {
callback ( id, ""+data['ids'] );
//console.log ( data['ids'] )
}
});
}
获取 id 的错误,例如 730292604265435100
、 702941284181237800
抱歉回答晚了。我的一天有点忙。
我尝试通过Twitter API控制台查找这些ID,并得到了与您提到的相同的结果。
在为这些 id 尝试了各种 api 方法(每个人都失败了)之后,我决定在 Twitter 本身上查找个人资料。
这也失败了。所以我寻找了一个随机的个人资料,关注者很少,但在这个 id 范围内有一个关注者。
我找到了一个!他有一个ID为728920217020158000
的追随者。如果您通过 api 查找此 id,您将看到没有结果,因为 id 无效。
我注意到id_str字段显示了另一个 ID。如果您通过 api 728920217020157955
查找 ID,您将找到他的个人资料。
所以你必须告诉twit
把字符串化的id还给你。
我现在没有安装twit
,所以你可以这样尝试
twits.get ( 'followers/ids', { id : id, stringify_ids : true }
//console.log (data);
if ( err ) {
console.log ( err + ' for ' + id );
} else {
callback ( id, ""+data['ids'] );
//console.log ( data['ids'] )
}
});
有关此终结点的更多信息,请访问 Twitter API 参考