-
-
Save abourget/5603331 to your computer and use it in GitHub Desktop.
Updated for M4
This file contains 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
import "dart:io"; | |
import "dart:utf"; | |
import 'dart:json' as JSON; | |
import 'dart:async'; | |
import 'dart:isolate' as isolate; | |
import 'dart:mirrors'; | |
String API_KEY = "INSERT_KEY_HERE"; | |
class Bot { | |
Socket _socket; | |
String botName = "abourget_dartBot"; | |
String botChannel = "#montrealpython"; | |
String ircServer = "irc.freenode.net"; | |
String superUser = ':[email protected]'; | |
Bot() { | |
Socket.connect(ircServer, 6667).then((socket) { | |
_socket = socket; | |
// Listen for incoming events now | |
socket.listen((event) { | |
String data = decodeUtf8(event, 0, event.length); | |
print(">>> ${data}"); | |
List<String> server_response = data.split(' '); | |
String command_reply = server_response[0]; | |
switch (command_reply) { | |
case 'PING': | |
ircPong(server_response[1]); | |
break; | |
case superUser: | |
if (server_response[1] == "PRIVMSG") { | |
if (server_response[3] == ":printnews\r\n") { | |
isolate.spawnFunction(isolated_dart_news_stream).call("").then((Map m) { | |
m.forEach((k, v) { | |
v = v.replaceAll('\r\n', "").replaceAll('\r', "").replaceAll("\n", ""); | |
var send_to_channel = "PRIVMSG ${botChannel} :${v} \r\n"; | |
socket.write(send_to_channel); | |
}); | |
}); | |
} | |
} | |
break; | |
} | |
}); | |
// Bootstrapping connection | |
send("NICK $botName"); | |
send("USER $botName $ircServer bla :$botName"); | |
send("JOIN ${botChannel}"); | |
}); | |
} | |
send(String data) { | |
if (_socket == null) { | |
print("ERROR: Can't send: $data"); | |
return; | |
} | |
print("<<< $data"); | |
_socket.write(data); | |
_socket.write("\r\n"); | |
} | |
ircPong(String s) { | |
send("PONG ${s.trim()}"); | |
} | |
} | |
void isolated_dart_news_stream() { | |
isolate.port.receive((msg, reply) { | |
dart_news_stream().then((m) { | |
reply.send(m); | |
// print(m); /// send news | |
isolate.port.close(); | |
}); | |
}); | |
} | |
void isolate_goo_gl_shortner() { | |
isolate.port.receive((msg, reply) { | |
goo_gl_shortner(msg).then((m) { | |
reply.send(m); | |
// print(m); | |
isolate.port.close(); | |
}); | |
}); | |
} | |
void isolate_goog_gl_shortner_proc() { | |
isolate.port.receive((msg, reply) { | |
var dart_news = {}; | |
var args = ["-X", "POST","https://www.googleapis.com/urlshortener/v1/url", | |
"-H","Content-Type: application/json", "-d",'{"longUrl": "${msg["url"]}"}']; | |
Process.run("curl", args).then((ProcessResult proc_res) { | |
if (proc_res.exitCode == 0) { | |
Map short_url_map = JSON.parse(proc_res.stdout); | |
dart_news[short_url_map["id"]] = "g+/#dartlang: ${msg["title"].trim()} ${short_url_map["id"]}"; | |
} | |
reply.send(dart_news); | |
isolate.port.close(); | |
}); | |
}); | |
} | |
Future goo_gl_shortner(dart_items) { | |
Completer completer = new Completer(); | |
Map dart_news = {}; | |
List<Future> shortner_procs = new List<Future>(); | |
if (dart_items != null) { | |
dart_items.forEach((item) => shortner_procs.add(isolate.spawnFunction(isolate_goog_gl_shortner_proc).call(item))); | |
} else { | |
print("No news!"); | |
} | |
Future.wait(shortner_procs).then((List<Map> values) { | |
values.forEach((Map m) { | |
m.keys.forEach((k) { | |
dart_news[k] = m[k]; | |
}); | |
}); | |
completer.complete(dart_news); | |
}); | |
return completer.future; | |
} | |
Future dart_news_stream() { | |
Completer complete = new Completer(); | |
// Since we do not have https in HttpClient, going to run with curl for now. | |
String activity_query = "%23dartlang"; | |
String gplus_url = "https://www.googleapis.com/plus/v1/activities?query=${activity_query}&key=${API_KEY}&maxResults=3"; | |
Map dart_news = {}; | |
Process.run("curl", [gplus_url]).then((ProcessResult proc_res) { | |
if (proc_res.exitCode == 0) { | |
Map dart_gplus_activities = JSON.parse(proc_res.stdout); | |
List<Map> dart_items = dart_gplus_activities["items"]; | |
isolate.spawnFunction(isolate_goo_gl_shortner).call(dart_items).then((Map m) => complete.complete(m)); | |
} | |
}); | |
return complete.future; | |
} | |
void main() { | |
Bot b = new Bot(); | |
} |
Thanks for the starting point here! I've churned this up into a little bot project of my own, and it's updated for Dart 1.1.3!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You must replace the API key, by getting one here:
https://code.google.com/apis/console