Posting douban saying using python / 用python發送豆瓣廣播

Finally I got sometime to work on implementing my own python script for posting douban saying, again. This time I used the library written by Leah Culver, and everything went smoothly. It’s just that I have no idea why douban chose to use a different signing algorithm(if a thing as stupid as it is can be called a algorithm) for requesting access token.

終於又有時間再一次捯鼓豆瓣. 這次在 Leah Culver 寫的 library 的基礎上, 終於把python腳本發送豆瓣廣播終於搞定了. 唯一理解不能的事就是豆瓣在獲取access token的時候用了一個莫名其妙的簽名算法(如果這種也能叫做算法的話 = =).

Click on read more to view the actual code of the script.
查看腳本內容請點擊 read more
Continue reading

iptables port forwarding for emule

Suppose you wanna have HighID and open KAD network connection for eMule on a computer in a LAN, and the gateway is running iptables. Just add the rules below to your existing iptables rules and you are done.

# this example is based on the assumption that 
# eth0 is WAN interface and eth1 is LAN interface,
# 192.168.0.2 is the ip address of a computer in LAN,
# 4262 is the tcp port for ed2k network,
# 4272 is the udp port for kad network.

-A PREROUTING -i eth0 -p tcp -m tcp --dport 4262 -j DNAT --to-destination 192.168.0.2:4262
-A PREROUTING -i eth0 -p udp -m udp --dport 4272 -j DNAT --to-destination 192.168.0.2:4272

-A FORWARD -d 192.168.0.2/32 -i eth0 -o eth1 -p tcp --dport 4262 -j ACCEPT
-A FORWARD -d 192.168.0.2/32 -i eth0 -o eth1 -p udp --dport 4272 -j ACCEPT

rtorrent with magnet link support

Yesterday I wanted to download a file but could find only magnet link. Since rtorrent doesn’t support magnet link yet officially, I did some research on google and came cross this great tutorial on how to patch the source from svn and get magnet link support. So here they are, deb packages for libtorrent and rtorrent. These packages were built for my debian squeeze, so I’m not sure if they also work on ubuntu, but if you want you can try.

  libtorrent11_0.12.6-4_i386.deb (371.5 KiB, 2,051 hits)

  rtorrent_0.8.6-2_i386.deb (393.9 KiB, 2,010 hits)

Syncing twitter to digu using python / 用python同步twitter到嘀咕

#!/usr/bin/env python
import base64
import simplejson as json
import os
import sys
import urllib
import urllib2

global twitter_username
global digu_username
global digu_password
twitter_username = 'username'
digu_username = 'username'
digu_password = 'password'

def post_to_digu(msg):
	url = 'http://api.minicloud.com.cn/statuses/update.json'
	data = urllib.urlencode(msg)
	req = urllib2.Request(url, data)
	req.add_header("Authorization", "Basic %s" % base64.encodestring('%s:%s' % (digu_username, digu_password))[:-1])
	handle = urllib2.urlopen(req)

def main():
	path = sys.path[0]
	if (sys.platform[:3] == 'win'):
		path += '\\'
	else:
		path += '/'
	id_file = path + 'last_id'
	last_id = 0
	if os.path.exists(id_file):
		f = open(id_file, 'r')
	        last_id = int(f.read())
        	f.close()
	else:
		f = open(id_file, 'w')
		f.write('0')
		f.close()
	url = 'http://twitter.com/statuses/user_timeline/' + twitter_username + '.json?last_id='
	updates = json.load(urllib.urlopen(url+str(last_id)))
	new_last_id = None
	n = 0
	for update in updates:
		text = update['text']
		id = update['id']
		if (text[0] != '@') and (id > last_id):
			if (new_last_id < id):
				new_last_id = id
			text = urllib.quote(text.encode('utf-8'))
			msg = {'content': text}
                        stack.append(msg)
        while (stack)
                post_to_digu(stack.pop())
	if (new_last_id != None):
		f = open(id_file, 'w')
		f.write(str(new_last_id))

if __name__ == "__main__":
    main()

eva 0.4.921 svn79

從 jaunty 的 repository 裡面安裝的 eva 登錄就崩潰了, 自己從 svn 拉回來編譯了一個, 只編譯了 x86 的因為現在沒有 x64 的機器, 目前發現隱身可見的用戶發來消息的時候會崩潰… 有興趣的同學可以裝上試試

順便 fuck 麻花騰, qq for linux 就這麼停了

  eva_0.4.921svn79-0ubuntu1_i386.deb (2.7 MiB, 1,179 hits)

Sysinfo plugin for xchat2 on linux

It’s my first time writing something in perl, the code is not very efficient and for now it can only display info of cpu, memory, bandwidth and uptime.

# Configuration
$eth = "eth0";

# Script
Xchat::register("Sysinfo", "0.1", "System Information Displayer");
Xchat::print("Sysinfo 0.1 Loaded.");
Xchat::hook_command("uptime", "uptime");
Xchat::hook_command("os", "os");
Xchat::hook_command("bw", "bw");
Xchat::hook_command("cpu", "cpu");
Xchat::hook_command("mem", "mem");

sub uptime {
	$uptime = `cat /proc/uptime | awk '{printf \$1}'`;
	$_day = 60 * 60 * 24;
	$_hour = 60 * 60;
	$_minute = 60;
	$day = int($uptime/$_day);
	$uptime %= $_day;
	$hour = int($uptime/$_hour);
	$uptime %= $_hour;
	$minute = int($uptime/$_minute);
	$uptime %= $_minute;
	$second = int($uptime);
	$uptime = "";
	if($day != 0) {
		$uptime .= $day;
		if($day == 1) {
			$uptime .= " day ";
		} else {
			$uptime .= " days ";
		}
	}
	if($hour != 0) {
		$uptime .= $hour;
		if($hour == 1) {
			$uptime .= " hour ";
		} else {
			$uptime .= " hours ";
		}
	}
	if($minute != 0) {
		$uptime .= $minute;
		if($minute == 1) {
			$uptime .= " minute ";
		} else {
			$uptime .= " minutes ";
		}
	}
	$uptime .= $second;
	if($second == 1) {
		$uptime .= " second";
	} else {
		$uptime .= " seconds";
	}
	Xchat::command("SAY \002[UPTIME]\002 $uptime");
	Xchat::EAT_ALL;
}

sub os {
	$_kernel_release = `uname -r`;
	$_operating_system = `uname -o`;
	chomp($_kernel_release);
	chomp($_operating_system);
	$os = $_operating_system . " " . $_kernel_release;
	Xchat::command("SAY \002[OS]\002 $os");
	Xchat::EAT_ALL;
}

sub bw {
	$old = `cat /proc/net/dev | grep $eth | cut -d ":" -f 2 | tr -s " "| cut -d " " -f 1,9`;
	sleep(1);
	$new = `cat /proc/net/dev | grep $eth | cut -d ":" -f 2 | tr -s " "| cut -d " " -f 1,9`;
	@ary_old = split(/\s/, $old);
	@ary_new = split(/\s/, $new);
	$down = $ary_new[0] - $ary_old[0];
	$up = $ary_new[1] - $ary_old[1];
	$bw = "";
	if($down < 1000) {
		$bw .= $down . " B/s Down ";
	} else {
		$bw .= sprintf("%.02f", $down / 1000) . " KB/s Down ";
	}	
	if($up < 1000) {
		$bw .= $up . " B/s Up";
	} else {
		$bw .= sprintf("%.02f", $up / 1000) . " KB/s Up";
	}
	Xchat::command("SAY \002[BW]\002 $bw");
	Xchat::EAT_ALL;
}

sub cpu {
	$model_name = `cat /proc/cpuinfo | grep "model name" | tail -1 | cut -d ":" -f 2 | tr -s " "`;
	$number_cores = `cat /proc/cpuinfo | grep "model name" -c`;
	$freq = `cat /proc/cpuinfo | grep "cpu MHz" | tail -1 | cut -d ":" -f 2 | tr -s " "`;
	$idle = `vmstat | tail -1 | awk '{printf \$15}'`;
	$load = 100 - $idle;
	chomp($model_name);
	chomp($number_cores);
	chomp($freq);
	$cpuinfo = $number_cores . " CPU";
	if($number_cores > 1) {
		$cpuinfo .= "'s";
	}
	$cpuinfo .= " -" . $model_name;
	$cpuinfo .= " @ " . int($freq) . " MHz" . " (" . $load . "% Load)";
	Xchat::command("SAY \002[CPU]\002 $cpuinfo");
	Xchat:EAT_ALL;
}

sub mem {
	$mem_total = `free -m | grep Mem | awk '{printf \$2}'`;
	$mem_used = `free -m | grep - | awk '{printf \$3}'`;
	$swap_total = `free -m| grep Swap | awk '{printf \$2}'`;
	$swap_used = `free -m| grep Swap | awk '{printf \$3}'`;
	$meminfo = "\002[MEM]\002 " . $mem_used . "/" . $mem_total . " MB \002[SWAP]\002 " . $swap_used . "/" . $swap_total . " MB";
	Xchat::command("SAY $meminfo");
	Xchat::EAT_ALL;
}