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

June 27th, 2009

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.

A refresh start

October 19th, 2008

Well, apparently, my host service screwed up the MySQL database of this blog. I guess it is a good chance to give it a refresh start. I do have some entries backed up and I might re-post some of those old entries if I think they might be useful.

Y-combinator in python

August 3rd, 2008

In my current work, the main stream platform is .NET. I have not invested a lot of time to start to program in C# for .NET, but I am kind of attracted by F#, which is a functional language for .NET that I would like to know more about.

Recently, I purchased a book to know more about F# and I learn some thing that is called “Y-combinator” in functional programming. The F# code for the “Y-combinator” looks like this,

let rec y f x =
  f (y f) x;;

You can apply the “Y-combinator” to some non-recursive functional function to make it recursive. For example,

let fac f = function
  | 0 -> 1
  | n -> n * f (n-1);;

> y fac 5;;
val it : int = 120

I was curious about how to do similar thing in Python. So, I try the following code snippet:

def Y(f):
  def g(x):
    return f(Y(f))(x)
  return g

def fac(f):
  def g(x):
    return 1 if x == 0 else x * f(x-1)
  return g

With these function definitions, “Y(fac)(10)” will give you the correct result 3628800. On the other hand, it is not really easy for me to understand this code as most of my daily programming tasks are in imperative programming style with some more straight forward functional feature. Beside the mathematical way (described in the wiki page) to understand how this work, one way one can see how this work is to see how the code unroll under python interpreter:

Y(fac)(5) -> fac(Y(fac))(5) -> g(5) inside fac with f = Y(fac)

Then the “g(5)” inside fac returns 5 * Y(fac)(4) . The Y(fac)(4) will return 4 * Y(fac)(3) and so on.

It seems there is a lot interesting thing in pure functional world. Hope I will learn more about them soon.

phdfs.py, a ctypes wrapper of hadoop libhdfs for python

May 10th, 2008

I use python and hadoop distributed file system (HDFS) to process large amount of data at work. Instead of using the regular map-reduce mechanism provided by hadoop, I have my home-made map-reduce python engine written using Pyro. It turns out it is quite efficient and sometimes it is much faster than the corresponding streaming code for some simple map-reduce work. For this kind of work, I access the file in HDFS using “hadoop fs -cat” by the unix pipe (popen) in python. It seems to me it might be useful to be able to bypass the somehow ugly unix pipe and “hadoop fs -cat” combination. There already is a SWIG wrapper of python for hdfs. However, I think it will be nice to have ctypes wrapper such that no extra compiling is necessary for installation. I spend a few nights working on such wrapper and hope it will be useful. The results is a single python module that I call “phdfs“. It provides most of the API in the libhdfs. It will be useful if one want to read, write and manipulate the hadoop filesystem with the flexible and powerful python syntax.

You can download the phdfs.py, and try it out yourself. I have not tested all the methods, so YMMV.

Postdocs, “Not Exactly Students, Not Exactly Employees, What are you?”

May 3rd, 2008

My neighbor shows me this article from East Bay Express. Those stories sound very familiar. My personal feeling is that such academic system should be fixed soon. The academic society should give more recognition to postdocs.

As a postdoc, you don’t get those benefit to students. You are not considered as a formal employee. You don’t get any benefit and you are paid low in the name of science. I still remember that I felt so absurd when I was told I could not pay my monthly parking fee by automatic deduction from my paycheck, because I was a “temporary worker” in the school I had being working for a few years.

Well, I can not say that my career is not benefit from my postdoc research. But, I can not say I totally enjoy being treated by the school as “temporary worker” for an indefinitely amount of time. One should treat the real “working horses” in the academic research industry a little better. Without these working horses, there will be no “super-star” in research communities. Anyway, there is not much point for me to complain anymore. Industrial R&D can be fun too.

奇文共賞

April 18th, 2008

在二十一世紀的今天,台灣的某大報系下的海外版的社論出現下列的句子:

『在「百年老店」裡,58歲的馬英九是春秋鼎盛、如日方中的新星。』

『他領導國民黨仆而復起,號召台灣人民、尤其是青年一代,終結了台獨政權,正是「青年創造時代」的典型。』

『愛因斯坦的「相對論」改造了百年間的科學奧秘,而孔孟之道卻歷經千餘年影響世道人心,連馬克斯信徒也不得不信。』

『鼓勵青年學習馬英九,絕不是搞甚麼「偶像祟拜」,更無意要造一座「新神」,而是就近取譬,用大家都看得見的事實,期勉繼往開來的青年世代,好好鑄造自己、鍛鍊自己,無負「青年創造時代」的期望。』

久居國外,我對馬英九了解不算多,也沒有意見。但看了這文章後,不得不想起那連小學裡作文都要以『解救大陸水深火熱同胞』『以三民主義統一中國』的年代。也許,只是也許,某聖君可以不和獨裁磕頭,完成反共復國的大業。這樣就不用每年去拜拜了。

Disclaimer: 我年幼無知的時候為了考試或是混公假,應該也寫了不少奇聞,不過那可是上世紀的歷史共業呀!

奇文原出處之一

我最先發現奇文的地方

My one day trip to Lugradio, San Francsico, 2008

April 12th, 2008

從 PingYeh 那聽到有 Lugradio 這週末在 San Francisco 舉行, 一時興起,決定和老婆女兒告假一天去看看熱鬧。

雖然我在 1993 還是 1994 安裝過 Linux with kernel version 0.97 後,有幾年是非 Linux 不用的人,參加 Linux / open source 社群的活動倒是第一次。台灣的 open source 活動開始熱絡的時候,我人已不再台灣,而人在米國的時候,因為學業和懶的關係也沒有看看過有沒有甚嘛好玩的活動可以參加。所以對我來說,這一次湊熱鬧的感覺是很新鮮的。

Img 4957

我約十一點多到達會場,當場交了米金大洋十塊錢,註了冊,拿了名牌和有贊助商的小禮品的小袋子就進到會場裡逛逛。會場是在 San Francisco Metreon 戲院的頂樓的 CITY VIEW,從前到 Metron 時從來沒聽過有這麼個地方可以辦活動又還有不錯的 city view 的地方。同一時間內,會場會有三場演講進行,你可以選擇比較有興趣的來聽。不想聽的話,就可以逛逛廠商地展示。我隨意聽了幾個演講:其中有 Second Life 的人來說他們 open source 的策略,有 Bungee Connect 的人示範他們的發展平台,有 VMWare 的人展示 Virtual Machine 的 Streaming,也有 Humanized 的 Aza Raskin 討論使用者介面等等。 大部分都還滿有趣,但並沒有在很多技術上比較有深度的討論,大多的討論都在比較形而上的層次。但這樣也好。而從其他聽眾的提問看來,很多參與的人很重視 open source 的發展。

在其他廠商展示方面,我跑去收集了不少 linux 廠商提供的 live CD。而在眾多的廠商展示裡,對我來說最有趣的卻是兩個硬體的廠商。其一是 TI 可以跑 Linux 的單晶片電腦 beagleboard 。看來等 TI 六月出了這東西,我可能會受不了灑點錢買來玩玩。

Img 4954

另外一個有趣的是我終於看到傳說中的 OLPC,的確是很可愛讓人會不住把玩的東西。可惜這有趣的 laptop 只能看看而已。

Img 4955

今天最後一個演講到五點,本來要 skip 晚上的 party,已打算要回家了。在離開會場前,一個對 amateur biotech 有很大興趣的軟體工程師在得知我在一家 biotech 公司工作後,興高采烈的和我討論有沒有甚麼可以在家裡做的 biotech 的計畫,聊了一個小時後才放我回家。

明天 Lugradio 還有一整天的活動,不過我有其他事要做,不能去了。但今天的一日行倒是收穫不少。意外地得了不少在工作上或是家中得不到得 inspiration 和平常不容易看到的 San Francisco City View!!
Img 4960

ad$ense or ad$pam?

April 2nd, 2008

200804022149

I wish I have a little bit more virtual memory so I can convert virtual money to real one.

Using Safari on iPhone to read CHM file

September 23rd, 2007

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.

Tomorrow

July 22nd, 2007

Tomorrow, a new day with new challenges! What a great feeling for entering the next stage of my career! Although I have to make a tough decision, it is indeed time to move on. I am really feeling the excitement of a new environment and a new career path now.