获得豆瓣FM加心歌曲信息的py脚本

2011年4月13日 18:37

update at 2012.2.29: 传送门 to isnowfy大牛的方法

参考各种资料写的,有个小缺点是需要输入有多少页加心歌曲
改改输出格式应该可以用小众软件里介绍的E音乐盒批量下载吧
当时不知道这个软件,只好又写个爬谷歌音乐的,然后手动下载,真是蛋疼

python 2.7 + BeautifulSoupct

# coding: utf-8

from BeautifulSoup import BeautifulSoup
import urllib,urllib2,cookielib

loginurl='http://www.douban.com/accounts/login'
url='http://douban.fm/mine?start=%d&type=liked'
captchaurl='http://www.douban.com/misc/captcha?id=%s&size=s'
email=raw_input('email:')
passwd=raw_input('passwd:')
pages=int(raw_input('pages:'))

cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)

content=urllib2.urlopen(loginurl).read()

postdata=urllib.urlencode({
    'source':'simple',
    'form_email':email,
    'form_password':passwd,
    #'captcha-id':captchaid,
    #'captcha-solution':raw_input('code in pic:'),
    #'user_login':'登录',
})

req = urllib2.Request( url = loginurl, data = postdata)
content=urllib2.urlopen(req).read()

if opener.open(req).geturl() == 'http://www.douban.com/':
    print 'login success!'
    for i in range(pages):
        content=urllib2.urlopen(url % (i*9,)).read()
        soup=BeautifulSoup(str(content))
        for tr in soup.findAll('tr')[1:]:
            td=tr.findAll('td')
            print td[0].string,td[1].span.string #,td[1].a.string # title artist album
else:
    print 'login fault'

 

update:登录方式有变化,douban现在会对频繁登录的ip加验证码,总之此代码有时效性,仅供参考

另附一个从baidu爬歌曲url的脚本,把上面脚本的结果保存成文件,再用这个脚本得到url,最后再用downthemall或迅雷批量下载,多么蛋疼

# coding: utf-8

import sys,os,urllib2,xml
from xml.dom import minidom

lc = sys.getfilesystemencoding()

baseurl0="http://box.zhangmen.baidu.com/x?op=7&mode=1&count=1&listid=&title=%s"
baseurl1="http://box.zhangmen.baidu.com/x?op=12&count=1&mtype=4&title=%s"

f=open('list.txt','r')
for eachline in f.readlines():
    song=eachline#raw_input('Song name:').decode(lc).encode('gbk')
    try:
        data=urllib2.urlopen(baseurl0 % urllib2.quote(song))
        data=data.read().replace('gb2312','utf-8').decode('gbk').encode('utf8')
    except:
        print 'failed on '+eachline
        continue
    doc=minidom.parseString(data) # 丫真挑剔
    names=doc.getElementsByTagName('name')
    if not names:
        print 'failed on '+eachline
        continue
    count=0
   #for i in names:
       #print count,i.firstChild.nodeValue.replace('$',' ').encode(lc)
       #count+=1

    choice=0#int(raw_input('\nyour choice:'))
    data=urllib2.urlopen(baseurl1 % urllib2.quote(names[choice].firstChild.nodeValue.encode('gbk')))
    data=data.read().replace('gb2312','utf-8').decode('gbk').encode('utf8')
    doc=minidom.parseString(data)
    urls=doc.getElementsByTagName('url')

   #print '\n song urls:'
   #for i in urls:
    i=urls[0]
    try:
        t=i.childNodes
        pos=t[0].firstChild.nodeValue.rfind('/')
        prefix=t[0].firstChild.nodeValue[0:pos+1]
        suffix=t[1].firstChild.nodeValue
        print prefix+suffix
    except:
        print 'failed on '+eachline

raw_input('\nEnter to exit ...')

 

评论(3) 阅读(3720)

django中使用css静态文件

2011年4月06日 16:21

使用的是django 1.3

编辑settings.py,设置static路径,静态文件会被收集到这里:

STATIC_ROOT = 'static'

设置static_url,预设了,不用动:

STATIC_URL = '/static/'

设置静态文件路径,静态文件会从这里收集:

STATICFILES_DIRS = (
    'MYAPP/static',
)

在MYAPP下递归建立static/css/foo.css文件和文件夹

模板中可写成: <link href="/static/css/foo.css" rel="stylesheet" type="text/css">

运行manage.py collectstatic进行收集就可以了

ref:http://docs.djangoproject.com/en/dev/howto/static-files/

