DataFlex
DataFlex
Twitter - Get Follower IDs
Demonstrates how to get a list of follower IDs for a Twitter user. If the number of followers is more than can be returned in a single response, this example will use cursors to page through the followers.This example is deprecated and no longer valid.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoJson
Handle hoRest
Variant vOauth1
Handle hoOauth1
Boolean iBAutoReconnect
Handle hoJsonResponse
Handle hoSbNextCursor
Integer iPageNum
Boolean iCaseSensitive
Boolean iBContinue
String sResp
Integer iNumIds
Integer i
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// ----------------------------------------------------------------------
// This initial setup, which involves setting the OAuth1 properties and connecting
// to api.twitter.com, is only required once at the beginning. Once connected, the same
// object instance may be re-used, and if necessary, it will automatically reconnect
// as needed.
// Assume we've previously obtained an access token and saved it to a JSON file..
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Get ComLoadFile Of hoJson "qa_data/tokens/twitter.json" To iSuccess
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Get Create (RefClass(cComChilkatOAuth1)) To hoOauth1
If (Not(IsComObjectCreated(hoOauth1))) Begin
Send CreateComObject of hoOauth1
End
Set ComConsumerKey Of hoOauth1 To "TWITTER_CONSUMER_KEY"
Set ComConsumerSecret Of hoOauth1 To "TWITTER_CONSUMER_SECRET"
Get ComStringOf Of hoJson "oauth_token" To sTemp1
Set ComToken Of hoOauth1 To sTemp1
Get ComStringOf Of hoJson "oauth_token_secret" To sTemp1
Set ComTokenSecret Of hoOauth1 To sTemp1
Set ComSignatureMethod Of hoOauth1 To "HMAC-SHA1"
Get ComGenNonce Of hoOauth1 16 To iSuccess
Get pvComObject of hoOauth1 to vOauth1
Get ComSetAuthOAuth1 Of hoRest vOauth1 False To iSuccess
Move True To iBAutoReconnect
Get ComConnect Of hoRest "api.twitter.com" 443 True iBAutoReconnect To iSuccess
If (iSuccess <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
// This ends the initial setup...
// ----------------------------------------------------------------------
// This Twitter user has about 77.5K followers..
Get ComAddQueryParam Of hoRest "screen_name" "MarcusMiller959" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoJsonResponse
If (Not(IsComObjectCreated(hoJsonResponse))) Begin
Send CreateComObject of hoJsonResponse
End
Set ComEmitCompact Of hoJsonResponse To False
// Get the 1st page of results using a cursor of "-1".
Get Create (RefClass(cComChilkatStringBuilder)) To hoSbNextCursor
If (Not(IsComObjectCreated(hoSbNextCursor))) Begin
Send CreateComObject of hoSbNextCursor
End
Get ComSetString Of hoSbNextCursor "-1" To iSuccess
Move 1 To iPageNum
Move False To iCaseSensitive
Move True To iBContinue
// Get a maximum of 5 pages (of 5000 ids each).
While (iBContinue = True)
Get ComContentsEqual Of hoSbNextCursor "0" iCaseSensitive To bTemp1
If (bTemp1 = True) Begin
// This will cause the loop to exit..
Move False To iBContinue
End
Else Begin
// Adds or replaces the query param.
Get ComGetAsString Of hoSbNextCursor To sTemp1
Get ComAddQueryParam Of hoRest "cursor" sTemp1 To iSuccess
// Get the next page of follower IDs
Get ComFullRequestNoBody Of hoRest "GET" "/1.1/followers/ids.json" To sResp
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Get ComLastErrorText Of hoRest To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComLoad Of hoJsonResponse sResp To iSuccess
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComEmit Of hoJsonResponse To sTemp1
Showln sTemp1
Procedure_Return
End
// Show the number of IDs returned in this
Get ComSizeOfArray Of hoJsonResponse "ids" To iNumIds
Showln "Page " iPageNum ", Number of Ids: " iNumIds
// Show the 1st 10 ids in this page.
Move 0 To i
While (i < iNumIds)
Set ComI Of hoJsonResponse To i
Get ComStringOf Of hoJsonResponse "ids[i]" To sTemp1
Showln " " i ": " sTemp1
Move (i + 1) To i
If (i > 10) Begin
// Force the loop to exit.
Move iNumIds To i
End
Loop
Move (iPageNum + 1) To iPageNum
If (iPageNum > 5) Begin
Move False To iBContinue
End
Else Begin
Get ComStringOf Of hoJsonResponse "next_cursor_str" To sTemp1
Get ComSetString Of hoSbNextCursor sTemp1 To iSuccess
End
End
Loop
// A successful JSON response for the 1st page looks like this:
// {
// "ids": [
// 3140496044,
// 793204773751324672,
// 789951187781050369,
// 763520773587922945,
// ...
// 15031286,
// 2668251246,
// 3751659443
// ],
// "next_cursor": 1531680438812851153,
// "next_cursor_str": "1531680438812851153",
// "previous_cursor": 0,
// "previous_cursor_str": "0"
// }
// The output of this program looks like this:
// Page 1, Number of Ids: 5000
// 0: 931953350
// 1: 786708055
// 2: 560845700
// 3: 3140496044
// 4: 793204773751324672
// 5: 789951187781050369
// 6: 763520773587922945
// 7: 793143274059988992
// 8: 793139683412762624
// 9: 1588222783
// 10: 703821370778451968
// Page 2, Number of Ids: 5000
// 0: 15031286
// 1: 2668251246
// 2: 3751659443
// 3: 3324584493
// 4: 2440214809
// 5: 1322335441
// 6: 4439178393
// 7: 4911573711
// 8: 720792880080560128
// 9: 720805087124267008
// 10: 172926330
// Page 3, Number of Ids: 5000
// 0: 93867176
// 1: 2992946183
// 2: 2825296077
// 3: 3784572861
// 4: 2150051321
// 5: 2460881603
// 6: 4128849341
// 7: 2234697931
// 8: 2379418164
// 9: 3425171542
// 10: 325759186
// Page 4, Number of Ids: 5000
// 0: 22793412
// 1: 3347750489
// 2: 316923043
// 3: 2481719196
// 4: 3363591905
// 5: 3238116492
// 6: 58467130
// 7: 3015182362
// 8: 2985342719
// 9: 3095965720
// 10: 17505957
// Page 5, Number of Ids: 5000
// 0: 2541434684
// 1: 140022957
// 2: 134845054
// 3: 772810508
// 4: 16979294
// 5: 2320540225
// 6: 105439442
// 7: 2796744529
// 8: 251128801
// 9: 350229758
// 10: 2683994716
//
End_Procedure