Drafts Action Directory

Twitter: Get Likes

Posted by agiletortoise, Last update 3 months ago

Prompt for a Twitter handle, the request a list of their recent likes from the Twitter API. Build a Markdown version of those tweets, put them in a new draft and open it in the editor.

Demonstrates use of the Twitter object request method to make arbitrary requests to the Twitter API.

Install

Steps

  • script

    // set default twitter screen name for prompt
    var screenName = "agiletortoise";
    
    // prompt to screen to query for recent favs/likes
    var p = Prompt.create();
    p.title = "Get Twitter Likes"
    p.message = "Enter twitter handle for the account who's likes you wish to download";
    
    p.addTextField("user", "User", screenName);
    p.addButton("Get Likes");
    
    if (p.show()) {
    	screenName = p.fieldValues["user"];
    }
    else {
    	context.cancel();
    }
  • script

    // create a Twitter object
    var twitter = Twitter.create();
    
    // construct API endpoint to use.
    // information on API at http://developer.twitter.com
    var baseURL = "https://api.twitter.com/1.1/";
    var url = baseURL + "favorites/list.json";
    
    // make request to Twitter API
    var response = twitter.request({
    	"url": url,
    	"method": "GET",
    	"parameters": {
    		"screen_name": screenName
    	}
    });
    
    // handle the response
    if (response.statusCode == 200) {
    	var content = "";
    
    	// favorites return array of tweet objects
    	// loop over them and build a Markdown version
    	for (var fav of response.responseData) {
    		var id = fav.id_str;
    		var sn = fav.user.screen_name;
    		var tweetURL = `http://twitter.com/${sn}/status/${id}`;
    		var text = fav.text;
    	
    		content += `${text}\n*[link](${tweetURL})*\n\n---\n\n`
    	}
    	// create a new draft and open it
    	var d = Draft.create();
    	d.content = content;
    	d.update();
    	editor.load(d);
    }
    else {
    	context.fail();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.