Awesome WM - weather widget
While I was searching for weather widget to Awesome WM all what I found was insufficient to my idea, so I’ve decided to write my own one.
The thing was that I can’t write anything in Lua language. Hopefully there is solution. You can write the core in another language and just run in from Lua as CLI script and get the output. And this is exactly what I did.
Python part (expects country ID as param):
#!/usr/bin/python
# -*- encoding: UTF-8 -*-
from json import loads
from urllib2 import urlopen
import sys
base_url = “http://api.openweathermap.org/data/2.5/weather?id=%s&units=metric”
data = loads(urlopen(base_url % sys.argv[1]).read())
print “%s %s” % (int(data[“main”][“temp”]), data[“weather”][0][“main”])
And rc.lua part (passes country ID to python script):
weatherwidget = widget({ type = “textbox” })
weatherwidget.text = ” — “
weatherwidgettimer = timer({ timeout = 120 })
weatherwidgettimer:add_signal(“timeout”,
function()
temp = assert(io.popen(“/home/grafa/.config/awesome/weather.py 3067696”, “r”))
weatherwidget.text = “<span color=’#ff892c’>”..temp:read(“*l”)..”</span>”
temp:close()
end
)
weatherwidgettimer:start()
Result (the first orange part):

Mount backup created by DD
Let’s assume we created backup file with dd command and now, we would like to open that backup and read the files. The solution is simple…. just mount the *.dd file with loop option.
But…it’s not so simple as it can seems to be. We have to add another option and it’s offset =xxx. Hopefully the offset number we can get when we run:
fdisk -l the_file.dd
and the result is in the partition table in column “Start”. In my case it’s 8192. If we multiply this number by 512 we will get the offset and successfully run:
sudo mount -o loop,offset=4194304 -t vfat htc_bu.dd /media/usb/
PostgreSQL: Find nearest locations
Let’s assume following:
You have a database of locations, for example list of all zip codes in your country including GPS positions and you would like to get nearest 10 zip codes to specific one. Or you have a GPS of your house. you have a list of all bars in your city including theirs GPS positions and you would like to get nearest 10 ones.
You can get these results quite simple with Postgres database.
I’m going to demostrate this on zip codes:
Firstly we have to load following extension into database:
CREATE EXTENSION cude;
CREATE EXTENSION earthdistance;
If you don’t have these extensions installed, you can simply intall them:
sudo apt-get install postgresql-contrib-9.1
Now let’s create the table:
CREATE TABLE zip_code (
id serial NOT NULL,
zip_code integer NOT NULL,
region character varying(100) NOT NULL,
latitude numeric(6,4) NOT NULL,
longitude numeric(6,4) NOT NULL,
earth_coords cube,
CONSTRAINT zipcode_pkey PRIMARY KEY (id)
);
insert some data:
INSERT INTO zip_code (zip_code, region, latitude, longitude) VALUES (10200, ‘Prague 1’, 50.0833, 14.4275)
…
and finally create the index:
CREATE INDEX ON zip_code USING GIST (earth_coords)
Now you have the table with data and index on the column, which you will need for conditioning. But you have to do one more thing:
UPDATE zip_code SET earth_coords = ll_to_earth(latitude, longitude);
This will fill the earth_coords column with GPS positions converted into earth data type for each record in the table.
Now you have to know GPS position of the zip code which you would like get nearest zip codes to. Let’s say the GPS position is:
50.0794, 14.43400
So you can run something like:
SELECT * FROM zipcode WHERE earth_box(ll_to_earth(50.0794, 14.43400), 5000) @ earth_coords LIMIT 10
which will select all zip codes from your current one in 5 km radius (that’s why 5000).
On my local machine (Intel i5) with 6.8k records the query takes 142ms.
Installing Google Earth on 64 Debian
Basically you have 2 options. First one is a bit dirty, because it uses ia32-libs package, which contains tons of 32 bit libs. Second one is how it should be done (according to me).
So 1st option:
sudo apt-get install googlearth-package ia32-libs
make-googleearth-package
sudo dpkg -i GENERATED_PACKAGE_BY_PREV_COMMAND.deb
Second option is about to add i386 architecture to your repository list, then install needed libs.
sudo apt-get install lsb-core
sudo dpkg —add-architecture i386
cd /tmp
wget -c http://goo.gl/hZQ6K -O google-earth-stable_amd64.deb
sudo dpkg -i google-earth-stable_amd64.deb
The last command will probably fail, because it will demands a few 32 bit libs + ia32-libs package. So install each 32 lib but do not install the ia32-libs one. It’s just dependency thing. Instead of that run this:
sudo dpkg —force-all -i google-earth-stable_amd64.deb
It will instll the Google Earth regardless ia32-libs package error.
Here is the proof:

Peace.
Unaccent function in Python
If you need to remove accent from text, for example for comparsion with another text (fulltext search solution), there is simple way how to do that:
#!/usr/bin/python
# -*- encoding: UTF-8 -*-
import unicodedata
def unaccent(text):
s = text.decode(“utf-8”)
s = unicodedata.normalize(“NFKD”, s)
s = “”.join(c for c in s if ord(c) < 127)
return s
print unaccent(“Výhry a výherci”)
This script can be used for either in PostgreSQL as an function
Google Chrome: Hardware acceleration
If your browser don’t want to accelerate graphics, you can force him to do so.
Solutions is simple. Try to run your browser with next two flags
google-chrome —enable-webgl —ignore-gpu-blacklist
Now open your browser and go to:
about:gpu
and you should see this:

If those flags did the work, you can tell the browser to make this settings default. Open this URL:
about:flags
and find out this:
Override software rendering list
and then click to Enable it.
Enjoy your hardware accelerated browser for example with Google MapsGL.
PostgreSQL: postgres user password
Each time you install PostgresSQL database server, you will need access to that server under some user with full access (alike on MySQL root user).
PostgreSQL has such user, its “postgres”. But server default configuration is not friendly to you, so u cannot login into database with this user. It requires a few changes.
First of all we need to change postgres’s password. We can do it simply by login under postgres user into system, then run database console and change that password. Database console will not require password for login for now.
sudo su - postgres
psql
alter user postgres with password ‘our password’;
We configured postgres user. Its time to say to PostgreSQL, that user postgres, trying to login from this machine, will be authenticated by password, which is in md5 (that password was crypted by database automaticaly).
We need edit this conf file: pg_hba.conf. So it should be something like this:
sudo vim /etc/postgresql/9.1/main/pg_hba.conf
Then we need to put this line into that file:
local all postgres md5
It says that user “postgres” from local machine will be authenticated via password which is in md5. Just what we need.
Last step is to restart postgres, because we changed config file.
sudo /etc/init.d/postgres restart
Done.
Python GData: YouTube
GData is project that provides API interface to most of Google Services (YouTube, Drive, Search, …).
However…
It’s really hard to find some specific examples for GData for YouTube. I tried to list all of my playlists and for each one all videos. With GData API Doc and a lot of trying i produced this. Hope it will help to someone else.
from gdata.youtube.service import YouTubeService
service = YouTubeService()
service.email = “xxx@xxx.com”
service.password = “xxx”
service.ProgrammaticLogin()
playlist_feed = service.GetYouTubePlaylistFeed()
for e in playlist_feed.entry:
print e.title.text
video_feed = service.GetYouTubePlaylistVideoFeed(e.feed_link[0].href)
for v in video_feed.entry:
print “\t %s” % v.title.text
Pastebin version: http://pastebin.com/xZXbEPz2.
EDIT: I established GitHub repository: https://github.com/grafa/pyou.
FTP (proftpd) and iptables - can’t execute “directory listing”
FTP needs 2 ports for working properly + for passive mode port range. One is for commands, second for data transfer and for passive mode the port range, which you can specify in config file and than create rule with ipconfig to accept traffic on these ports.
First u need to specify port range for passive mode in proftpd configuration. Config file is placed at
/etc/proftpd/proftpd.conf
Just add this line to file
PassivePorts 10000 11000
Than restart ftp daemon
sudo /etc/init.d/proftpd restart
As last you need to add all rules for FTP to iptables:
iptables -A INPUT -p tcp —dport 20:21 -j ACCEPT
iptables -A OUTPUT -p tcp —sport 20:21 -j ACCEPT
iptables -A INPUT -p udp —dport 20:21 -j ACCEPT
iptables -A OUTPUT -p udp —sport 20:21 -j ACCEPT
iptables -A INPUT -p tcp —dport 10000:11000 -j ACCEPT
iptables -A OUTPUT -p tcp —sport 10000:11000 -j ACCEPT
iptables -A INPUT -p udp —dport 10000:11000 -j ACCEPT
iptables -A OUTPUT -p udp —sport 10000:11000 -j ACCEPT