Monday, 15 October 2012

A comic download life


download conan all episodes in once






#author: <limkokhole@facebook.com>
import os
import re
import subprocess
from BeautifulSoup import BeautifulSoup
last_failed_episode_index = 4 #change it to failed index if last time failed
url = "http://www.kkkmh.com/manhua/0710/ming-zhen-tan-ke-nan-conan.html"
host = "http://www.kkkmh.com"
def download(folder_name, picture_url):
    file_destination = "/".join([folder_name, picture_url.split("/")[-1]])
    command = "curl -vk -w '%{http_code}' -e '"+picture_url+"' -A 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6' '"+picture_url+"' -o '"+file_destination+"'" #don't follow redirect !!!
    print command
    proc = subprocess.Popen(command, shell=True, bufsize=2048, stdout=subprocess.PIPE, close_fds=True)
    http_code = proc.stdout.read()
    try:
        proc.communicate() #wait for complete
    except Exception, e:
        print "proc exception ", e #[Errno 10] No child processe
    if http_code == "302":
        print "302, retrying... :)"
        download(folder_name, picture_url)
def request_url(url):
    command = "curl -vLk -e '"+url+"' -A 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6' '"+url+"'"
    print command
    proc = subprocess.Popen(command, shell=True, bufsize=2048, stdout=subprocess.PIPE, close_fds=True)
    r = proc.stdout
    return r
r = request_url(url)
soup = BeautifulSoup(r)
episode_list = soup.html.findAll('ul')[-1]
full_episode_list = []
for episode in episode_list:
    full_episode_list.append([episode.find("a").get("title"), "".join([host, episode.find("a").get("href")])])
print "full_episode_list is ", full_episode_list
for index, episode in enumerate(full_episode_list):
    try:
        if index >= last_failed_episode_index:
            title = episode[0]
            folder_name = title.replace("/", "[]").replace("'", "\"")
            if len(folder_name) > 255:
                print "len(folder_name) > 255"
                folder_name = folder_name[:255]
            if not os.path.exists(folder_name):
                os.makedirs(folder_name)
            r = request_url(episode[1]).read()
            regex = re.compile(";pic\[.*?\][ \n]=[ \n]'(.*?)'", re.DOTALL | re.UNICODE | re.IGNORECASE)
            print r
            hex_code_list = regex.findall(r)
            print "hex_code_list is ", hex_code_list
            server_list_url = host + "/common/server.js".join([r.split("/common/server.js")[0].split("\"")[-1], r.split("/common/server.js")[1].split("\"")[0]])
            r = request_url(server_list_url).read()
            server_url = r.split("url:'")[1].split("'")[0]
            print "server_url is ", server_url
            picture_list = []
            for hex_code in hex_code_list:
                picture_list.append("".join([server_url, hex_code.decode("hex")]))
            print "picture_list is ", picture_list
            for picture_url in picture_list:
                download(folder_name, picture_url)
    except Exception, e:
        print "failed to download episode ", index
        print "because of ", e
        break

Monday, 13 August 2012

A routine linux life 2

Recently I force to cleanup all windows 7 and reinstall bt5, so, this time I try kde 32bits. Let's take a look,
pacman game, sometime confuse with pcmanfm file manager.

quite boring? so let's try another window manager, called fluxbox. Many way can start the fluxbox, for example, type kdm or gdm, then choose the fluxbox.

quite nice, and I love mostly many style available.
Double-click the title bar wouldn't max/min, instead of hide the main screen. And I found terminal called urxvt, I like the name. urxvt can config inside ~/.Xdefaults


I also change my fedora desktop, so

what the hell of those Jack-o'-lantern? Ya, I have to call idesk utility for desktop icon, just create ~/.idesktop directory and put icon .lnk file, example gkrellm.lnk:
table Icon
  Caption: gkrellm
  Icon: /root/Desktop/_start:/icon/Jack-O-Lantern.png
  X: 1000
  Y: 950
  Command[0]: gkrellm
  Command[1]: gkrellm ~/.idesktop/gkrellm.lnk
end 

To make the auto startup apss and locate position permanently, edit the  ~/.xinitrc file, my example here:
dbus-launch usr/libexec/gnome-settings-daemon &
sleep 2
gnome-panel &
sleep 2
gnome-volume-control-applet &
idesk &
gkrellm &
pidgin &
nautilus --no-desktop --browser /root/Desktop/t/tx/youtube/sounds  --geometry=500x500+0+500 &
gnome-terminal --geometry=200x22+500+30 &
gnome-terminal --geometry=200x22+500+430 &
exec /usr/bin/startfluxbox



Ya, I also able to open gnome-panel on the same time, juz hide the bottom bar enough. I also trigger gnome-settings-daemon because I want the scrollbar/directory color appearance remain the same, green theme, GNOME-Colors-Wise. sleep required of course. 
No purpose for gkrellm, currently too empty, juz simply put it.
Just saying, gnome-terminal always have a bug, the copy-paste shortcut problem. Simply go to keyboards shortcuts settings, even though "Shift-Ctrl-C and Shift-Ctrl-V" is here already, but you still required to click a few time(new accelerator...)  again. 

time for ktm.




Saturday, 14 July 2012

Manually download baidu song on API way

Manually download baidu song on API way:
1. Open
http://box.zhangmen.baidu.com/dev/api/?tn=gls&titles=poker+face%24%24lady+gaga&callback=jsonplink1342282653123&r=0.02055948238744856 
, "titles=" change to song title, %24%24 also means $$, the next is singer name.
, if it doesn't work, r= should change to any random number, jsonplink's 1342282653123  can change to current javascript UNIX timestamp. The javascript UNIX timestamp can get via alert(new Date().getTime()). Or as a bash way, command "date +%s" and append any 3 digits. Or as a python geek, using "{0:.0f}".format(time.time()*1000)

