Archive for scripting

pull analytics “site search terms” with google data api

I’d heard about the google data api, but up until recently hadn’t found much practical use for it.

Suddenly I needed to pull back the top search terms for a site monitored by google analytics. The default view only shows 500 at a time. Granted this accounted for 30% of the “unique searches’, but I wanted to see a bit more.

Though it was sort of hard to find, I used the example script dataFeed.sh to retrieve the top 10k search terms.

The script wants you to set the variables for username/password and “PROFILE_ID” The value for this last is the id=xxxx in the url when you cruise around the analytics site.

Rather than read thru the quirky documentation as to what dimensions and metrics were available, I used the Data Feed Query Explorer to find the values instead:

feedUri="https://www.google.com/analytics/feeds/data\
?start-date=2009-08-01\
&end-date=2009-12-04\
&dimensions=ga:searchKeyword\
&metrics=ga:searchDepth,ga:searchDuration,ga:searchExits,ga:searchRefinements,ga:searchUniques,ga:searchVisits\
&sort=-ga:searchUniques\
&max-results=40000\
&ids=ga:$PROFILE_ID\
&prettyprint=true"

curl $feedUri -s --header "Authorization: GoogleLogin $googleAuth"

Despite the max-results of 40k, it looks like there is a max of 10k, which is fine.

All in all, pretty ez. Digging it all up.. sort of sucked.

Hence the write up… hope it helps some buddy!

Advertisements

Leave a Comment

bash: string to integer conversion

Bash arrays are odd, globally scoped critters which can only be indexed by integer values… but the values don’t have to be contiguous so they are sort of like hash maps…

All you need is something to convert strings to integers, for example:

_map_string_to_integer() {
	echo ${*} | od -t u1 | sed 's,[^ ]*,,;s, ,,g;' | tr -d '\n'
	echo
}

And you can use it like this:

% fun[$(_map_string_to_integer this)]="is neat"
% echo ${fun[$(_map_string_to_integer this)]}
is neat

if that is of any use to anyone….

Leave a Comment

4 useful shell scripts for maven

yeah maven

Maven (2 of course) is the jam when it comes to Java build systems.

Ant is some old timey donkey-mess madness which doesn’t even address the issue of dynamic build dependency resolution.

There are other build systems like: ivy, maven1, and made-up-mess, but the real goodness is the m2 universe.

In addition, to it’s main duties, maven produces a really neat resource in the form of your local repository (mine lives in ${HOME}/.m2/repository).

The dependencies your project pulls in also pull in dependencies and some of them are cool things you didn’t even know you couldn’t live without!

Here are a few scripts I use to leverage a little bit of that powerful force for funtimes!

m2_cp.sh : maven classpath

Maven compiles all your code and runs all your test cases. To do that it has to use an olde timey classpath just like duh, but …. how can you get at it when all you see is your pom.xml?

You can use the olde school trick: mvn -X test -Dmaven.test.skip=true ; and scrap it out of there

Or… you can use my little m2_cp.sh and it will parse it out for you into a file called “m2_cp.txt” that you can use to play more reindeer games.

Like what?

Like packaging up some tarball+script to run your stuff, or some uberJar (though the assembly plugin is a better solution for that), or maybe some…

m2_javap.sh : all your class are… ok…

Almost every Java developer uses an IDE. They pretty much all use eclipse.

I’m in that oddball 0.01% that just uses eclipse as a remote debugger… so… I use a lot of command line tools instead, for example: javap

The javap command will print the method signatures given a list of class names. But… you have to feed it a classpath.

In tricky eclipse, this happens like magic! Neat! But… as a magician, I want to be the magical man… so…

When I’m working on say, a portlet project (from maven-archetype-portlet), and I’m wanting to see some good times, I type: m2_javap.sh javax.portlet.GenericPortlet

Even that too much bother? How ’bout: m2_javap.sh GenericPortlet

m2_latest.sh : to the greatest

Ever use log4j in a project? Ha! Yeah… so now you have a new project and you want to pull it in again… what as the most recent version: m2_latest.sh log4j ; tells you the jar

m2_dep.sh : jar file to dependency

