Posts Tagged hacking

How to save PDF/Images from Mobile Safari to WebDAV compatible iPhone program?

Unlike most desktop browser, there is no “download” option to save an image, a PDF file, etc., from the Mobile Safari so one can read thoses files offline. Well, this is purely a restriction imposed by Apple for security or whatever other stupid reasons. Fortunately, with the most recent update of the operation that support cut and paste and some third party WebDAV compatible file viewer, there could have a workaround.

I have recently set up a system such that when I found an interesting URL, I can cut and paste the URL to a widget served from my own host such that it will fetch the resource from the URL, and save it to a location that corresponds to the location of a WebDAV service in my web host account. Then, I can setup the WebDAV compatible file viewer (currently, I am using Air Share Pro) to view the file in my WebDAV direcotry. Specially, you can also download the files in the WevDAV server locally for future offline reading. This is extreme useful. Once the file is local, you don’t have to worry if you get wifi, EDGE, or 3G signal, and you might also save some battery power since you don’t have to reload the files from the network when you read a document on and off.

The main caveat is that you probably need your own web hosting service or iDisk that supports WebDAV. Beside that, you will need to some simple CGI/AJAX programming and spend some money to buy a WebDAV compatible file viewer like Air Share Pro. I won’t be surprised that one day there will be an iPhone app that implements something like this using a similar idea in a more polished way. Nevertheless, such application is always at Apple’s mercy. Once Apple opens some policy in the OS allowing sharing data and files between application like an real operation system, all workarounds will be totally useless. Wait, actually, the way I set up getting file to iPhone is not limited for submitting URL from Mobile Safari. Any browser can push the resource from an URL to the WebDAV directory, regradless if an desktop OS supports mounting a WebDAV volumn. Not bad as an quick method to get some useful document in the Internet from a desktop system to iPhone.

Tags: , ,

Using Safari on iPhone to read CHM file

iPhone is a fancy toy with a lot of power but Apple deliberately locks a lot of the potential power. One thing I like to do on an iPhone is to be able to read CHM files. As a weekend project, I setup the tool chain for iPhone following the instructions. Then, I grabbed the source code of chmlib. With some minor modification, I was able to compile the chmlib as an iPhone binary library. That was very encouraging.

This provides a convenient way to make iPhone as a CHM reader. In the chmlib source code distribution, there is an example program that runs as a http-server that serves the content of a CHM as standard web page. The “mobileSafari” has no problem to render the results, but the fonts are usually too small to read and the text is typically rendered too wide such that a lot horizontal scrolling becomes annoyingly necessary.

I decided to combine some python code with the chm_http server from the chmlib source code. I modified the source code of chm_http so it can call python code to modify the HTML code in the CHM file, replacing the original CSS with new setting for reading on small screen. Furthermore, I found it was tedious to start the chm_http from a terminal every time when you want to read a different book. I wrote another small python script that can scan a directory and find all CHM files in the directory to output an index html page. At the end, I was able to use the mobileSafari pointing to the index page and select the book I want to read. The “chm_http” server would start automatically to get the book I like to read.

If you are interested in reading CHM on your iPhone. Get this iphoneCHM.tgz (the file would be upload soon). Copy the “chm_http2“, “rewriteHTML.py“, and “CHMServer” to “/usr/local/bin/” in your iPhone. Change the permission of these files such that you can run all of them. Put some chm files in /var/root/Media/CHM_Ebooks/. Open a terminal in the iPhone or ssh into the iPhone to run “CHMServer”. After that, ask the Safari to open this URL http://127.0.0.1:8000. You should see the links to the CHM files. You can now click on any of them and enjoy a nice reading time.

Tags: ,

Diggs in a box

I have been a fan of the “treemap” algorithm to visualize data with tree-like structure . Finally, I decide to implement one last Friday noon. After a few hours, I had a basic Treemap class in python. When I was testing the class, I found the algorithm can generate some interesting artistic painting effect.

