Skip to content

Instantly share code, notes, and snippets.

@Plutor
Last active December 6, 2019 08:57
Show Gist options
  • Select an option

  • Save Plutor/4a7bb314ddc59b5f343f to your computer and use it in GitHub Desktop.

Select an option

Save Plutor/4a7bb314ddc59b5f343f to your computer and use it in GitHub Desktop.

Revisions

  1. Plutor revised this gist Aug 26, 2015. 1 changed file with 130 additions and 49 deletions.
    179 changes: 130 additions & 49 deletions UberBut.js
    Original file line number Diff line number Diff line change
    @@ -1,49 +1,72 @@
    // This is the script that runs http://twitter.com/UberBut

    // Get a Wordnik API key: http://developer.wordnik.com/
    var WORDNIK_API_KEY = "XXXX";
    // Get a Twitter app key: https://apps.twitter.com/
    var TWITTER_CONSUMER_KEY = "XXXX";
    var TWITTER_CONSUMER_SECRET = "XXXX";
    // Get access tokens using twurl: https://github.com/twitter/twurl
    var TWITTER_ACCESS_TOKEN = "XXXX";
    var TWITTER_ACCESS_SECRET = "XXXX";

    function Start() {
    var props = PropertiesService.getScriptProperties();

    props.setProperties({
    TWITTER_CONSUMER_KEY: TWITTER_CONSUMER_KEY,
    TWITTER_CONSUMER_SECRET: TWITTER_CONSUMER_SECRET,
    TWITTER_ACCESS_TOKEN: TWITTER_ACCESS_TOKEN,
    TWITTER_ACCESS_SECRET: TWITTER_ACCESS_SECRET,
    });

    // Delete exiting triggers, if any
    var triggers = ScriptApp.getProjectTriggers();

    for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
    }

    // Setup trigger to post new Tweets every hour.
    // Setup trigger to post a Tweet every hour.
    ScriptApp.newTrigger("UberBut")
    .timeBased()
    .everyHours(1)
    .create();
    }

    function GetTwitter() {
    var twit = new Twitter.OAuth(PropertiesService.getScriptProperties());
    twit.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
    .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
    .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
    .setAccessToken(TWITTER_ACCESS_TOKEN)
    .setAccessTokenSecret(TWITTER_ACCESS_SECRET);
    // From https://github.com/googlesamples/apps-script-oauth1
    function GetTwitterService() {
    // Create a new service with the given name. The name will be used when
    // persisting the authorized token, so ensure it is unique within the
    // scope of the property store.
    return OAuth1.createService('twitter')
    // Set the endpoint URLs.
    .setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
    .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
    .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
    // Set the consumer key and secret.
    .setConsumerKey(TWITTER_CONSUMER_KEY)
    .setConsumerSecret(TWITTER_CONSUMER_SECRET)
    // Set the name of the callback function in the script referenced
    // above that should be invoked to complete the OAuth flow.
    .setCallbackFunction('AuthCallback')
    // Set the property store where authorized tokens should be persisted.
    .setPropertyStore(PropertiesService.getScriptProperties());
    }

    function TwitterFetch(url) {
    var twit = GetTwitterService();
    if (!twit.hasAccess()) {
    var authorization_url = twitterService.authorize();
    Logger.log("No Twitter access -- go to " + authorization_url + " to authorize");
    return;
    }
    return twit.fetch(url);
    }
    function TwitterPost(url, payload) {
    var twit = GetTwitterService();
    if (!twit.hasAccess()) {
    twit.runAuthorizeFlow();
    var authorization_url = twitterService.authorize();
    Logger.log("No Twitter access -- go to " + authorization_url + " to authorize");
    return;
    }
    return twit.fetch(url, {
    "method" : "post",
    "payload" : payload
    });
    }

    function AuthCallback(request) {
    var twit = GetTwitterService();
    if (twit.handleCallback(request)) {
    Logger.log('Success!');
    } else {
    Logger.log('Denied.');
    }
    return twit;
    }

    // Makes an HTTP request to the Wordnik API for a random word, given the part of speech
    @@ -56,46 +79,104 @@ function RandomWord(part_of_speech){
    "maxDictionaryCount=-1&minLength=4&maxLength=-1&api_key=" +
    WORDNIK_API_KEY);
    var word = JSON.parse(content)["word"];
    Logger.log("Got random word: " + word);
    //Logger.log("Got random word: " + word);
    return word;
    }

    function WordIsPartOfSpeech(word, part_of_speech){
    var content = UrlFetchApp.fetch(
    "http://api.wordnik.com/v4/word.json/" + word + "/definitions?limit=1&partOfSpeech=" + part_of_speech +
    "&api_key=" + WORDNIK_API_KEY);
    var defs = JSON.parse(content);
    return defs.length;
    }

    // For pluralizing: https://code.google.com/p/inflection-js/
    function Pluralize(noun) {
    return noun.pluralize();
    }

    // From http://airhadoken.github.io/2015/06/29/twitter-lib-explained.html
    function authCallback(request) {
    var service = new Twitter.OAuth(PropertiesService.getScriptProperties());
    service.handleCallback(request);
    function StandaloneTweet() {
    var text = "";
    while (text == "") {
    var noun = RandomWord("noun");
    text = Pluralize(noun);
    if (text == noun) {
    text = "";
    }
    }

    // An adjective 50% of the time
    if (Math.random() < 0.5) {
    var adjective = RandomWord("adjective");
    if (adjective != "") {
    text = adjective + " " + text;
    }
    }

    return "Like Uber, but for " + text + ".";
    }

    function UberBut() {
    try {
    var text = "";
    while (text == "") {
    var noun = RandomWord("noun");
    text = Pluralize(noun);
    if (text == noun) {
    text = "";
    }
    }
    function ReplyTweet() {
    var props = PropertiesService.getScriptProperties();
    var max_id = props.getProperty("MAX_TWITTER_ID");
    if (max_id && max_id > 0) {
    max_id = "&since_id=" + max_id;
    } else {
    max_id = "";
    }

    // An adjective 20% of the time
    if (Math.random() < 0.2) {
    var adjective = RandomWord("adjective");
    if (adjective != "") {
    text = adjective + " " + text;
    var url = "https://api.twitter.com/1.1/search/tweets.json?q=" + encodeURIComponent("-from:uberbut @uberbut") + "&count=50" + max_id;
    Logger.log(url);
    response = TwitterFetch(url);
    if (response.getResponseCode() !== 200) {
    Logger.log("Got response code: " + response.getResponseCode());
    return StandaloneTweet();
    }
    data = JSON.parse(response.getContentText());
    if (!data) {
    Logger.log("Can't parse json from '" + data + "'");
    return StandaloneTweet();
    }

    var tweets = data.statuses;
    for (var t in tweets.reverse()) {
    tweet = tweets[t]
    props.setProperty("MAX_TWITTER_ID", tweet.id_str);
    if (!tweet.possibly_sensitive && !tweet.retweeted_status) {
    // For each word, in reverse order, try to find a noun.
    var words = tweet.text.split(" ").reverse().filter(function(w) {
    // If the word has any non-alphabetic characters or is less than 6 letters long, skip it.
    return (w.length >= 6 && w.match('^[a-zA-Z]*$'));
    });
    for (var w = 0; w < words.length; w++) {
    word = words[w];
    if (WordIsPartOfSpeech(word, "noun")) {
    var choice = Pluralize(word);
    // Check to see if the previous word is an adjective.
    if ((w < words.length - 1) && WordIsPartOfSpeech(words[w+1], "adjective")) {
    choice = words[w+1] + " " + choice;
    }
    // Done!
    var url = 'https://twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id_str;
    var text = "Like Uber, but for " + choice + ".\n" + url;
    Logger.log(text);
    return text;
    }
    }
    }

    text = "Like Uber, but for " + text + ".";
    }

    return StandaloneTweet();
    }

    function UberBut() {
    try {
    var text = ReplyTweet();
    Logger.log("Tweeting: '" + text + "'");

    // Post to twitter:
    var twit = GetTwitter();
    twit.sendTweet(text);
    TwitterPost("https://api.twitter.com/1.1/statuses/update.json", {'status': text})
    } catch (f) {
    Logger.log("Error: " + f);
    }
  2. Plutor revised this gist Aug 25, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions UberBut.js
    Original file line number Diff line number Diff line change
    @@ -26,10 +26,10 @@ function Start() {
    ScriptApp.deleteTrigger(triggers[i]);
    }

    // Setup trigger to read Tweets every five minutes
    // Setup trigger to post new Tweets every hour.
    ScriptApp.newTrigger("UberBut")
    .timeBased()
    .everyHours(4)
    .everyHours(1)
    .create();
    }

  3. Plutor created this gist Aug 25, 2015.
    102 changes: 102 additions & 0 deletions UberBut.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,102 @@
    // This is the script that runs http://twitter.com/UberBut

    // Get a Wordnik API key: http://developer.wordnik.com/
    var WORDNIK_API_KEY = "XXXX";
    // Get a Twitter app key: https://apps.twitter.com/
    var TWITTER_CONSUMER_KEY = "XXXX";
    var TWITTER_CONSUMER_SECRET = "XXXX";
    // Get access tokens using twurl: https://github.com/twitter/twurl
    var TWITTER_ACCESS_TOKEN = "XXXX";
    var TWITTER_ACCESS_SECRET = "XXXX";

    function Start() {
    var props = PropertiesService.getScriptProperties();

    props.setProperties({
    TWITTER_CONSUMER_KEY: TWITTER_CONSUMER_KEY,
    TWITTER_CONSUMER_SECRET: TWITTER_CONSUMER_SECRET,
    TWITTER_ACCESS_TOKEN: TWITTER_ACCESS_TOKEN,
    TWITTER_ACCESS_SECRET: TWITTER_ACCESS_SECRET,
    });

    // Delete exiting triggers, if any
    var triggers = ScriptApp.getProjectTriggers();

    for (var i = 0; i < triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
    }

    // Setup trigger to read Tweets every five minutes
    ScriptApp.newTrigger("UberBut")
    .timeBased()
    .everyHours(4)
    .create();
    }

    function GetTwitter() {
    var twit = new Twitter.OAuth(PropertiesService.getScriptProperties());
    twit.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
    .setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
    .setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
    .setAccessToken(TWITTER_ACCESS_TOKEN)
    .setAccessTokenSecret(TWITTER_ACCESS_SECRET);
    if (!twit.hasAccess()) {
    twit.runAuthorizeFlow();
    }
    return twit;
    }

    // Makes an HTTP request to the Wordnik API for a random word, given the part of speech
    // Modified from: https://gist.github.com/scazon/a427f58974b6e7c80827
    function RandomWord(part_of_speech){
    var content = UrlFetchApp.fetch(
    "http://api.wordnik.com/v4/words.json/randomWord?includePartOfSpeech=" +
    part_of_speech +
    "&minCorpusCount=8000&maxCorpusCount=-1&minDictionaryCount=8&" +
    "maxDictionaryCount=-1&minLength=4&maxLength=-1&api_key=" +
    WORDNIK_API_KEY);
    var word = JSON.parse(content)["word"];
    Logger.log("Got random word: " + word);
    return word;
    }

    // For pluralizing: https://code.google.com/p/inflection-js/
    function Pluralize(noun) {
    return noun.pluralize();
    }

    // From http://airhadoken.github.io/2015/06/29/twitter-lib-explained.html
    function authCallback(request) {
    var service = new Twitter.OAuth(PropertiesService.getScriptProperties());
    service.handleCallback(request);
    }

    function UberBut() {
    try {
    var text = "";
    while (text == "") {
    var noun = RandomWord("noun");
    text = Pluralize(noun);
    if (text == noun) {
    text = "";
    }
    }

    // An adjective 20% of the time
    if (Math.random() < 0.2) {
    var adjective = RandomWord("adjective");
    if (adjective != "") {
    text = adjective + " " + text;
    }
    }

    text = "Like Uber, but for " + text + ".";
    Logger.log("Tweeting: '" + text + "'");

    // Post to twitter:
    var twit = GetTwitter();
    twit.sendTweet(text);
    } catch (f) {
    Logger.log("Error: " + f);
    }
    }