虾米自动签到

罪过罪过。。好久不写。最近用了虾米,歌是挺多,就是觉得网站好乱,下歌也还要积分。我又从来不能坚持登陆的,于是用cron + python写个定时登录脚本。很简单,直接上代码。

#!/usr/bin/python
# encoding:utf-8

import cookielib, urllib2, urllib, StringIO, gzip, os, time
import sys

if len(sys.argv) != 3:
    exit(0)

def get_raw_content(rev):
    if rev.headers.has_key('Content-Encoding'):
        fileobj = StringIO.StringIO()
        fileobj.write(rev.read())
        fileobj.seek(0)
        gzip_file = gzip.GzipFile(fileobj=fileobj)
        content = gzip_file.read()
        fileobj.close()
    else:
        content = rev.read()

    return content

LOG = True
LOG_FILE = os.environ['HOME'] + '/.xiami_signin_log'

SIGNIN_HEADERS = {
    'Accept' : '*/*',
    'Accept-Charset' : 'UTF-8,*;q=0.5',
    'Accept-Encoding' : 'gzip,deflate,sdch',
    'Accept-Language' : 'en-US,en;q=0.8',
    'Connection' : 'keep-alive',
    'Content-Length' : 0,
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Host' : 'www.xiami.com',
    'Origin' : 'http://www.xiami.com',
    'Referer' : 'http://www.xiami.com/',
    'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Ubuntu/10.04 Chromium/11.0.672.2 Chrome/11.0.672.2 Safari/534.20',
    'X-Requested-With' : 'XMLHttpRequest'
}

LOGIN_HEADERS = {
    'Accept' : 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
    'Accept-Charset' : 'UTF-8,*;q=0.5',
    'Accept-Encoding' : 'gzip,deflate,sdch',
    'Accept-Language' : 'en-US,en;q=0.8',
    'Cache-Control' : 'max-age=0',
    'Connection' : 'keep-alive',
    'Content-Type' : 'application/x-www-form-urlencoded',
    'Host' : 'www.xiami.com',
    'Origin' : 'http://www.xiami.com',
    'Referer' : 'http://www.xiami.com/member/logout',
    'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Ubuntu/10.04 Chromium/11.0.672.2 Chrome/11.0.672.2 Safari/534.20',
}

LOGIN_DATA = {
    'done' : 'http://www.xiami.com/',
    'type' : '',
    'email' : sys.argv[1],
    'password' : sys.argv[2],
    'submit' : '登 录'
}
signin_url = 'http://www.xiami.com/task/signin'
login_url = 'http://www.xiami.com/member/login'

TIMEOUT = 100

def do_sign(data):
    success = True
    try:
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar()))
        urllib2.install_opener(opener)
        request1 = urllib2.Request(login_url, data = urllib.urlencode(data), headers = LOGIN_HEADERS)
        rev1 = urllib2.urlopen(request1)

        request2 = urllib2.Request(signin_url, headers = SIGNIN_HEADERS, data = {})
        rev2 = urllib2.urlopen(request2)
        res = get_raw_content(rev2)
    except:
        success = False

    if success and len(res) > 100:
        success = False

    if LOG:
        log_file = open(LOG_FILE, 'a')
        log_file.write('%s %s signin %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), data['email'], str(success)))
        log_file.close()

    if not success:
        time.sleep(TIMEOUT)
        do_sign(data)

do_sign(LOGIN_DATA)

存好后再 crontab -e,输入
0 12 */1 * * python /path-to-xiami_signin.py email password

发表评论?

9 条评论。

  1. 这东西怎么用啊?

  2. 上面那个程序运行一下就可以了,得有python环境

  3. 博主好,这个代码我试过了,确实好使,不过你那个raw content函数,并没有使用呀,是开始的时候测试用的吗?还有我自己研究了半天都没弄明白那个签到是怎么弄的,博主怎么找到是从http://www.xiami.com/task/signin这里request来签到的呀?是用抓包软件试出来的吗?

    • 唔。那个是用来解压缩返回的内容的,因为开始写的时候要看下返回嘛,后来也没删。不是用的系统抓包,就是chromium的inspect element就可以了,在network界面查看。

  4. 哦,我也用的firefox下的插件看的,没看出来什么名堂,估计我用的header可能不对,谢谢博主

  5. 最近的折腾 | xjpvictor's Blog - pingback on 11/26/2011 在 12:54
  6. 你好:运行后打crontab -e
    怎么提示:
    name ‘crontab’ is not defined

    是不是那里不对!

  7. 运行后
    提示:
    Traceback (most recent call last):
    File “crontab.py”, line 24, in
    LOG_FILE = os.environ['HOME'] + ‘/.xiami_signin_log’
    File “C:\Python27\lib\os.py”, line 423, in __getitem__
    return self.data[key.upper()]
    KeyError: ‘HOME’

  8. 博主弄个架设在Google app engine上面的版本吧,那样更方便,而且也支持cron job

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackbacks and Pingbacks: