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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
<?php
require_once('twitter/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) {
$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;
// Additional code to receive posts from postmarkapp
$mail = file_get_contents('php://input');
$m = json_decode($mail);
 
$text = $m->TextBody;
/*
"TextBody": "This is a reminder for:\r\n\r\nTitle: test twitter account\r\nWhen: Thu 26 Jan 19:00 – 20:00 London\r\nCalendar: Momentum\r\nWho:\r\n * paulsgrovemen@gmail.com- organiser\r\n\r\nEvent details: \r\nhttps://www.google.com/calendar/event?action=VIEW&eid=MTQ5aTFwNXUycGJ2aHJwdTBjZnFtdGRhdTQgcGF1bHNncm92ZW1lbkBt&tok=MjMjcGF1bHNncm92ZW1lbkBnbWFpbC5jb20yMWFkYzdiODIxNTUxMDA5ZTdhYTQ1NDZhYjU0YWVjODk2ZGZjMTZh&ctz=Europe%2FLondon&hl=en_GB\r\n\r\nInvitation from Google Calendar: https://www.google.com/calendar/\r\n\r\nYou are receiving this email at the account paulsgrovemen@gmail.com because \r\nyou set a reminder for this event on the calendar Momentum.\r\n\r\nYou can change your reminders for specific events in the event details page \r\nin https://www.google.com/calendar/.\r\n"
*/
// 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;
 
 
 
 
// Send to twitter
print sendToTwitter($message);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.