OK, so you need log4j, and the latest is ${HOME}/.m2/repository/log4j/log4j/1.2.14/log4j-1.2.14.jar ; time to start typing that annoying stanza… but wait:

m2_latest.sh log4j | m2_dep.sh

% m2_latest.sh log4j | m2_dep.sh

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>

Script it up

The local repo is really neato! Perfect scripting fodder! Good times…

Leave a Comment

scripting essentials: fullpath

One of the things you end up doing in cli world a lot is jacking around with files.

Usually you are doing some junk with files in some (collection of) terminal(s) with a given working directory and referring to files with a local path eg: src/main/webapp/WEB-INF/web.xml

Then… for some reason, you want to manipulate them from another context, maybe a browser or some shells doing stuff in another working directory. In some cases you could change working directories, but is that really what you want?

Kind of breaks the flow, doesn’t it? “Now let me hop over here…”

Why are you hopping? You want to grep src/main/webapp/WEB-INF/web.xml? Just use the fullpath name, eg: /home/jameson/src/devilish/src/main/webapp/WEB-INF/web.xml !

But how? EZ: echo ${PWD}/src/main/webapp/WEB-INF/web.xml

By jamming around the fullname instead of hopping around the filesystem like a bunny rabbit, you will be better able to maintain the separation between the different contexts of your activities.

Ya dig it? If not, try this approach out for a while and see if you can feel the difference.

To make this easier, I prefer to use a bash function like this:

fullpath() { 
    local f
    for f in ${*}; do
        ( 
		 	( 
			 	cd ${f} && echo ${PWD} 
			) || ( 
			 	cd $(dirname ${f} ) && echo ${PWD}/$( basename ${f} ) 
			) 
		) 2> /dev/null
    done
}

This takes care of some annoying corner cases that I can’t remember at the moment… but feel free to shorten it

This tip says: move the world around your mind, not your mind around the world.

Leave a Comment

pretty column (field) printing with awk

You know what you have to do… you have to collect all the strings for every field, keep the max length for each column, then at the end spit them back out again.

But… you need to store each field for each row and awk only supports 1 dimensional arrays… suck it up and say the script only supports up to a max of 255 fields:

BEGIN {
	row = 0
	max_fields = 0;
	super_max = 255;
} {
	for ( i = 1 ; i <= NF ; i++ ) {
		idx = row + super_max * i;
		len = length( $i );
		data[ idx ] = $i;
		if ( len > max[ i ] ) {
			max[ i ] = len;
		}
	}
	if ( NF > max_fields ) {
		max_fields= NF;
	}
	row++;
} END {
	for ( r = 0 ; r < row ; r++ ) {
		for ( i = 1 ; i <= max_fields ; i++ ) {
			idx = r + super_max * i;
			fmt = "%-" max[ i ]  "s ";
			printf( fmt , data[ idx ], max[ i ]  );
		}
		printf( "\n" );
	}
}

columnbo.awk

Here is some prettified output:

this.logger            = logger;            
this.dataSourceFactory = dataSourceFactory; 
this.wsdlUrl           = wsdlUrl;           
this.wsdlKey           = wsdlKey;           
this.documentDAO       = documentDAO;   

Leave a Comment

adding empty constructors

Here is a kooky cli I cooked up to add empty constructors to all the model objects in the project that have explicit constructors but no empty constructors…

vim $( for o in $( for m in $( nocvs |grep /model/ ) ; do c=$( echo ${m} | sed ‘s,.*/,,;s,\..*,,’ ) ; grep -c “public ${c}()” ${m} | sed “s,^0$,${m},” ; done | grep src ) ; do c=$( echo ${o} | sed ‘s,.*/,,;s,\..*,,’ ) ; grep -l “public ${c}” ${o} ; done )

Madness!

Leave a Comment

css sprites and montage

I recently started playing around with css sprites again.

Using the montage command, I was able to glue a number of individual sprites together:

montage  ???1.png   -tile 1x -geometry +0+0 -background none last1.png

Using these tricks, I implemented a bejeweled-like puzzle game puzzle game, completely in Javascript using CSS trickery.

Lots of fun! Check it out!

Leave a Comment

« Newer Posts · Older Posts »