The following is a composition using the Gene Ontology. I guess this is one of the many ways to visualize the so-called Systems Biology. You can also click on the image to see some other randomly generated compositions.

Picture 2

Inspired by Newsmap, I tried to see if I can use the python Treemap class and some javascript to map Diggs to a treemap. So, I spent a few hours in the last weekend making this little toy — Diggs in A Box.

Picture 3


It is rather primitive now and I can think a long TODO list to improve it. Hopefully, I will get some time to improve its functionality and appearance eventually. If you have any suggestions, please leave a message and I hope you find such presentation of data interesting and useful.

Tags: , , ,

Code Snippet for Converting Longitude and Latitude to Google Image Tiles URI

This function in javascript returns the X and Y used to fetch google map tiles by giving the longitude and latitude.

function getXYfromLatLon(lat, lon, zoomLevel) {
  var lon = 180.0 + lon;
  var x = Math.floor( (lon / 360.0) * 131072 ); /* 2^17 = 131072 */
  x >>= zoomLevel;

  var lat = lat / 180.0 * 3.1415926
  var y = Math.PI - 0.5 * Math.log((1+Math.sin(lat))/(1-Math.sin(lat)))
  y = Math.floor( (y / 2 / Math.PI) * 131072 );
  y >>= zoomLevel;
  return new Array(x,y);
}

This function returns the string used to fetch the satellite image tiles by give the X and Y from the getXYfromLatLon().

function getImageTileStrFromXY(x, y, zoomLevel) {
    var quarter2letter = new Array("q","r","t","s");
    var rtnStr = "t"
    for (i=16-zoomLevel; i>=0; i--) {
        var quarter =
        (x & (1 < < i)) >> i | ( ((y & (1 < < i)) >> i) < < 1 );
        rtnStr += quarter2letter[quarter];
    }
    return rtnStr;
}

Simple usage:

<script language="javascript">
var lon = -122;
var lat = 37;
var zl =3;

var XY = getXYfromLatLon(lat, lon, zl);
document.getElementById('img1').src=
"http://mt0.google.com/mt?v=w2.61&x="+XY[0]+"&y="+XY[1]+"&zoom="+zl;

var tileStr = getImageTileStrFromXY(XY[0], XY[1], zl);
document.getElementById('img2').src=
"http://kh0.google.com/kh?&v=20&t="+tileStr;
</script>

Tags: , , ,

Block web ad

We are living in a world where ads are everywhere. With powerful servers, web advertisement is ubiquitous. Unfortunately, I think they are too intrusive most of time. So, it is generally a good idea to disable the online ads. For a free solution, there are several common techniques. First, figure the ad servers’ IP, and use /etc/hosts to send the ad request call into void. Second, use a userContent.css to filter out ads. For example, if you don’t like the Google ads. You can put the following lines in your /etc/hosts (for OS X or other unix).

127.0.0.1   pagead.googlesyndication.com
127.0.0.1   pagead2.googlesyndication.com
127.0.0.1   domains.googlesyndication.com
127.0.0.1   googlesyndication.com
127.0.0.1   www.google-analytics.com
127.0.0.1   partner.googleadservices.com

For OS X, you will need to use the Terminal.app to run the command lookupd -flushcache to re-flush the DNS cache.

Now, with this setup, I am free from all mighty Google ads. You can do the same to block other ad providers. Combined with the userContent.css, I have regain the nice web browsing experience without the interferences from ads.

I also find out that the Safari plugin SafariBlock can block images and flash ads in a convenient way. Moreover, it can also block specific javascripts although you might have to type the URL pointing to the javascript in SafariBlock’s preferences by yourself. For example, you can block the annoying in-text pop-out type ads from Intellitext by putting a new rule in the SafariBlock preferences to block out the URL ‘http://*.intellitxt.com/*.js’. This will block all javascript from Intellitext without disabling javascript totally.

Tags: ,

Building BOOST under OS X

