Skip to content

Instantly share code, notes, and snippets.

@Plutor
Last active March 9, 2016 21:19
Show Gist options
  • Save Plutor/2108648f09d18d008a21 to your computer and use it in GitHub Desktop.
Save Plutor/2108648f09d18d008a21 to your computer and use it in GitHub Desktop.
// Get a Wordnik API key: http://developer.wordnik.com/
var WORDNIK_API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
// Get a Twitter app key: https://apps.twitter.com/
var TWITTER_CONSUMER_KEY = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
var TWITTER_CONSUMER_SECRET = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ";
// Email to send errors to
var ERROR_EMAIL_ADDR = "[email protected]"
function Start() {
// 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 a Tweet every hour.
ScriptApp.newTrigger("RegisteredAf")
.timeBased()
.everyMinutes(10)
.create();
}
// 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 = twit.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()) {
var authorization_url = twit.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.');
}
}
// 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;
}
function RandomDomain() {
var adj = RandomWord("adjective");
if (adj == "") {
Logger.log("Couldn't get a random adjective.");
return "";
}
return adj;
}
// Looks up the domain on whois.nic.af and returns either:
// "is available" with a link or "was registered on DATE"
// (or empty string if there's an error).
function LookupDomain(name) {
var tries_left = 3;
var content = "";
var errors = "";
while (tries_left-- > 0 && content == "") {
try {
content = UrlFetchApp.fetch("https://www.gandi.net/whois/details?search=" + name + ".af");
} catch (e) {
errors += "Error getting registration info: " + e + "\n";
}
}
if (content == "") {
MailApp.sendEmail(ERROR_EMAIL_ADDR, "RegisteredAf got no content from whois.nic.af", errors);
return "";
}
var matches = /Creation Date: ([^T]*)/.exec(content);
if (!matches) {
var link = "https://www.gandi.net/domain/buy/result?domain_list=" + name + "&tld=af";
return "is available\n\n" + link;
}
return "was registered on " + matches[1];
}
function RegisteredAf() {
try {
var name = RandomDomain();
if (Math.random() > 0.1) {
Logger.log("Not tweeting " + name + ".af");
return;
}
var lookup = LookupDomain(name);
var text = name + ".af";
if (lookup != "") {
text = text + " " + lookup;
}
Logger.log("Tweeting: '" + text + "'");
// Post to twitter:
if (text != "") {
TwitterPost("https://api.twitter.com/1.1/statuses/update.json", {'status': 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