Push Twitter Replies to your iPhone using Prowl
Yesterday I bought Prowl for the iPhone. Prowl is similar to Growl on OSX systems. Prowl pushes notifications to your iPhone or iTouch. One of the first uses I thought to use Prowl for was pushing my @replies and mentions from Twitter to my iPhone. I wanted to run the script from my Ubuntu server rather then keeping my desktop on 24/7. I did a quick search online and didn't find any command line options for achieving such a task. I decided to code a quick and dirty PHP script to accomplish what I wanted to do.
This is a quick hack. There are probably more efficient ways of accomplishing this task. I used ProwlPHP to link to the Prowl API.
I assume you know PHP and know how to navigate in Terminal. This is a command line app, it will run on any computer/server running PHP. I have extensively commented the script. Hopefully it is easy to follow along.
Quick Instructions:
- Download the newest version of ProwlPHP and copy it a directory.
- Create a file lastreply.txt and stick in in the same directory as the below code will be copied into. This file needs to have read and write permissions.
- Copy the below code into another file. Alter the ProwlPHP includes directory on line 2, and add your Prowl API key, Twitter Username, and Twitter Password in the constructor.
//CHANGE THIS PATH TO WHERE ProwlPHP IS LOCATED ON //YOUR SERVER include('../API/ProwlPHP.php'); $t = new Twitter(); /** * @class Twitter * This class integrates Twitter and Prowl notifications with the * iPhone. When this script runs it checks to see if any new * Twitter mentions have occured on your account since last check. * If any exist, the Tweet is sent to Prowl and will be notified * via push on your iPhone. * * This class uses ProwlPHP located at: * http://github.com/Fenric/ProwlPHP * * @author Kastang (josh dot kastang at gmail dot com) */ class Twitter { var $xml; var $lastID; var $tUser; var $tPass; var $prowl; /** * Constructor for Twitter Class. Three lines need to be edited * below before running the file: prowl, tUser, and tPass. */ function __construct() { //EDIT THE 3 LINES BELOW $this->prowl = new Prowl('YOUR PROWL API KEY'); $this->tUser = "TWITTER USERNAME"; $this->tPass = "TWITTER PASSWORD"; //XML info loaded from Twitter API. $this->xml = simplexml_load_string($this->getReplies()); //Opens the lastreply file which contants the //id of the last mentioned tweet. $this->lastID = file_get_contents("lastreply.txt"); //If the file is empty (probably the first time //you are using the script). It will pull the //newest mention id from your twitter feed and //store it in the file. if ($this->lastID == null) { $this->lastID = $this->xml->status[0]->id; $full = "@" . $this->xml->status[0]->user->screen_name . ": " . $this->xml->status[0]->text; $this->prowl($full); $this->updateNewest($this->xml->status[0]->id); } //Checks for new Twitter Mentions. $this->checkForUpdates(); } /** * Checks for updates. */ function checkForUpdates() { //first run boolean. $first = true; //For each mention in the XML array, check to see if //the current ID is greater then the last recorded ID. //If it is, push the current Tweet to Prowl, if it isn't, //check to see if it is the first run, if it is, break out //of the for loop, if it isn't the first run, update the //lastreply.txt file and break from the forloop. for ($i = 0; $i < 10; $i++) { $curr = $this->xml->status[$i]->id; if ($curr > $this->lastID) { $first = false; $full = "@" . $this->xml->status[$i]->user->screen_name . ": " . $this->xml->status[$i]->text; $this->prowl($full); } else { if ($first) { break; } else { $this->updateNewest($this->xml->status[($i - 1)]->id); break; } } } } /** * Writes the newest @reply id to a file. */ function updateNewest($id) { $file = fopen("lastreply.txt", "w"); fwrite($file, $id); fclose($file); } /** * Push the Tweet to Prowl. This code is modified from * example.php in the ProwlPHP API Wrapper. */ function prowl($tweet) { $this->prowl->push(array( 'application' => 'Twitter', 'event' => 'Reply', 'description' => $tweet, 'priority' => 0, ), true); } /** * Gets replies from Twitter. In order to grab replies, you * must be authenticated. */ function getReplies() { $twitterHost = "http://twitter.com/statuses/mentions.xml"; $curl; $curl = curl_init(); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERPWD, "$this->tUser:$this->tPass"); curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($curl, CURLOPT_URL, $twitterHost); $result = curl_exec($curl); curl_close($curl); header('Content-Type: application/xml; charset=ISO-8859-1'); return $result; } } |
I chose to add an entry in my crontab to run this script every 10 minutes. The time can be adjusted to suit your needs. Personally I do not see a need to ping Twitter more then once every ten minutes.
*/10 * * * * curl /path/to/replies.php |
If all goes well, you should see something like this when someone mentions you in a tweet:
It is possible to setup a redirection within Prowl to automatically launch your Twitter client of choice when you get a Twitter based notification. You can also open Prowl and view all notifications:
The script should be fairly easy to modify. I will probably add Direct Messaging next to my Prowl Push Notifications.
AWStats on Ubuntu 10.04 Server
Yesterday I finalized moving my websites to my new Linode. With all the base configuration done, it is time to start installing some monitoring and stats tools. AWStats in particular is one of my favorite tools for analyzing web traffic.
This guide assumes you are using Apache2, Ubuntu 10.04(though I would assume to process would be similar for Ubuntu 9.xx), and have root access to your box. Also I assume you have Apache properly setup with a working access.log file.
Installing AWStats
Ubuntu 10.04 has AWStats available in their repositories:
apt-get install awstats |
The Ubuntu Repo. does not have the newest version of AWStats. The repo has 6.9 while the newest, stable, version is 6.95. The 6.9 install that Ubuntu provides should be enough for most people. If you wish, you can install the newest version manually and continue following afterwards.
Configuring AWStats
AWStats does offer some sort of automated configuration process. I have never used or tested this function. If you are not comfortable with manual configuration you may want to look up the automated configuration option.
I have multiple domains I wish to track. I needed to make a copy of the default awstats.conf file for each domain. I used the format awstats.mydomain.ext.conf for my configuration file name. In my case, mydomain.ext is kastang.com. You will have to create a copy of awstats.conf for each domain you want to monitor.
cp /etc/awstats/awstats.conf /etc/awstats/awstats.yourdomain.ext.conf |
Open awstats.yourdomain.ext.conf, find and modify the following fields:
#Path to access.log for your domain LogFile="/var/log/apache2/access.log" #I recommend '1' for more detailed information LogFormat=1 SiteDomain="yourdomain.ext" HostAliases="localhost 127.0.0.1 yourdomain.ext" |
Now we can generate the initial stats for AWStats based off of your existing access.log file. You need to run this command for every domain you have configured.
/usr/lib/cgi-bin/awstats.pl -config=yourdomain.ext -update |
Check the output for any errors. If it successfully Finds/Parses your access.log file you should be good to continue. If you have any errors, double check your configuration file and make sure the path to the access.log is correct.
Configuring Apache
After configuring AWStats, we need to tell Apache where to point when the AWStats address is accessed.
Navigate to /etc/apache2/sites-available/. If you have no VirtualHosts setup, you will want to place the following code in 'default'. If you have VirtualHosts setup, add this code inside the VirtualHost tag for each domain you want to monitor.
Alias /awstatsclasses "/usr/share/awstats/lib/" Alias /awstats-icon/ "/usr/share/awstats/icon/" Alias /awstatscss "/usr/share/doc/awstats/examples/css" ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ ScriptAlias /awstats/ /usr/lib/cgi-bin/ Options ExecCGI -MultiViews +SymLinksIfOwnerMatch |
Reload the Apache configuration:
/etc/init.d/apache2 reload |
Check to make sure you do not have and errors or warnings. Depending how you originally setup your Apache config, the /cgi-bin/ ScriptAlias may already be implemented. In this case, just remove that line from the configuration file. (Don't forget to reload the configuration after all changes).
You should now be able to navigate to http://yourdomain.ext/awstats/awstats.pl and see the existing webstats from your access.log file.
Automating AWStats updates
AWStats will not automatically update by default. I cuggest using Cron to automate the process:
Add this to crontab and it will update stats once every three hours. You can adjust the timing to suit your needs. I have seen AWStats update on a weekly basis, daily basis, and even sometimes every couple minutes. You will need to add a separate crontab line for each domain AWStat is monitoring.
0 */3 * * * root /usr/lib/cgi-bin/awstats.pl -config=yourdomain.ext -update >/dev/null |
Securing AWStats
If you are at this step, you should have a working AWStats. One issue is AWStats does not come secured by default. They do offer to enable Authentication in the configuration, but I think it is just easier to generate a simple .htaccess file. This method is a quick and dirty method for securing AWStats. If you require a more sophisticated method of securing AWStats, please consult the official documentation.
Navigate to /usr/lib/cgi-bin/ this is where awstats.pl is located.
Create a new .htpasswd file:
htpasswd -cb /path/to/.htpasswd username password |
Create a new .htaccess file with the following inside:
AuthName "AWStats Authentication" AuthType Basic AuthUserFile /path/to/.htpasswd Require valid-user |
Now if all goes well, when you refresh your browser you will be prompted to enter a username and password to access AWStats.
This should get you to a point where AWStats is usable and (reasonably) secure. From here you can add Plugins or further configure AWStats to fit your individual needs.
Adjust ATI Fan Speed in Ubuntu
I found this post hovering in my Drafts folder today, the blog post is old but the information still applies.
Currently ATI drivers do not have the ability to modify fan speeds from their GUI. Here is a quick and easy way to manually adjust fan speeds via Terminal. The commands below work with my ATI Radeon 4870 card, I would assume they will also work with any ATI card with a fan though. Note: ATI drivers from ATI website are required.
To check the temperature of your card:
aticonfig --adapter=0 --od-gettemperature
To change the fan speed:
aticonfig --pplib-cmd "set fanspeed 0 XX"
(Where the XX is replace it with the fan speed percentage)

