c# 4.0 - how to work with pagination using since_id and max_id in linqtotwitter v2.1? -
i had used linqtotwitter fetching tweets twitter.
can pull tweets 1 shot @ time if try pull tweets using sinceid , maxid returning empty , i'm executing range dates in query. below code i'm able 100 tweets.what i'm doing wrong
thanks
my code
var auth = new singleuserauthorizer { credentials = new inmemorycredentials { consumerkey = "xxxxx", consumersecret = "xxxxx", oauthtoken = "xxxxx", accesstoken = "xxxxxxx" } }; var twitterctx = new twittercontext(auth); var owntweets = new list<status>(); ulong sinceid = 0; ulong maxid = 0; int laststatuscount = 0; var datefrom = datetime.now.adddays(-20); bool flag = true; var statusresponse = new list<status>(); statusresponse = (from tweet in twitterctx.status tweet.type == statustype.user && tweet.screenname == screenname && tweet.count == 100 && (tweet.createdat >= datefrom && tweet.createdat <= datetime.now) select tweet).tolist(); if (statusresponse.count > 0) { maxid = statusresponse.min(status => ulong.parse(status.statusid)) - 1; owntweets.addrange(statusresponse); sinceid = convert.touint64(owntweets.last().statusid); } { int ratelimitstatus = twitterctx.ratelimitremaining; if (ratelimitstatus != 0) { statusresponse = (from tweet in twitterctx.status tweet.type == statustype.user && tweet.screenname == screenname && tweet.sinceid == sinceid && tweet.maxid == maxid && tweet.count == 100 && (tweet.createdat >= datefrom && tweet.createdat <= datetime.now) select tweet).tolist(); laststatuscount = statusresponse.count; if (laststatuscount != 0) { maxid = statusresponse.min(status => ulong.parse(status.statusid)) - 1; owntweets.addrange(statusresponse); } else { flag = false; } } else { flag = false; } } while (flag);
you can fix changing how initialize sinceid, represents oldest tweet retrieve. remember, you're working backwards through list, until reach sinceid.
- create field sinceid in data store, associated user represented screenname (or userid).
- initialize field 1. don't set 0, because tweet id #0 doesn't exist , twitter return error.
- in code, initialize sinceid value data store user, 1 first time through.
- remove other changes sinceid in code, including 1 inside if (statusresponse.count > 0) block.
- when algorithm complete, use line removed if block next sinceid should be.
- save value of sinceid in data store user can read again next time run algorithm user.
for more info, please visit blog post working timelines linq twitter.
Comments
Post a Comment