评论(0) 阅读(3941)

tkinter 小计时器

2011年3月30日 19:48

为了学英语,还是自己写个计时器吧,可以正计时和倒计时.

翻出来以前的tkinter的笔记,又从网上搜了搜Thread的资料,没想到做着做着就花了好几个小时,居然还写得这么长......不过挺好玩儿的,那就贴出来吧

update:

在线程里使用tkMessageBox一直失败,在这里找到了解决方案,现在时间到了可以有弹出窗了

# coding: utf-8
from Tkinter import *
from threading import Thread
from tkMessageBox import showinfo
import time,tkMessageBox

class Timer(Thread):
    over=False
    pause=False
    def __init__(self,func):
        Thread.__init__(self)
        self.func=func
        #self.setDaemon(True)
    def run(self):
        global t,root
        time.sleep(1)
        finish=False
        while not self.over and not finish:
            if not self.pause:
                finish=self.func()
            time.sleep(1)
        if finish:
            #root.focus_force()
            root.event_generate('<<pop>>',when='tail')
        t=None
    def kill(self): self.over=True
    def paus(self): self.pause=True
    def cont(self): self.pause=False

t=None
sec=None
root=Tk()
root.bind('<<pop>>',lambda event=None: showinfo('Oh!','Time is over!'))
e1=StringVar()
e2=StringVar()

def show():
    global e1,e2,sec
    e1.set('%.2d'%(sec/60))
    e2.set('%.2d'%(sec%60))
def down():
    global sec
    if sec:
        sec-=1;show()
        return False
    else: return True
def up():
    global sec
    sec+=1;show()
    return False
    
def st():
    global sec,t
    if t:t.cont();return
    sec=0;show()
    t=Timer(up)
    t.start()

def cd():
    global sec,t
    if t:t.cont();return
    sec=0
    try: sec=int(e1.get())*60
    except Exception:pass
    try: sec+=int(e2.get())
    except Exception:pass
    if not sec: return
    show()
    t=Timer(down)
    t.start()

    pass
def pus():
    global t
    t.paus()

def stp():
    global t,sec
    sec=0;show()
    if t: t.kill()
    t=None

en1 = Entry (root, textvariable = e1 ,width=10 ,justify=RIGHT)
en2 = Entry (root, textvariable = e2 ,width=10)
lb = Label (root, text = ':' )
stbtn = Button(root ,width=10,text= 'start',command =st)
cdbtn = Button(root ,width=10,text= 'countdown',command =cd)
pusbtn = Button(root ,width=10,text= 'pause',command =pus)
stpbtn = Button(root ,width=10,text= 'stop',command =stp)

en1.grid(row = 0 ,column = 0,)
lb .grid(row = 0 ,column = 1)
en2.grid(row = 0 ,column = 2)
stbtn.grid(row = 1 ,column = 0)
cdbtn.grid(row = 1 ,column = 2)
pusbtn.grid(row = 2 ,column = 0)
stpbtn.grid(row = 2 ,column = 2)

root.geometry('+500+400')
root.mainloop ()

 

评论(0) 阅读(2784)

zip my comics

2011年2月28日 09:57

# coding: utf-8
import os,sys,zipfile

def walkdir(d):
    l=os.listdir(d)
    nodir=True
    for t in l:
        p=os.path.join(d,t)
        if os.path.isdir(p):
            nodir=False
            walkdir(p)
    if nodir:
        fn=d+'.zip'
        if os.path.exists(fn):
            print 'Skip:',os.path.basename(fn)
            return
        z=zipfile.ZipFile(fn,'w')
        print 'Get:',os.path.basename(fn)
        for t in l:
            p=os.path.join(d,t)
            z.write(p,t)
        z.close()

if __name__ == '__main__':
    walkdir(os.path.abspath('.'))
    t=raw_input('Enter to end...\n')

 

评论(0) 阅读(1301)

获得 wp Audio Player 文件地址

2011年2月11日 21:47

只是把hacklog中的php代码用python改写了一下

def decode(str):
    codekey='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'
    bins=''
    res=''
    for i in range(len(str)):
        curpos=codekey.find(str[i])
        bin6=('000000'+bin(curpos)[2:])[-6:]
        bins+=bin6

    for i in range(0,len(bins),8):
        bin8=bins[i:i+8]
        res+='%c' % int(bin8,2)

    return res

if __name__ == '__main__':
    while True:
        src=raw_input('code:\n')
        print 'decode:'
        print decode(src)
        

 

评论(0) 阅读(1312)