Tuesday, 26 August 2014

Mark Down

Download

Beanshell: download

Requirement

A markdown based parser that can work in on both desktop and mobile platform. In this, the daringfireball offering falls short aimed heavily at blogging platforms and Perl on the command line.

The syntax, like with any Markdown concept has to be simple to use. See the README for details.

Beanshell is a requirement of this script. Beanshell is a Java based scripting language available for any platform that Java is available for.

- Android SL4A and BeanShell for Android.

- Desktop Create a wrapper script which triggers Beanshell. I made this (linux/Mac) utility for the purpose.

Design

The intention was to use the system clipboard as a general purpose (and useful) mechanism to allow the user to interact with the script without getting in the way of the workflow of creating a blog article.

The script will look for the contents of the system clipboard, parse the contents and then re-paste the parsed text back to the clipboard. This is neat because it works on both Desktop and Mobile, and is simple requiring no user interface other than notification messages.

The actual parser is a simple Regular Expression based parse that applies the expressions line by line. Not terribly elegant but suitable for the objective we are trying to acheive.

Final Thought

The original objective was to create something so compact it could fit into a QR code. However that seems unlikely to fit unless a different approach is taken.

Monday, 30 June 2014

Download a Spreadsheet from Google Docs

The following is my research into the Google APIs and how to successfully download a spreadsheet.

Auth Token

The method used for authorisation in this article is the ClientLogin Interface. This endpoint is accessible from a HTTP endpoint, which is ideal for a shell script.

The interface also requires the caller to specify the type of service that will be accessed which we can specify in the script.

This kind of interface is appropriate for applications which are installed on a users system, as they require the username and password of the user to perform their action.

Spreadsheet Application

The spreadsheet export feature is a little trickier to find API documentation for. Instead the URL was derived by looking at the URL the browser uses for the same function.

Script

The following is the script which I put together using CURL to perform the download.

 # Download Google Docs Spreadsheet  
 # google-login  
 # Performs a login operation against the Google ClientLogin endpoint to acquire the access token.  
 #   
 # For more details on request parameters, see:  
 # https://developers.google.com/accounts/docs/AuthForInstalledApps#Request  
 #  
 # This function needs to know the service that this auth token will be used for.  
 # This is covered in the following FAQ:  
 # https://developers.google.com/gdata/faq#clientlogin  
 #  
 # Parameters:  
 # 1 - Google E-mail address  
 # 2 - Google Password  
 # 3 - Service name  
 AUTH=  
 google-login() {  
     URL="--silent"  
     URL="$URL https://www.google.com/accounts/ClientLogin"  
     URL="$URL --data-urlencode Email=$1"  
     URL="$URL --data-urlencode Passwd=$2"  
     URL="$URL -d accountType=HOSTED_OR_GOOGLE"  
     URL="$URL -d source=Google-cURL-Example"  
     URL="$URL -d service=$3"  
     # Make the call and parse the auth token.  
     AUTH=`curl $URL | grep Auth= | sed -E 's/=/ /' | cut -d ' ' -f 2`  
 }  
 # spreadsheet-download  
 # Uses a given auth token to download the named Google Spreadsheet.  
 #  
 # Parameters:  
 # 1 - Auth token  
 # 2 - File ID  
 # 3 - Format: xls or csv  
 spreadsheet-download() {  
     curl --silent --header "Authorization: GoogleLogin auth=$1" "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=$2&hl&exportFormat=$3"  
 }  
 # Takes the given login credentials and performs a login, followed by a download  
 # operation for a Google Spreadsheet.  
 #  
 # Parameters:  
 # 1 - Google Email  
 # 2 - Google password  
 # 3 - Document File ID  
 download-as-xls() {  
     google-login "$1" "$2" wise  
     spreadsheet-download "$AUTH" "$3" xls  
 }  
 download-as-xls joe@blogs.com password 0123456789ABCDEFGHIJKLMNOPQURSTUWXYZ  

Feel free to use in your own projects.

Tuesday, 15 April 2014

ffmpeg Scripts

A collection of ffmpeg scripts I've created and added to my path that solve specific problems: XBOX compatible MP4
 ffmpeg -i "$1" -c:v libx264 -profile:v high -crf 22 -strict experimental -c:a aac -q:a 6 "$1.mp4"  
Convert to MP3
 ffmpeg -i "$1" "$1.mp3"  
Convert to MPEG
 ffmpeg -i "$1" -qscale:v 1 "$1.mpg"  
The last one is useful for appending videos together before final conversion.

Friday, 5 October 2012

Controlling what Log4J logs to a file

To control what goes into a log file, we can use the filtering mechanism that is provided by Log4J.

