Archive for August, 2009
Check if time falls between a range in PHP
I was digging through some old stuff today and found this PHP function I wrote awhile back. You pass a time to the function along with a start time and end time, and it will determine if the time falls between that range. I figured I would post it on here in case anyone has a need for it.
function CheckTime($currentTime, $startTime, $endTime){
// written 11/26/2006 by Patrick H. (patrickh@gmail.com)
//
// the time passed must meet all the below criteria to return 1 (true):
//
// - current hour needs to be equal or greater than start hour
// - current hour needs to be equal or less than end hour
// - current minute needs to be equal or greater than start minute (if current hour is ok)
// - current minute needs to be equal or less than end minute (if current hour is ok)
//
// if any of those checks does not pass, it will return 0 (false)
global $cHour;
global $cMin;
global $sHour;
global $sMin;
global $eHour;
global $eMin;
// break up current time
$now = explode(":",$currentTime);
$cHour = intval($now[0]); // current time - hour
$cMin = intval($now[1]); // current time - minute
// break up start time
$start = explode(":",$startTime);
$sHour = intval($start[0]); // start of range - hour
$sMin = intval($start[1]); // start of range - minute
// brek up end time
$end = explode(":",$endTime);
$eHour = intval($end[0]); // end of range - hour
$eMin = intval($end[1]); // end of range - minute
// this is the variable used to track the result of the checks
$pass = true;
if($sHour < $eHour){
// the range is on the same day
// compare to the start hour
if($cHour < $sHour){
$pass = false;
};
// compare to the end hour
if($cHour > $eHour){
$pass = false;
};
// compare to the start min
if($cHour == $sHour){
if($cMin < $sMin){
$pass = false;
};
};
// compare to the end min
if($cHour == $eHour){
if($cMin > $eMin){
$pass = false;
};
};
} else {
// the range is overnight, so the logic is a little different
if( ($cHour < $sHour) && ($cHour > $eHour) ){
$pass = false;
};
// compare to the start min
if($cHour == $sHour){
if($cMin < $sMin){
$pass = false;
};
};
// compare to the end min
if($cHour == $eHour){
if($cMin > $eMin){
$pass = false;
};
};
};
// done with check, return the result
if($pass == false){
return 0; // failed
} else {
return 1; // passed
};
};
// test it out
// make sure you use military time
// usage: current time, range start time, range end time
echo CheckTime("06:30","15:20","06:45"); // this should return 1
Using Perl for Google Analytics API
Posted by Patrick in google analytics, perl on August 10th, 2009
We use Google Analytics at work, and it has always been painful to create any kind of automated reports using that data. To date I have had to setup scheduled .csv reports to myself, parse them and import the into a database. It was definitely a pain to setup and maintain, so I was thrilled to see Google finally released a API for Google Analytics. I decided to put this together to help give people a starting point for using their Google Analytics information in their Perl programs.
The first thing you will need to do is request a token from Google. This is what you will use to authenticate yourself on all data feed requests for your session.
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use XML::Simple;
# this sub will return the token you need to authenticate api requests
# you need to pass your ga login and password to it
sub gaGetToken {
# arguments passed to this function
my $user = $_[0];
my $pass = $_[1];
# create user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# Create a request
my $req = HTTP::Request->new(POST => 'https://www.google.com/accounts/ClientLogin');
$req->content_type('application/x-www-form-urlencoded');
$req->content("accountType=GOOGLE&Email=$user&Passwd=$pass&service=analytics&source=companyName-applicationName-versionID");
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# declare variable
my $token;
# Check the outcome of the response
if ($res->is_success) {
# look at the result
if ($res->content =~ m/(?<=Auth=).*/im) {
# store token so it can be used in subsequent requests
$token = $&;
}
}
else {
# return the error if there was a problem
return "error: ". $res->status_line;
die;
}
# return the token
return $token;
}
# authenticate with the API to receive token
my $token = &gaGetToken('username','password');
This will give you the token you need to make other requests. Now you have the token, you can request a data feed of the analytics account information that is tied to your Google account. The way I chose to handle this is the parse the feed and return an array of account names and account numbers.
# this sub will return an array of all your ga accounts
# you need to pass your token to it
sub gaAccounts {
# the token you passed to this sub
my $token = $_[0];
# create user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# add authorization to headers
my @headers = (Authorization => "GoogleLogin Auth=$token");
# request the accounts feed
my $res = $ua->get("https://www.google.com/analytics/feeds/accounts/default", @headers);
# define accounts array
my @accounts;
# if the request was successful...
if ($res->is_success) {
# declare variables
my ($content, $e);
# this is the xml it returns
$content = $res->content;
# create a xml object for the response
my $xml = new XML::Simple(KeyAttr=>[]);
my $tree = $xml->XMLin($content);
# iterate through each entry
my $x = 0;
foreach $e (@{$tree->{entry}})
{
# add the account to the array
$accounts[$x][0] = $e->{title}->{content};
$accounts[$x][1] = $e->{'dxp:tableId'};
$x++
}
} else {
# return the error if there was a problem
return "error: ". $res->status_line;
die;
}
# return the array of accounts
return @accounts;
}
# get array of all sites in your account
my @sites = &gaAccounts($token);
# list all the sites in the array
foreach(@sites) {
print $_->[0] ." => ". $_->[1] ."\n";
}
# going forward, we will use the first account number
my $website = $sites[0][1];
Now you have a token and account number, you have everything you need to make data feed requests for the Google Analytics information for that account. Here is a example of getting visits by day:
# this sub will return the xml from a datafeed request
# you need to pass your token to it
sub gaDataFeed {
# arguments passed to this function
my $url = $_[0];
my $token = $_[1];
# create user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("MyApp/0.1 ");
# add authorization to headers
my @headers = (Authorization => "GoogleLogin Auth=$token");
# request page
my $res = $ua->get($url, @headers);
my $content;
# if the request was successful...
if ($res->is_success) {
# this is the xml response
$content = $res->content;
} else {
# return the error if there was a problem
return "error: ". $res->status_line;
die;
}
# return the xml
return $content;
}
# to test, lets request visit by day
my $pageviews = &gaDataFeed("https://www.google.com/analytics/feeds/data?ids=$website&dimensions=ga%3Adate&metrics=ga%3Avisits&start-date=2009-07-27&end-date=2009-08-10&max-results=365", $token);
# create a xml object for the response
my $xml = new XML::Simple(KeyAttr=>[]);
my $tree = $xml->XMLin($pageviews);
# iterate through each entry in the xml
foreach my $e (@{$tree->{entry}})
{
# date value
print $e->{'dxp:dimension'}->{value};
print " : ";
# visitors value
print $e->{'dxp:metric'}->{value};
print "\n";
}
That is the basics, you can use the above to access just about anything in your Google Analytics account. There is a good query explorer that you can use to generate and test data feed request URI’s. I hope this helps you get started with Google Analytics and Perl.
Streaming HD movies to PS3
I recently found out that I could stream HD video files to my PS3. If you have a file that is .m2ts, you can add it to your Tversity library and the PS3 will recognize it and play with no problems. However, often when you download a x264 movie it comes in the format .mkv which the PS3 does not support. Luckily there is a quick way to convert .mkv to .m2ts on PC:
- Download tsMuxer made by SmartWare (freeware)
- add your .mkv file to the Input Files area
- choose M2TS muxing in the Output area
- under General track options, set Change level to 4.0 if it defaults to a level higher then that
- click the Start muxing button and it will create you a new .m2ts file that you can stream to your PS3
The program is surprisingly fast, it usually takes me 1-2 minutes to convert a 5GB video file.
Tversity streaming to PS3 fix
Posted by Patrick in 64-bit, networking, ps3, tversity on August 2nd, 2009
Despite Tversity streaming flawlessly to my PS3 from Windows XP x86, I have never been able to get it working quite right from Windows XP x64. Every time I have tried to set it up, it would always have some degree of choppiness and not allow me to fast forward more than a few seconds before it would hang. When looking through the Tversity forums tonight I finally stumbled on a solution that has appeared to fix things entirely.
The key seems to be changing the MTU value on the PS3 from automatic to 1496 (instructions). That’s it, all I did was change that on my PS3 and use the default Tversity settings and everything streams with no stuttering at all.