Last active
December 6, 2019 08:57
-
-
Save Plutor/4a7bb314ddc59b5f343f to your computer and use it in GitHub Desktop.
Script that runs @UberBut
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
| // 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. | |
| 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); | |
| 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment