├── README.md ├── pack.pl └── prolog └── twitter.pl /README.md: -------------------------------------------------------------------------------- 1 | This is a pack to make application searches of twitter. 2 | 3 | You need to go to 4 | https://apps.twitter.com/ and make an app with a consumer key and consumer secret. 5 | 6 | 7 | 8 | To use : 9 | 10 | use_module(library(twitter)). 11 | get_bearer_token('yourkey','yoursecret',Json,Token,Error). 12 | 13 | %This this asserts token/1 14 | 15 | Then you can make a search. e.g. searching for tweets with the 16 | word 'walrus' is done as so: 17 | 18 | token(T),make_a_search('walrus',T,Json,ErrorCode). 19 | 20 | Json is a dict with the response from twitter. 21 | -------------------------------------------------------------------------------- /pack.pl: -------------------------------------------------------------------------------- 1 | name('twitter'). 2 | title('A pack to access the twitter api'). 3 | version('0.10'). 4 | author('Sam Neaves', 'sam.neaves@gmail.com'). 5 | 6 | 7 | -------------------------------------------------------------------------------- /prolog/twitter.pl: -------------------------------------------------------------------------------- 1 | :- module(twitter, 2 | [token/1, 3 | get_bearer_token/5, 4 | make_a_search/4, 5 | get_friends_list/4, 6 | get_user/4, 7 | get_user_following/4, 8 | get_tweet/4]). 9 | 10 | :- use_module(library(http/thread_httpd)). 11 | :- use_module(library(http/http_dispatch)). 12 | :- use_module(library(http/http_error)). 13 | :- use_module(library(http/html_write)). 14 | :- use_module(library(http/http_session)). 15 | :- use_module(library(http/js_write)). 16 | :- use_module(library(http/http_files)). 17 | :- use_module(library(http/json)). 18 | :- use_module(library(http/http_open)). 19 | :- use_module(library(http/http_json)). 20 | :- use_module(library(http/http_parameters)). 21 | :- use_module(library(http/http_client)). 22 | :- use_module(library(http/http_ssl_plugin)). 23 | 24 | :- dynamic 25 | token/1. 26 | 27 | bearer_token_credentials(Key,Secret,B_Token):- 28 | format(atom(B_Token),"~w:~w",[Key,Secret]). 29 | 30 | get_bearer_token(Key,Secret,JSON,Token,ErrorCode):- 31 | bearer_token_credentials(Key,Secret,B_Token), 32 | base64(B_Token,B_Token64), 33 | format(atom(My_Auth),"Basic ~w",[B_Token64]), 34 | ListofData =[grant_type=client_credentials], 35 | http_open('https://api.twitter.com/oauth2/token', In, 36 | [ request_header(authorization=My_Auth),status_code(ErrorCode), 37 | method(post),post(form(ListofData)) 38 | ]), 39 | call_cleanup(json_read_dict(In, JSON), 40 | close(In)), 41 | Token = JSON.access_token, 42 | assertz(token(Token)). 43 | 44 | 45 | make_a_search(My_Search,B_Token64,JSON,ErrorCode):- 46 | Path='/1.1/search/tweets.json', 47 | Search=[q(My_Search)], 48 | get_json(Path, Search, B_Token64, JSON, ErrorCode). 49 | 50 | get_friends_list(Username, B_Token64, JSON, ErrorCode) :- 51 | % DEPRECATED: Twitter API v1.1 https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-friends-list 52 | get_friends_list_at_cursor(Username, B_Token64, -1, JSON, ErrorCode). 53 | 54 | get_friends_list_at_cursor(Username, B_Token64, Cursor, JSON, ErrorCode) :- 55 | Cursor=\=0, 56 | Path='/1.1/friends/list.json', 57 | Search=[screen_name=Username, count=200, cursor=Cursor], 58 | get_json(Path, Search, B_Token64, Json0, ErrorCode0), 59 | ( % succeed for current 60 | JSON = Json0, 61 | ErrorCode = ErrorCode0 62 | ; % succeed for next 63 | _{next_cursor:NextCursor}: