xxxxxxxxxx
144
/*
var ck = 'OTgmJSU3BjUIEFEbI10CWxwLHRELUBMhAw==';
var cs = 'UiI3ChZQDh0+HlcnHVsGUz8QWEcMMAIMISg5QhpLO1snAhkKPSEKOC4HHlkKIjoALxg=';
var t = 'W11GXEFTWUZaRlRbQ1hDVFBGRDo2KytdHhBfAFA0DDFAE0EsAAEDFlNcPAAxMQJKCzQ=';
var ts = 'Ei1FPSoiGTg4QAw8QhM3CCshDUEmHhBdESwtQS8WLyUzOhAvWzQ6ODoxKj5L';
*/
var ck = 'CSI5Hio1IxkYHTADEBtFUCYCIkQ1EgQCCw==';
var cs = 'Dzw/DwQuG0UbNxsSSh8jABAwLUQkEUcFNyxQNTEdDyEIOQRRDEoRJQsZE1g/VxA9PQQ=';
var t = 'UF5GXURWXEVaXgglMBweGQwIBgsoMhENBjcfNC4aGS9CAAouBBY7C1IrIx0QMTAfWwQ=';
var ts = 'KCoYKxESPShZHAVaITgpJy43GCAlBxReGDI4Aiw9Cg87AUQHWzAkJjIRMQM9';
let button;
var cb = new Codebird();
function setup() {
createCanvas(windowWidth, windowHeight);
button = createButton('post to twitter');
button.position(20, 20);
button.mousePressed(captureImageAndSendTweet);
createP('https://twitter.com').addClass('text').hide();
createP("error posting tweet").addClass('text').hide();
messages = selectAll('.text');
}
function draw() {
background(255);
noStroke();
fill(80, 128, 200);
rect(width/4, height/4, width/2, height/2);
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
// ------------------------------------------
// all of the twitter stuff
// 1) grab canvas
// 2) convert to base64 image
// 3) upload to twitter, get the media ID
// 4) post a tweet containing the media ID
function captureImageAndSendTweet(){
var img = captureImage();
postImage(img);
}
function captureImage(){
button.hide();
messages[0].hide();
messages[1].hide();
var currentCanvas = document.getElementById("defaultCanvas0");
var b64Img = currentCanvas.toDataURL('image/png').replace(/data:image\/png;base64,/, '');
button.show();
return b64Img;
}
function postImage(img){
cb.setConsumerKey(decrypt(ck, "chris"), decrypt(cs, "chris"));
cb.setToken(decrypt(t, "chris"), decrypt(ts, "chris"));
var params = {
"media_data": img
};
cb.__call(
"media_upload",
params,
parseMediaId
);
}
function parseMediaId(reply, rate, err){
let mediaId = reply.media_id_string;
sendTweet(mediaId);
}
function sendTweet(mediaId){
cb.setConsumerKey(decrypt(ck, "chris"), decrypt(cs, "chris"));
cb.setToken(decrypt(t, "chris"), decrypt(ts, "chris"));
cb.__call(
"statuses_update",
{
"media_ids": mediaId,
// "status": "text for the status"
},
function (reply, rate, err) {
console.log(reply);
textSize(width / 3);
textAlign(CENTER, CENTER);
fill(0);
if (reply.text != undefined){
console.log(reply.text);
messages[1].hide();
messages[0].html('<a href=' + reply.text + ' target="_blank">' + reply.text + '</a>');
messages[0].position(25, 40);
messages[0].show();
} else {
console.log("error posting tweet");
messages[0].hide();
messages[1].position(25, 40);
messages[1].show();
}
}
);
}
// ------------------------------------------
// ------------------------------------------------------------------------------------------------
// horrible "encryption" code below
// only to stop random bots scraping the web for api keys
function encrypt(string, key){
result = btoa(encryptXor(string, key));
return result;
}
function decrypt(string, key){
result = encryptXor(atob(string), key);
return result;
}
function encryptXor(text, key) {
var result = '';
for (var i = 0; i < text.length; i++) {
result += String.fromCharCode(text.charCodeAt(i) ^ key.charCodeAt(i % key.length));
}
return result;
}
// ------------------------------------------------------------------------------------------------