Last active
March 29, 2018 12:55
-
-
Save tonmcg/fee6f6512073b72111c5cf3deb2695fc to your computer and use it in GitHub Desktop.
M Language Twitter API Functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| Twitter.GetFriends = (screen_name as text, number as number, optional cursor as text) as any => | |
| let | |
| initial_cursor = if cursor is null then "-1" else cursor, | |
| count = if number is null then 5000 else number, | |
| max_requests = 15, // Twitter-provided max for app authorized calls | |
| number_requests = if count > 75000 then max_requests else Number.RoundUp(count / 15), // 75,000 is the maximum number of items that can be returned in a 15-minute period | |
| baseUri = "https://api.twitter.com", | |
| relativeUri = "1.1/friends/ids.json", | |
| fullUri = Uri.Combine(baseUri,relativeUri), | |
| GetOAuthToken = let | |
| Twitter.GetOAuthToken = (consumerKey as text, consumerSecret as text) as text => | |
| let | |
| // Concatenates the Consumer Key & Consumer Secret and converts to base64 | |
| authKey = "Basic " & Binary.ToText(Text.ToBinary(consumerKey & ":" & consumerSecret),0), | |
| url = "https://api.twitter.com/oauth2/token", | |
| // Uses the Twitter POST oauth2/token method to obtain a bearer token | |
| GetJson = Web.Contents( | |
| url, | |
| [ | |
| Headers = | |
| [ | |
| #"Authorization" = authKey, | |
| #"Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8" | |
| ], | |
| Content = Text.ToBinary("grant_type=client_credentials") | |
| ] | |
| ), | |
| FormatAsJson = Json.Document(GetJson), | |
| // Gets token from the Json response | |
| AccessToken = FormatAsJson[access_token], | |
| AccessTokenHeader = "bearer " & AccessToken | |
| in | |
| AccessTokenHeader | |
| in | |
| Twitter.GetOAuthToken, | |
| token = GetOAuthToken(consumer_key, consumer_secret), | |
| GetResponse = let | |
| responseCall = (query as record) => | |
| let | |
| call = Web.Contents( | |
| baseUri, | |
| [ | |
| Headers = | |
| [ | |
| #"Authorization" = token | |
| ], | |
| RelativePath = relativeUri, | |
| Query = query | |
| ] | |
| ) | |
| in | |
| call | |
| in | |
| responseCall, | |
| tableType = type table [id = text], | |
| friendRecords = Table.FromList( | |
| List.Generate( | |
| ()=> | |
| [ | |
| n = 0, | |
| query = [screen_name = screen_name,cursor = initial_cursor,stringify_ids = "true",count = Number.ToText(count)], | |
| response = Json.Document(GetResponse(query)), | |
| ids = response[ids], | |
| prev_cursor = initial_cursor, | |
| next_cursor = response[next_cursor_str] | |
| ], // initial | |
| each [prev_cursor] <> "0", // condition | |
| each | |
| [ | |
| n = [n] + 1, | |
| query = [screen_name = screen_name,cursor = [next_cursor],stringify_ids = "true",count = Number.ToText(count)], | |
| response = Json.Document(GetResponse(query)), | |
| ids = response[ids], | |
| prev_cursor = if n = max_requests then "0" else response[previous_cursor_str], | |
| next_cursor = response[next_cursor_str] | |
| ], // next | |
| each [[ids], [n], [prev_cursor], [next_cursor]] // selector | |
| ), | |
| Splitter.SplitByNothing(), {"Friends"}, null, ExtraValues.Error | |
| ), | |
| friendLists = Table.ExpandRecordColumn(friendRecords, "Friends", {"ids"}, {"id"}), | |
| friendsTable = Table.ExpandListColumn(friendLists, "id"), | |
| friendsFinal = Value.ReplaceType(friendsTable,tableType) | |
| in | |
| friendsFinal | |
| in | |
| Twitter.GetFriends |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment