I was quite disappointed that there is no Push Support for Twitterrific Premium 2.1 for the iPhone. So I decide to write a short code to Push Tweets through Prowl.
This script will get the latest tweet on your “friends and user” timeline and push it to your iPhone through Prowl.
To run this script, you will need to have a server or web-host capable of running PHP on a crontab. (the host I use, uses CPanelX). I use PHP cause most web-host will support it without special requirement.
Instructions
1. Upload the main script, a setting file, and an ID file which you need to set write permission to make it work.
2. Fill in your Twitter ID, Password and Prowl API-Key in the gettweet.ini file.
3. Enable Write permission to the lastTweetID file (only needed by some host).
4. Setup Cron to poll Twitter every minute. (the script will stop polling if your hits is too low, to prevent blacklisting).
I do not provide guarantee for this script in any foreseeable way.
I will not be responsible for any harm or damages cause in the process of using this Script.
GetTweet.zip (3.4 KiB, 96 hits)
/*
This file is part of Gettweet Script.
Copyright (C) <2009>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
*/
include "gettweet.ini";
# Setup Prowl
$application = "Twitter";
$priority = 0;
#Text to remove (Regex)
$TextTrim = "http:\/\/[a-z.\/0-9A-Z]+"; #Remove Http links - Dsiplay Text only.
#Twitter Rate Limiting - to prevent Blacklisting due to excessive hits
$ratelimit = 80; #Don't poll for status under this limit.
$TwitRate = getRate($twitter_user, $twitter_password);
$cur_time = time();
$time2reset = ($TwitRate -> {"reset-time-in-seconds"} - time());
$time_min = (int)($time2reset/60);
$time_sec = $time2reset - ($time_min * 60);
if ($TwitRate -> {"remaining-hits"} <= $ratelimit) { outputhtmlheader(); echo "Hits Remaining : " . $TwitRate -> {"remaining-hits"} . " of ". $TwitRate -> {"hourly-limit"} . "
";
echo "Twitter Rate set limit hit (set to {$ratelimit})";
echo "Reseting in : {$time_min}min and {$time_sec}sec
";
}
else
{
$Tstatus = getTweet($twitter_user, $twitter_password) -> status;
#check for Twitter Error;
if ($Tstatus == NULL)
{
echo "Twitter Error";
}
else
{
#Process XML
#check for new Update
$TUser = $Tstatus -> user;
if ($Tstatus != "")
{
outputhtmlheader();
if (file_exists('lastTweetID.txt'))
{
$f = fopen('lastTweetID.txt', 'w');
if ($f)
{
fwrite($f, $Tstatus -> id);
fclose($f);
}
else
{
echo "File Write Error";
}
}
else
{
echo "File lastTweetID.txt missing";
}
#Send to Prowl
$description = preg_replace("/{$TextTrim}/","", $Tstatus -> text);
$event = "Updates from {$TUser -> name}";
echo $event;
echo "
";
echo $description;
echo "
";
echo "Hits Remaining : " . $TwitRate -> {"remaining-hits"} . " of ". $TwitRate -> {"hourly-limit"} . "
";
echo "Reseting in : {$time_min}min and {$time_sec}sec
";
outputhtmlfooter();
sendProwl($apikey,$application,$event,$description,$priority);
}
else
{
outputhtmlheader();
echo "No New Updates
";
echo "Hits Remaining : " . $TwitRate -> {"remaining-hits"} . " of ". $TwitRate -> {"hourly-limit"} . "
";
echo "Reseting in : {$time_min}min and {$time_sec}sec
";
outputhtmlfooter();
}
}
}
function getTweet($twitter_user, $twitter_password)
{
#init Curl and check for last setting
$ch = curl_init();
if (file_exists('lastTweetID.txt'))
{
$lastID = file_get_contents('lastTweetID.txt');
if ($lastID == NULL)
{
$TwitterURL = "http://twitter.com/statuses/friends_timeline.xml" ."?count=1";
}
else
{
$TwitterURL = "http://twitter.com/statuses/friends_timeline.xml" . "?count=1" . "&since_id=" . $lastID;
}
}
else
{
return NULL;
}
# Setup CURL
curl_setopt($ch, CURLOPT_URL, $TwitterURL);
curl_setopt($ch, CURLOPT_USERPWD, "{$twitter_user}:{$twitter_password}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
# Get from URL
$xmlTwitter = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200)
{
return NULL;
}
else
{
return simplexml_load_string($xmlTwitter);
}
}
function sendProwl($apikey,$application,$event,$description,$priority)
{
#init Curl and check for last setting
$ch = curl_init();
$prowlurl = "https://prowl.weks.net/publicapi/add";
curl_setopt($ch, CURLOPT_URL, $prowlurl);
curl_setopt($ch, CURLOPT_HEADER, True);
curl_setopt($ch, CURLOPT_CRLF, True);
curl_setopt($ch, CURLOPT_POST, True);
curl_setopt($ch, CURLOPT_POSTFIELDS, "apikey=" . $apikey . "&application=" . $application . "&event=" . $event . "&description=" . $description . "&priority=" . $priority) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$HttpData = curl_exec($ch); # grab URL and pass it to the browser
curl_close ($ch); # close cURL resource, and free up system resources
}
function getRate($twitter_user, $twitter_password)
{
# INSTANTIATE CURL.
$ch = curl_init();
$TwitterURL = "http://twitter.com/account/rate_limit_status.xml";
# Setup CURL
curl_setopt($ch, CURLOPT_URL, $TwitterURL);
curl_setopt($ch, CURLOPT_USERPWD, "{$twitter_user}:{$twitter_password}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
# Get from URL
$xmlRate= curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpcode != 200)
{
return NULL;
}
else
{
return simplexml_load_string($xmlRate);
}
}
function outputhtmlheader()
{
echo "";
echo "";
echo "";
echo "";
echo " ";
echo "";
}
function outputhtmlfooter()
{
echo "";
echo "";
}
?>
Put your Twitter Password and Prowl API key in the gettweet.ini file.