public
Last active

  • Download Gist
gcal2twitter.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
<?php
require_once('twitteroauth/twitteroauth.php');
 
define("CONSUMER_KEY","");
define("CONSUMER_SECRET","");
define("SOURCE","");
 
define("OAUTH_TOKEN","");
define("OAUTH_TOKEN_SECRET","");
 
 
function getConnectionWithAccessToken($oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_token, $oauth_token_secret);
return $connection;
}
 
function sendToTwitter($message,$lat,$long) {
$connection = getConnectionWithAccessToken(OAUTH_TOKEN,OAUTH_TOKEN_SECRET);
$content = $connection->post("statuses/update", array(
"status" => $message,
"source" => SOURCE
));
if (empty($content)) {
return "Failed sending to twitter: '".$message;
} else {
return "Sent message to twitter: '".$message;
}
}
 
function relative_date($d) {
 
// Format time
$the_time = date("g:ia",$d);
 
 
// Start of today
$s_today = strtotime(date("Y-m-d"));
// Start of tomorrow
$s_tomorrow = $s_today + 86400;
 
// Start of tomorrow+1
$s_dayafter = $s_tomorrow + 86400;
$diff = time() - $d;
 
 
if ($d > $s_dayafter) { // This is after tomorrow
return date("D jS F",$d) . " at $the_time";
} elseif ($d > $s_tomorrow) { // This is tomorrow
return "tomorrow at $the_time";
} elseif ($d > $s_today) { // This is today
if (date("G",$d) > 17) {
return "this evening at $the_time";
} elseif (date("G",$d) > 12) {
return "this afternoon at $the_time";
} else {
return "today at $the_time";
}
}
}
 
 
 
function decode_message($mail) {
 
// Get the text encoded version
$mail = str_replace("\r","",$mail);
 
print "\n\n";
preg_match("/base64\n+([^\\=]+=)/",$mail, $a);
//var_dump($a);
$text = base64_decode($a[1]);
//var_dump($text);
//var_dump($text);
// Find the title
preg_match("/Title: ([^\\r\\n]+)/",$text, $b);
$title = $b[1];
 
 
// Find the location
preg_match("/Where: ([^\\r\\n]+)/",$text, $b);
$where = $b[1];
 
 
//Get the time
//Time: Mon 14 Sep 12:15 – 13:15
preg_match("/When: \w+ (\d+) (\w+) (\d+):(\d+)/",$text, $b);
 
$time = $b[1] . " " . $b[2] . " " . date("Y") . " " . $b[3] . ":" . $b[4] . ":00";
 
$t = strtotime($time);
 
$message = $title . " " . relative_date($t) . ", " . $where;
return $message;
}
 
// Load e-mail content
$mail = $HTTP_RAW_POST_DATA;
 
 
// Decode message
 
$message = decode_message($mail);
 
 
// Send to twitter
print sendToTwitter($message);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.