Saturday, October 26, 2013

wget

So a few days ago I was browsing a site

http://www.speedbicycles.ch/showBike.php?enr=442

and I wanted to download the images, but there are like 72 of them and I don't want to right click each one and save them. So I was thinking since I could use wget, but I don't want to download the whole site. I just want the pictures of this sweet looking bike! So I took a look at the URL for the images and sure enough they are all using a simple naming scheme (e.g. speed_001.jpg). So what immediately comes to mind is a bash script with a for loop calling wget to download all the images for me (because you know as programmers we are lazy).
#!/bin/bash
NUM=72

for ((i=1; i<=NUM; i++)) {

    if [ $i -lt 10 ]
    then
        ADD="http://www.speedbicycles.ch/bikes/442/bigPic/speed_00${i}.jpg"
        wget ${ADD}
    else
        ADD="http://www.speedbicycles.ch/bikes/442/bigPic/speed_0${i}.jpg"
        wget ${ADD}
    fi
}

I'm not saying that this is the only way to do this, it's just what came to mind when faced with the problem.

Thursday, September 19, 2013

Getting Alt Text from IMG tags in HTML using Pattern Regex - Java

I was given the task to retrieve alt= from an img tag inside an html string. Here is an example:


<div class="rc_release_list_item_picture">
 <a href="/release/fatboy-slim-and-riva-starr/21683/eat-sleep-rave-repeat/"><img width="87" height="87" border="0" src="http://n.image.weareone.fm/news/_newsgrafiken/2013/_releases/lames/24-07-2013--fatboy-slim-and-riva-starr-eat-sleep-rave-repeat_s.png" alt="Fatboy Slim and Riva Starr - Eat, Sleep, Rave, Repeat" title="Fatboy Slim and Riva Starr - Eat, Sleep, Rave, Repeat" /></a>
</div>


I used the Pattern and Matcher Class provided by the java.util package. The following is a sample of quick main I built to achieve my task:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * User: GKproggy
 */
public class Main
{
    private static String testStrings = "<div class=\"rc_release_list_item_picture\">\n" +
            " <a href=\"/release/fatboy-slim-and-riva-starr/21683/eat-sleep-rave-repeat/\"><img width=\"87\" height=\"87\" border=\"0\" src=\"http://n.image.weareone.fm/news/_newsgrafiken/2013/_releases/lames/24-07-2013--fatboy-slim-and-riva-starr-eat-sleep-rave-repeat_s.png\" alt=\"Fatboy Slim and Riva Starr - Eat, Sleep, Rave, Repeat\" title=\"Fatboy Slim and Riva Starr - Eat, Sleep, Rave, Repeat\" /></a>\n" +
            "</div>";
    public static void main(String[] args)
    {
        String regexPattern = "<img[^>]*alt=[\"]*([\\w\\s-.:\\/,]+)[\"]*[^>]*/>";
        Pattern p = Pattern.compile(regexPattern);
        Matcher m = p.matcher(testStrings);
        if(m.find())
        {
            System.out.println(m.group(1));
        }

    }
}


Hope this was a quick help to get an idea how to use regex to retrieve info you want from raw HTML.

Tuesday, July 30, 2013

GKproggy Video Poker 2.0 is out on Google Play

The new version of 2.0 features:
  • Custom bet option to enter any amount. 
  • Google Play Game Services is available for you to play and unlock achievements as well as post your tournament score to show off your winnings on the leader board. Check it out on Google Play



Monday, February 18, 2013

XNA Visual Studio 2010 Direct3d Issue - Solved

I was trying to run some code in Visual Studio 2010 using XNA framework, but I was required to run using HiDef profile. Running my code in Refresh profile was not working due to some buffer indices issue. Here is a screenshot of error I was seeing after building project and trying to debug when setting profile to HiDef:


I looked all over the internet for error, "No suitable graphics card found. Could not find a Direct3D device that supports the XNA Framework HiDef profile...." I kept finding suggestion to change project property to refresh, but this was not good enough. 

Solution:
Here is how I solved my issue. I was trying to build from a laptop (ASUS) which has two graphic cards, Intel and Nvidia. Turns out something is up with Intel graphics drivers and when you run Visual Studio 2010 it throws the problem above. When I tried to run VS2010 by right clicking on desktop application on icon and choosing "Run with graphics processor" -> "High performance NVIDIA processor", my project compiled using HiDef profile.


Hope this helps you guys looking to run HiDef profile.

Saturday, January 26, 2013

Draw Text on Canvas with Font Size Independent of Screen Resolution - Android

I will show how to draw text with a font size independent of screen resolution on to a canvas.
First we need to create an XML and place it in res/values. Call the file myFontSize. The following is the content of the file


    20sp

Here is a sample use and how to implement.
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class MainActivity extends Activity
{
 Paint paint;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  paint = new Paint();
  View test = new TestView(this);

  setContentView(test);
 }

 public class TestView extends View
 {

  public TestView(Context context)
  {
   super(context);
   setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  }

  @Override
  protected void onDraw(Canvas canvas)
  {
   int size = getResources().getDimensionPixelSize(R.dimen.myFontSize);
   paint.setColor(Color.BLACK);
   paint.setTextSize(size);
   canvas.drawText("HELLOOOOOOOOOOOOOO!", 0, size, paint);
   super.onDraw(canvas);
  }
 }
}

The magic happens on the call to getResources().getDimensionPixelSize(..). This function grabs your set font size (in this case 20sp) and returns it as an appropriate size to give to your paint. Paint is the attributes which will tell the canvas what to do to the provided text.

You should see the following on different resolutions