Skip to content

Instantly share code, notes, and snippets.

@remixz
Last active August 29, 2015 14:03
Show Gist options
  • Save remixz/21f4ae7bcb8080fa410c to your computer and use it in GitHub Desktop.
Save remixz/21f4ae7bcb8080fa410c to your computer and use it in GitHub Desktop.
YO CAM

instructions

  1. deploy server.js somewhere (i just used heroku)
  2. create an API yo account on http://developer.justyo.co/, set the callback URL to http://<server url>/yo
  3. run client.js on your computer (make sure you've edited the socket.io URL)
  4. send yo's to your API account

notes

this is very specific to my usecase: sending a photo of me from my webcam to a group chat i have with my friends on facebook. because facebook doesn't have a chat API for group chats, i use selenium to control a browser to upload stuff. it's ugly, but it works!

if you use this for your own craziness, you don't need any of the selenium/notifier stuff, just the socket.io stuff.

i used socket.io since it was super easy to set up, and very fast. it triggers pretty much instantly after the yo is sent, so that's cool. have fun!

var io = require('socket.io-client')('http://xxxxxxxxxxxx.herokuapp.com/');
var exec = require('child_process').exec;
var webdriver = require('selenium-webdriver');
var Notification = require('node-notifier');
var notifier = new Notification();
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('https://m.facebook.com/messages/read/?tid=id.xxxxxxxxxxxxxxxxx'); // mobile fb loads a lot faster
driver.findElement(webdriver.By.css('[name=email]')).sendKeys('email');
driver.findElement(webdriver.By.css('[name=pass]')).sendKeys('password');
driver.findElement(webdriver.By.css('[name=pass]')).submit();
io.on('connect', function () {
console.log('yocam: connected to socket.io');
});
// the code inside this is very specific to what i was doing, you don't need it
io.on('yo', function (username) {
// http://iharder.sourceforge.net/current/macosx/imagesnap/
exec('imagesnap -w 1', function (err) {
notifier.notify({
title: 'YOCAM',
message: username + ' TOOK YO PICTURE'
});
console.log('yocam: ' + username + ' sent a yo');
if (err) throw err;
var msg;
if (username === 'BRUGGIE') {
msg = 'zach, you\'re using this to take a selfie, stop';
} else {
msg = 'hi ' + username + '! ';
}
driver.findElement(webdriver.By.css('textarea[name=body]')).sendKeys(msg);
driver.findElement(webdriver.By.css('[type=file]')).sendKeys(process.cwd() + '/snapshot.jpg');
// hecka unstable, sometimes it can't find the el and it crashes
// i run the client with `forever` so it just restarts
// i could actually catch the exception but nahh
driver.wait(function () {
return driver.isElementPresent(webdriver.By.css('.btnC[name=send]:not([disabled])')).then(function (element) {
driver.findElement(webdriver.By.css('.btnC[name=send]')).click();
});
});
});
});
{
"name": "yo-cam",
"version": "0.0.0",
"description": "YO CAM",
"main": "server.js",
"author": "Zach Bruggeman <[email protected]>",
"license": "MIT",
"dependencies": {
"node-notifier": "^3.0.6",
"restify": "^2.8.1",
"selenium-webdriver": "^2.42.1",
"socket.io": "^1.0.6",
"socket.io-client": "^1.0.6"
}
}
var restify = require('restify');
var socket = require('socket.io');
var server = restify.createServer();
server.use(restify.queryParser());
var io = socket(server);
server.get('/', function (req, res, next) {
res.send({status: 'ok'});
next();
});
// set this as your callback URL in your dev dashboard
// it would be nice if yo sent the token as well to verify it's actually from
// them, not just a random person visiting this route
server.get('/yo', function (req, res, next) {
io.emit('yo', req.params.username);
res.send({status: 'ok'});
next();
});
io.on('connection', function () {
console.log('i\'ve made a connection');
});
server.listen(process.env.PORT || 3000, function () {
console.log('yocam listening at %s', server.url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment