论坛首页 编程语言技术论坛

写了个开心网查看朋友果实的小程序

浏览 24644 次
精华帖 (1) :: 良好帖 (2) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-03-04   最后修改:2009-03-04
用python2.6写的,用2.5跑可能要改里面json相关部分
其实就是菜园旁边那个按钮的功能,只是没那么多时间老去打开浏览器看那个东西,所以做了这个



#!/usr/bin/env python
# -*- coding:utf-8 -*-
# coded by Daniel King

import re, time, thread, webbrowser
from Tkinter import *
import urllib, urllib2, cookielib, json

class Kaixin(object):

    def __init__(self, app):
        self.app = app
        self.signed_in = False
        self.cj = cookielib.LWPCookieJar()
        try:
           self.cj.revert('kaixin.coockie')
        except:
            None
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)

    def signin(self):
        r = self.opener.open('http://www.kaixin001.com/home/')
        if r.geturl() == 'http://www.kaixin001.com/?flag=1&url=%2Fhome%2F':
            print 'Need logging on!'
            params = {'email':self.app.email, 'password':self.app.passwd, 'remember':1}
            req = urllib2.Request(
                'http://www.kaixin001.com/login/login.php',
                urllib.urlencode(params)
            )
            r = self.opener.open(req)
            if r.geturl() == 'http://www.kaixin001.com/home/':
                print 'Logged on successfully!'
                self.cj.save('kaixin.coockie')
                self.signed_in = True
        else:
            print 'ok!'
            self.signed_in = True

    def monitor(self):
        if not self.signed_in:
            self.signin()
        button = self.app.buttons['monitor']
        if self.signed_in:
            r = self.opener.open('http://www.kaixin001.com/app/app.php?aid=1062&url=garden/index.php')
            m = re.search('var g_verify = "(.+)";', r.read())
            verify = m.group(1)

            self.is_monitoring = True
            button['text'] = u'停止'
            while self.is_monitoring:
                print time.strftime('%y/%m/%d %H:%M:%S')
                req = urllib2.Request(
                    'http://www.kaixin001.com/house/garden/getfriendmature.php',
                    urllib.urlencode({'_':'','verify':verify})
                )

                r = self.opener.open(req)
                self.app.data = json.loads(r.read())
                app.list.delete(0, END)
                for f in self.app.data:
                    self.app.list.insert(END, f['realname'])
                time.sleep(30)

        button['text'] = u'开始'
        print 'Monitoring is stopped.'

    def stop_monitoring(self):
        self.is_monitoring = False

class App(Tk):

    def __init__(self):
        Tk.__init__(self)
        self.data = []
        self.email = ''
        self.passwd = ''
        self.interval = 30
        self.kaixin = Kaixin(self)
        self.font = 'Tahoma 9'
        self.geometry('240x200')
        self.title(u'开心网果实探测仪')

        self.btnpane = PanedWindow(self)
        self.btnpane.pack(expand = 0, side = 'bottom')

        buttons = [[self.on_monitor, u'开始', 'monitor'],
                [self.on_config, u'设置', 'config'],
                [self.on_exit, u'退出', 'exit']]
        self.buttons = {}

        for btn in buttons:
            button = Button(self.btnpane, text = btn[1], font = self.font)
            self.buttons[btn[2]] = button
            button.bind('<ButtonRelease-1>', btn[0])
            self.btnpane.add(button)

        self.list = Listbox(self, font = self.font)
        self.list.pack(expand = 1, fill = 'both', side = 'top')
        self.list.bind('<Double-ButtonRelease-1>', self.on_list_dblclick)

    def on_monitor(self, event):
        button = self.buttons['monitor']
        if button['text'] == u'开始':
            thread.start_new_thread(self.kaixin.monitor, ())
        else:
            self.kaixin.stop_monitoring()

    def on_config(self, event):
        win = ConfigWin(self)

    def on_list_dblclick(self, event):
        s = self.list.curselection()
        if len(s) == 1:
            uid = self.data[int(s[0])][u'uid']
            webbrowser.open('http://www.kaixin001.com/app/app.php?'
                'aid=1062&url=garden/index.php&_lgmode=pri&fuid='+str(uid));

    def on_exit(self, event):
        self.quit()