I have not programed in C/C++ for a while. For some reason, I think it is time for me to sharp my C++ knowledge again. I decide to install BOOST in my OS X laptop and start to get familiar with C++ and BOOST again. So, I downloaded the package, configure it and make. As usual, “make” never works the first time.

Tags: ,

Mindstorm NXT, python wrap

I finally decide to get a set of Lego Mindstorm NXT. After built the first model, I was looking for a better way to program the NXT and a way to build bidirectional communication between the NXT and my powerbook. I wrote a simple python wrap to the direct command set. The NXT python class can be downloaded. It is very preliminary but it works. A separated script nxt_download.py can help to download rxe file to the NXT brick. I can now use NBC + my downloading script to program the NXT brick on my powerbook without using any GUI. Is that cool?

Tags: ,

Create DMG Automator

This is a simple bash script to create DMG under Finder.

for f in “$@” do b=$(basename “$f”) d=$(dirname “$f”) hdiutil create -format UDZO -srcfolder “$f” “$d/$b.dmg” done

You can use this simple script combined with Automator such that you can create disk image files by selecting files or directories and using the Automator context menu in Finder.

Automator setup

200607231435

Usage:

200607231435-1

Tags: ,

Fix firewire HD problem on a powerbook

After re-organizing some of my daughter’s video and pictures, it is time to backup my whole user directory. Unfortunately, my PB failed to write to two different firewire HDs. The PB mounted the firewire HDs but it hanged when writing some files (I got such log: Jun 4 23:42:26 ECHO kernel[0]: disk1s3: I/O error.). At beginning, I though it was a problem of my firewire enclosure, but it happened on two external HD. I had to conclude the problem was on my PB. So, it was voodoo time. I zapped the PRAM, reseted NVRAM. Reboot, it behaved much better. At least it only hanged after finished copying my /User. This was the message I got in syslog.log

Jun  4 23:57:40 ECHO kernel[0]: FireWire (OHCI) Apple ID 31 built-in:
handleARxReqInt - ARx Request packet error, status = 0xe00002c7

Well, I unpluged the HD and re-pluged it back. It seemed working fine. I tested to write large files to the other HD, it also worked while it failed consistently before I had reseted the PRAM. So, I guess the problem is solved. I still don’t know what NVRAM has any thing to do with firewire.

Tags:

Compress Canon A620 AVI and DV to H.264 mp4 file

Just a memo for myself.

With mencoder, mplayer, ffmpeg and mp4box, I can finally reduce the size of the video files created from my Canon A620 to reasonable sizes such I can put them in my powerbook without depleting the hard drive space.

This is the script for converting AVI to mp4,

mencoder $1 \
-ovc x264 -nosound -ni -sws 0 -x264encopts \
threads=1:me=2:qp_min=22:\
qp_max=51:i4x4:keyint=120:nocabac:deblock:nob_adapt \
-ofps 29.97 -vop harddup -of rawvideo \
-o $1.264

mplayer -vc dummy -vo null -ao pcm:file=$1.wav $1

ffmpeg -i $1.264 -i $1.wav -y -vn -f mp4 -acodec aac \
-ab 96 -ar 11025 -ac 2 -map 1.0:0.0 $1.aac.mp4

MP4Box -fps 29.97 -add $1.264 -add $1.aac.mp4 -new $1.mp4

rm $1.wav
rm $1.264
rm $1.aac.mp4
touch -r $1 $1.mp4

For convert DV to mp4, I use

ffmpeg -i $1 -y -vcodec h264 -f mp4 -acodec aac \
-deinterlace -b 1280 -aspect 4:3 -s 640x480 -naq \
-parti4x4 -deblock -copyts -keyint_min 120 -h264 1 \
-mv4 1 -bf 2 -coder 1 -parti4x4 -partp4x4 -parti8x8 \
partp8x8 $1.mp4touch -r $1 $1.mp4

The results seem pretty reasonable good, while the sizes are still under control.

Tags: ,