2: then we would got something like "

jsonplink1342282653123({"errorCode":"0","data":{"songList":[[{"songID":"0","songName":"poker face",
"artistName":"lady gaga","albumID":"0","albumName":"","lrcLink":"http:\/\/zhangmenshiting.baidu.com\
/data2\/lrc\/598626\/598626.lrc","version":"","copy_type":"0","time":"218","songFileId":"","linkCode":
"0","songLink":"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/12758910\/12758910.mp3",
"songShowLink":"","format":"mp3","rate":"64","size":"1677721","copyRight":"4","LRCID":"598626"},
{"songID":"0","songName":"poker face","artistName":"lady gaga","albumID":"0","albumName":"","lrcLink":
"http:\/\/zhangmenshiting.baidu.com\/data2\/lrc\/598626\/598626.lrc","version":"","copy_type":"0",
"time":"","songFileId":"","linkCode":"0","songLink":"http:\/\/mbsky.com.cn\/music\/MUSIC.mp3",
"songShowLink":"","format":"mp3","rate":"","size":"6291456","copyRight":"0","LRCID":"598626"},
{"songID":"0","songName":"poker face","artistName":"lady gaga","albumID":"0","albumName":"","lrcLink":
"http:\/\/zhangmenshiting.baidu.com\/data2\/lrc\/598626\/598626.lrc","version":"","copy_type":"0",
"time":"","songFileId":"","linkCode":"0","songLink":"http:\/\/m2.megalyrics.ru\/m21\/a1\/32726\/335148.mp3",
"songShowLink":"","format":"mp3","rate":"","size":"6291456","copyRight":"0","LRCID":"598626"},{"songID":"0",
"songName":"poker face","artistName":"lady gaga","albumID":"0","albumName":"","lrcLink":
"http:\/\/zhangmenshiting.baidu.com\/data2\/lrc\/598626\/598626.lrc","version":"","copy_type":"0","time":"",
"songFileId":"","linkCode":"0","songLink":"http:\/\/www.naizuud.com\/file\/music_folder\/224.mp3?
stdfrom=3width=1","songShowLink":"","format":"mp3","rate":"","size":"6291456","copyRight":"0","LRCID":
"598626"},{"songID":"0","songName":"poker face","artistName":"lady gaga","albumID":"0","albumName":"",
"lrcLink":"http:\/\/zhangmenshiting.baidu.com\/data2\/lrc\/598626\/598626.lrc","version":"","copy_type":"0",
"time":"","songFileId":"","linkCode":"0","songLink":
"http:\/\/s53253wl1b.wo99.com:8080\/userdir_2297\/4102476\/wo99.com_1328160486252310.mp3","songShowLink":"",
"format":"mp3","rate":"","size":"3145728","copyRight":"0","LRCID":"598626"}]],"p2p":[{"p2pSign":
"f377b0d332c097f32d0d69e3980ba820e7a3fe59","url":
"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/3653665\/3653665.mp3?xcode=013740723af1edfcb033f115eaff28df",
"attr":"4294967295","duration":"238","cat":"0","size":"3893155","bitrate":"128","album_sign1":"4286053527",
"album_sign2":"3070126863","p2p_quku_id":"2914890","p2p_file_id":"3653665","unused1":"0","unused2":"0",
"unused3":"0"}],"xcode":"013740723af1edfcb033f115eaff28df"}})
"


, extract the .mp3, .wma,..whatever make sense song extension. example
"http:\/\/zhangmenshiting.baidu.com\/data2\/music\/10324364\/10324364.mp3"
, remove the back slash "\"
, become http://zhangmenshiting.baidu.com/data2/music/10324364/10324364.mp3

3. Try to download it, if no response or server error, extract "xcode=" value on the end of content we got just now,
"xcode":"013740723af1edfcb033f115eaff28df"}})
become http://zhangmenshiting.baidu.com/data2/music/10324364/10324364.mp3?xcode=013740723af1edfcb033f115eaff28df
Try download it again. Above url and xcode is example only. Normally the xcode would expired.
 
Chinese song example, http://box.zhangmen.baidu.com/dev/api/?tn=gls&titles=%E5%9C%A8%E5%8A%A8%E7%89%A9%E5%9B%AD%E6%95%A3%E6%AD%A5%E6%89%8D%E6%98%AF%E6%AD%A3%E7%BB%8F%E4%BA%8B%24%24my+little+airport&callback=jsonplink1335160684820&r=0.020559488740344856
or just http://box.zhangmen.baidu.com/dev/api/?tn=gls&titles=在动物园散步才是正经事%24%24my+little+airport&callback=jsonplink1335160684820&r=0.020559488740344856
Unknown singer example, portal song, http://box.zhangmen.baidu.com/dev/api/?tn=gls&titles=still+alive$$&callback=jsonplink1335160684820&r=0.020559488740344856
First blog to mention it:)
 

A routine linux life

Sometime application is hang, and you may not enough expertise to analysis under the hood. Although you can command "ps aux", get the pid/name and kill -9 the pid or killall -9 the name, but there's another convenient way to kill certain window locate/click by mouse. That's "xkill" utility. Left-click can kill the window, right-click can cancel it.

I prefer add the command to panel, I didn't want to open terminal and type "xkill" when desktop already on the hang state :) [UPDATE] I use shortcut key <Super+X> nowadays.

So, right-click panel, "Add to Panel..." and Choose "Custom Application Launcher", then insert "xkill" inside both Name and Command text field. Edit the icon, just open "~/.gnome2/panel2.d/default/launchers/xkill.desktop", edit the "Icon[en_US]=IMAGE_LOCATION" part. Default image is "gnome-panel-launcher".