class ConfigWin(Toplevel):
    def __init__(self, app):
        Toplevel.__init__(self, app)
        self.app = app
        self.title(u'设置')
        Label(self, text = u'电邮:', font = app.font).grid(row = 0, column = 0)
        self.email = Entry(self)
        self.email.grid(row = 0, column = 1)
        Label(self, text = u'密码:', font = app.font).grid(row = 1, column = 0)
        self.passwd = Entry(self, show = '*')
        self.passwd.grid(row = 1, column = 1)
        Label(self, text = u'间隔:', font = app.font).grid(row = 2, column = 0)
        self.interval = Entry(self, text = '30')
        self.interval.grid(row = 2, column = 1)

        self.email.insert(0, app.email)
        self.passwd.insert(0, app.passwd)
        self.interval.insert(0, str(app.interval))

        btnpane = PanedWindow(self)
        btnpane.grid(row = 3, column = 0, columnspan = 2)
        btn_cancel = Button(btnpane, text = u'取消', font = app.font)
        btn_cancel.grid(row = 3, column = 0, columnspan = 2)
        btn_ok = Button(btnpane, text = u'确定', font = app.font)
        btn_ok.grid(row = 3, column = 0, columnspan = 2)
        btnpane.add(btn_cancel)
        btnpane.add(btn_ok)

        btn_ok.bind('<ButtonRelease-1>', self.on_ok)
        btn_cancel.bind('<ButtonRelease-1>', self.on_cancel)

        self.focus_set()
        self.grab_set()
        self.wait_window()

    def on_ok(self, event):
        self.app.email = self.email.get()
        self.app.passwd = self.passwd.get()
        self.app.interval = int(self.interval.get())
        self.destroy()

    def on_cancel(self, event):
        self.destroy()

app = App()
app.mainloop()
   发表时间:2009-03-04  
本来想用py2exe打包给别人用,遇到两个问题
一是msvcr90.dll发布的问题,想不到2.6的官方包是vc9编译的,这个问题好麻烦
二是第一次用tkinter,结果打包压缩了居然还要差不多8M,和wxpython程序py2exe后大小差不多,还不如用更好用的wxpython
0 请登录后投票
   发表时间:2009-03-23  
原来python也是捣鼓这些小东西的呀,我还以为python比ruby高级很多呢
0 请登录后投票
   发表时间:2009-03-28  
我想知道楼主是怎么搞到开心网的api的,如果是普通网页,可以检查源码,但它是flash啊。
0 请登录后投票
   发表时间:2009-03-28   最后修改:2009-03-28
flash也使用http请求啊
firebug很容易看到的

要是我够闲的话,可以把大部分操作的接口都找到,好多事情可以自动完成了, 哎, 昨天就忘了启动车子, 输掉了
0 请登录后投票
   发表时间:2009-03-28  
谢谢楼上,看来,我得好好学习一下 firebug 了。现在我只用它来看一下文档的结构。汗。。。。。。
1 请登录后投票
   发表时间:2009-03-30  
我用python 2.5.4,用了py.json,需要修改什么才能使用呢?
0 请登录后投票
   发表时间:2009-04-01  
就是解析json,你得懂我的源码就知道怎么用了,其实很简单
0 请登录后投票
   发表时间:2009-04-15  
                for f in self.app.data:   
                    self.app.list.insert(END, f['realname'])   


有个bug,没有结果的时候会出异常
0 请登录后投票
   发表时间:2009-04-17   最后修改:2009-04-17
何必做界面,后台偷偷跑多好,我挂了crontab,10分钟偷一轮,哈哈。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics