## LE Mensa

This week I released my first OS X Dashboard Widget.
It’s called ‘LE Mensa’ and it displays all menues for all cafeterias in Leipzig, which are part of the student union.
If you want to give it a try then visit the project page: http://stinfwww.informatik.uni-leipzig.de/~mai03fln/leMensa/
Today I updated it to version 0.15 !!

It works this way:
1. I wrote a python script which extracts information from the student union homepage and transform it into xml.
2. A .php script can call this .py script and give back those infos.
3. The widget was written in Dashcode with jQuery as an additional package. So it’s mainly javascript, some html and css. The widget now calls this php script, retrieves the xml and displays it in a (hopefully) pretty way.

This is how it looks:

## Python and building xml’s

I’m working on a little Project called “leMensa”. Maybe later I’ll tell you more about it.
While working on it I needed to build some xml documents with pyxml.
Here’s what I found. A little help from Matt Croydon was all I needed to know.

http://www.postneo.com/projects/pyxml/

## fast calculation of fibonacci numbers in python

I needed this for a challenge. If you need to reuse the values, it’s much faster with the memo.

```#!/usr/bin/python
memo = {0:0, 1:1}
def fib(n):
if not n in memo:
memo[n] = fib(n-1) + fib(n-2)
return memo[n]
print fib(100)
```
```\$./fib.py
354224848179261915075
```

## Atbash Cipher

Just al little python script for preforming a Atbash encryption (roman alphabet). This type of cipher is older than Caesar cipher
Maybe you can use it.
-> Wikipedia article

```clear="abcdefghijklmnopqrstuvwxyz 1234567890"
c=""
for i in sys.argv[1].lower():
index=clear.index(i)
c=c+clear[abs(len(clear)-index-1)%len(clear)]
print c
```

Example:

```\$ ./atbash.sh "This Is A Little Test"
r32sk2sk0kz2rrz6kr6sr
\$ ./atbash.sh "\$(./atbash.sh "This Is A Little Test")"
this is a little test
```

Here’s are 2 little scripts I wrote today for encoding/decoding XOR encrypted text.

Script 1 (HexXorEncode.py) takes a string/text and a integer key value. Then it preforms and xor encryption on the string with the given key.

```#!/usr/bin/python
import sys
#Copyleft m.puchalla 2010
#Preforms a XOR Encoding with a specific string and returns a hexadecimal representation of it

if(len(sys.argv)!=3):
print "Usage:",sys.argv[0]," [String] [integer key]"

def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and  "0" ) or ( baseN(num // b, b).lstrip("0") + numerals[num % b])

s=sys.argv[1]
key=baseN(int(sys.argv[2]),2)
sol=""
for i in range(0,len(s)):
sol=sol+baseN(int(str(baseN(ord(s[i]),2)),2)^int(key,2),16)
print sol
```

Example:

```\$ ./HexXorEncode.py "this is a little test" 25
6d71706a39706a39783975706d6d757c396d7c6a6d
```

Here’s script number 2 (HexXorDecode.py) which simply reverses the process.

```#!/usr/bin/python
import sys
#Copyleft m.puchalla 2010
#Decrypt a hex xor coded string with a key

if(len(sys.argv)<3):
print "Usage:",sys.argv[0]," [XOR Code String] [integer key]"
sys.exit(1)

def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
return ((num == 0) and  "0" ) or ( baseN(num // b, b).lstrip("0") + numerals[num % b])

s=sys.argv[1]
key=baseN(int(sys.argv[2]),2)
sol=""
for i in xrange(0,len(s)-1,2):
sol=sol+chr(int(str(baseN(int(s[i]+s[i+1],16),2)),2)^int(key,2))
print sol
```

Example:

```\$ ./HexXorDecode.py "6d71706a39706a39783975706d6d757c396d7c6a6d" 25
this is a little test
```

If you like it, use it.

## blog down

As you may have noticed my blog is down and what you see here is the veryold version of it.
I had problems with the provider of my server and he shut down my account.
I’ll now try to bring back my blog with all the latest posts. At least I have a backup ðŸ˜‰

## Howto disable iPhoto autostart when connecting an iPhone ?

Finally I found an awnser:
NOTE: This is only for Mac, I don’t know how to do it on a Windows machine.

2. close the starting iPhoto for the last Time ðŸ˜‰
3. open up the “Image Capture” (eng) / “Digitale Bilder” (de) Application
4. select your iPhone and choose “No Application” in the DropDown List.
5. close it and enjoy connecting your iPhone without the annoying autostart

## test proxy speed with bash and wget

I need to test the speed of some proxy server. Here’s a little script on how I achieved this.
I have a text-file ‘proxy.list’ which looks like: (I took out the last two digits of the ip).

```...[lot's of ip's]...
193.196.*.*:3124       Germany
143.205.*.*:3127      Austria
64.161.*.*:3128         United States
.....
```

Here is the script which will run through the list of all proxy and will download 5 test pages from a specific site. Then it will determine the period of time which is needed for execution. It’s create/append to a file ‘time.list’ which will contain the needed Information to determine the best proxies. You also need to create a subdirectory called ‘raw_proxy’ where the raw html code is save that you retrieve from the proxies. The files are named ”raw_proxy/\$ip.\$port.\$i.tmp’ where \$i is the i.th test page you downloaded. I need to keep those files to determine if the proxy send me the right file or e.g. a login page .

```#!/bin/bash
size=\$(cat proxy.list | wc -l)
do
#determine the first parameter (IP:Port)
ad=\$(echo \$proxy | awk '{print \$1}')
#set and export the proxy settings
http_proxy=\$ip:\$port && HTTP_PROXY=\$http_proxy && export http_proxy HTTP_PROXY
#save start timestamp
start=\$(date +%s)
#download 5 pages #(yes I know 'seq' but I'm on Mac and I needed a sth. quick&dirty)
for i in \$(echo "1 2 3 4 5")
do
#use wget to retrive the page. We want to try 1 time and set some specific timeouts. + we force to use a Mozilla User agent to hide that we are using wget.
wget -O "raw_proxy/\$ip.\$port.\$i.tmp" --tries=1 --dns-timeout=10 --connect-timeout=8 --read-timeout=15 -U "Mozilla/5.0 (compatible; Konqueror/3.2; Linux)" "http://www.yourTestPage.com/\$i.txt" &> /dev/null
done
#save end timestamp
end=\$(date +%s)
#calculate the difference
diff=\$(( end - start ))
#append this info to time.list
echo -e "\$ip:\$port\t\$diff" >> time.list
#to have a nice and shiny output I use figlet, this is optional, if you don't want it comment out next 3 lines or just remove ' | figlet'
clear
echo "PC: #"\$size" - "\$diff"s" | figlet
sleep 1
size=\$(( size-1 ))
done < proxy.list
```

If you used figlet your output looks like this:

```
____   ____       _  _    __  _____ _           ____   ___
|  _ \ / ___|_   _| || |_ / /_|___ // |         |___ \ / _ \ ___
| |_) | |   (_) |_  ..  _| '_ \ |_ \| |  _____    __) | | | / __|
|  __/| |___ _  |_      _| (_) |__) | | |_____|  / __/| |_| \__ \
|_|    \____(_)   |_||_|  \___/____/|_|         |_____|\___/|___/
```

It shows how much proxies need to be checked and shows the last execution time.

After the script has finished you need to get a list of which proxy was best.
This is the command line which evaluates everything and gives me back a list of ip's sorted by access time. It also removes all proxies where the downloaded page had a size of 0B.

```#command line to list proxy with lowest time to download
clear && while read line; do ip=\${line% *};time=\$(echo \$line | awk '{print \$2}');ip=\${ip%:*};echo -e \$ip"\t"\$time"\t"\$(ls -alshr raw_proxy/ | grep 1.tmp | grep \$ip | awk '{print \$6}'); done < <(tr -s ' ' < time.list | sort -n -r -k2 | cut -d' ' -f3) | grep -v "0B"
```

This is the output:

```201.22.*.*	43	52K
196.213.*.*	43	13K
....
147.102.*.*	1	2,1K
132.227.*.*	1	2,1K
....
130.75.*.*	1	52K
```

If you know the filesize the you can append a

` | grep "52K"`

to the last command to show only files which have the right size.
This is it ðŸ˜‰

I know that out there are better and fast implementations to do this but ...
but it was fun

## using figlet to pimp your scripts

I just read about the unix tool ‘figlet’. One can create some kind of ascii art text.
Let’t dive into it quickly:

```cbo\$ figlist | while read font;
do figlet -f \$font "\$font";
echo \$font;
done

Figlet fonts in this directory:

#####    ##   #    # #    # ###### #####
#    #  #  #  ##   # ##   # #      #    #
#####  #    # # #  # # #  # #####  #    #
#    # ###### #  # # #  # # #      #####
#    # #    # #   ## #   ## #      #   #
#####  #    # #    # #    # ###### #    #

banner
_     _
| |   (_)
| |__  _  __ _
| '_ \| |/ _` |
| |_) | | (_| |
|_.__/|_|\__, |
__/ |
|___/
big

_|        _|                      _|
_|_|_|    _|    _|_|      _|_|_|  _|  _|
_|    _|  _|  _|    _|  _|        _|_|
_|    _|  _|  _|    _|  _|        _|  _|
_|_|_|    _|    _|_|      _|_|_|  _|    _|

block
_   _   _   _   _   _
/ \ / \ / \ / \ / \ / \
( b | u | b | b | l | e )
\_/ \_/ \_/ \_/ \_/ \_/
bubble
+-+-+-+-+-+-+-+
|d|i|g|i|t|a|l|
+-+-+-+-+-+-+-+
digital
_   _            _
| |_(_)_ ____   _(_)
| __| | '__\ \ / / |
| |_| | |   \ V /| |
\__|_|_|    \_/ |_|

ivrit

_/
_/    _/_/      _/_/_/  _/_/_/
_/  _/_/_/_/  _/    _/  _/    _/
_/  _/        _/    _/  _/    _/
_/    _/_/_/    _/_/_/  _/    _/

lean

._ _ o._ o
| | ||| ||

mini
mnemonic
mnemonic

o
,   __   ,_        _ _|_
/ \_/    /  |  |  |/ \_|
\/ \___/   |_/|_/|__/ |_/
/|
\|
script
|               |
__| __ \   _` |  _` |  _ \\ \  \   /
\__ \ | | | (   | (   | (   |\ \  \ /
____/_| |_|\__,_|\__,_|\___/  \_/\_/

__            __
_____/ /___ _____  / /_
/ ___/ / __ `/ __ \/ __/
(__  ) / /_/ / / / / /_
/____/_/\__,_/_/ /_/\__/

slant
_ _
____ __  __ _| | |
(_-< '  \/ _` | | |
/__/_|_|_\__,_|_|_|

small

,           ,   _   ,_  o    _|_
/ \_/|/|/|  / \_/   /  | | |/\_|
\/  | | |_/ \/ \__/   |/|/|_/ |_/
(|
smscript
|              |
(_-<  ` \ (_-<   \   _` |  _` |  _ \\ \  \ /
___/_|_|_|___/_| _|\__,_|\__,_|\___/ \_/\_/

__          __
___ __ _  ___ / /__ ____  / /_
(_-
You can pipe any string into figlet
cbo\$while [ 1 ];
do
clear
date +%r | figlet
sleep 1
done

___ _____  ____   ___    ____  ___
/ _ \___ /_|___ \ / _ \ _| ___|/ _ \   _ __  _ __ ___
| | | ||_ (_) __) | | | (_)___ \ (_) | | '_ \| '_ ` _ \
| |_| |__) | / __/| |_| |_ ___) \__, | | |_) | | | | | |
\___/____(_)_____|\___/(_)____/  /_/  | .__/|_| |_| |_|
|_|

```