Filters operate in a stack, with a logging event being passed to the first filter in the stack. Each filter can deny (discard the event), accept (log the event) or remain neutral in which case the event is passed to the next filter in the stack.

Knowning this we can devise a filter stack that only logs events with specific keywords to a file, and all else is discarded.

 <appender name="PERFORMANCE" class="org.apache.log4j.FileAppender">  
      <param name="File" value="${xsp.log.dir}/performance-metrics.log"/>  
      <param name="Append" value="true"/>  
      <param name="Threshold" value="DEBUG"/>  
      <layout class="org.apache.log4j.PatternLayout">  
           <param name="ConversionPattern" value="%d{DATE} %-5p [%t] [%c{1}] %m%n"/>  
      </layout>  
      <filter class="org.apache.log4j.varia.StringMatchFilter">  
           <param name="StringToMatch" value="PERF" />  
           <param name="AcceptOnMatch" value="true"/>  
      </filter>  
      <filter class="org.apache.log4j.varia.DenyAllFilter" />  
 </appender>  

This appender will look for the keyword PERF and log only those events to a file called performance-metrics.log.

Friday, 21 September 2012

Build Scripts

Having worked on a number of large build systems, mostly based on Ant, I am convinced they are not a helpful direction to go in.

The problems with Ant for example are legion:

  • Gardener effect - Great whilst the build system expert is present, but when they move on, it gets dumped in the lap of someone who might not give it as much love.
  • Tendency to grow - Systems growing organically is desirable in Agile development. An overgrown build system is however a pain to maintain.
  • Never Flexible Enough - Especially with Ant, we are constantly finding that the build system does not do exactly what we need it to and changing it is often awkward.

I could go on, but you get the point. What is the alternative?

Maven seems like a helpful option with its industry standard tasks and dependency resolution. I feel though that it is just a better Ant. That strikes me as a good starting point, but is at risk of suffering the same fate as Ant.

What is the alternative? Build it yourself.

  • Everyone can maintain it - But better than that, if you write it in the main development language then everyone can become a build system expert too.
  • Testable - Using the same testing framework as your production code means you can make sure the build system does what it says on the tin.
  • Expressability - How much extra faff do you need in Ant to conditionally compile some code? If you develop a light weight and sensible DSL for the building system, then you can express concepts like that in a lot less complicated, much more readable way.

Tuesday, 28 August 2012

Beanshell Android Source Script

Edit: Updated 2014-08-26

Download

For those using SL4A and Beanshell on their Android, one of the things you need to do if you want to access the Android instance is to know the location of the android.bsh script which is included as part of the Beanshell installation.

The script embedded in the above QR code provides a simple way of working out this location without having to hard code it per script, making your scripts much more portable.

Feel free to use this any way you like, and in particular I would be interested to see any further optimisations to this script as the character count matters in a QR Code based scripts.

Monday, 14 May 2012

UFO: Enemy Unknown on Android

Watching the video for the most recent and promising remake of UFO: Enemy Unknown I decided to see if you can get the game running on Android.

Turns out it is pretty easy.

Emulator

I have tried two emulators for this now:

  • aDosBox Free and works well enough. Performance is not great and the app crashes when you lock the screen which means you can't pause play to do anything else. There is a niggle with the mouse in that the game locks up until you move the mouse sometimes.
  • DosBox Turbo £2.49 and is excellent. The performance is flawless and the mouse trackpad is arguably better than aDosBox (which I thought was pretty good already). The screen can be locked and the game resumed later when you're ready. Worth the money.

Get the game

Find a copy of the DOS version of UFO maybe?

Unzip it to your SDCard. e.g. /sdcard/games/UFO

DosBox conf

aDosBox

If you are using aDosBox, then edit the DosBox configuration file _/mnt/sdcard/app-data/org.hystudio.android.dosbox/.dosbox/dosbox-0.74.conf_ and changing the end of the config file to the following:

  • mount c: /sdcard/games/UFO
  • c:
  • UFO

DosBox Turbo

If you are using DosBox Turbo you can find the Autoexec configuration in the DosBox Manager App. Create a configuration for the game, then long press and select Edit Config. DosBox Settings -> Autoexec. This will give you a text field to enter your autoexec config into.

For more details see the authors website

I would also recommend use the Absolute Touchscreen Mouse. After a little practice I found this to be much quicker than relative mode.

Play

There you go, DosBox will start and load XCOM automatically.

The keyboard and mouse interface works nicely, again not as responsive as a real mouse and keyboard but enough to play.

Dropbox Sync

Finally I went one stage further and used Dropbox synchronisation to ensure that I had my saved game regardless of the system I played on. In particular an app called DropSync is perfect for this job.