《新浪微博自动评论软件·设计与实现之UI篇》

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《新浪微博自动评论软件·设计与实现之UI篇》相关的知识,希望对你有一定的参考价值。

任务:编写用户界面

  1. 使用Python中的wxPython对界面进行编写工作

  2. 预计的按钮有:登录,评论,退出

  3. 预计的输入框有:cookie、评论内容、搜索关键字

  4. 预计的单选框有:是否使用关键字搜索

首先,看看我们需要的控件都有哪些,按钮(Button)、单选按钮(RadioButton)、静态文本(StaticText)、可编辑文本(TextCtrl),到WxPythonInAction查看对应的文档,要注意到的是,wxPython和之前玩的MFC不一样,不是先设计界面,再编写代码,而是所有控件的位置都要自己在代码中写好。

还有,因为我设置了延时函数,导致程序在后台运行的时候,UI界面会出现卡死的情况,所以这里要用到一个简单的多线程:

    def _login_gui(self, event):
     ... 

    def login_gui(self,event):
        import thread
        thread.start_new_thread(self._login_gui,(event,))

 剩下的就是一些细节的处理,比如鼠标在单选框切换点击时要使得文本域内容清空等。

完整代码如下,欢迎参考!!

(●‘?‘●)?♥~~~

  1 #!/usr/bin/env python
  2 # -*- coding:utf-8 -*-
  3 __author__ = ziv·chan
  4 __blog__ = http://www.cnblogs.com/ziv-chan/
  5 
  6 
  7 import time
  8 import base64
  9 import rsa
 10 import binascii
 11 import requests
 12 import re
 13 import wx
 14 import urllib
 15 import sys
 16 reload(sys)
 17 
 18 sys.setdefaultencoding(utf-8)
 19 
 20 time1 = time.time()
 21 
 22 # 构造 Request headers
 23 agent = Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/50.0.2661.94 Safari/537.36
 24 headers = {
 25     User-Agent: agent
 26 }
 27 
 28 session = requests.session()
 29 
 30 def get_su(user_name):
 31     username_ = urllib.quote(user_name)     # html字符转义
 32     username = base64.encodestring(username_)[:-1]
 33     return username
 34 
 35 # 预登陆获得 servertime, nonce, pubkey, rsakv
 36 def get_sso_data(user_name):
 37     prelogin_url = http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinaSSOController.preloginCallBack&su=%s&rsakt=mod&client=ssologin.js(v1.4.18) % user_name
 38     sso_data = eval(session.get(prelogin_url).text.replace(sinaSSOController.preloginCallBack,‘‘))
 39     return sso_data
 40 
 41 def get_sp(password, servertime, nonce, pubkey):
 42     weibo_rsa_n = int(pubkey, 16)
 43     key = rsa.PublicKey(weibo_rsa_n, 65537) #创建公钥
 44     message = str(servertime) + \\t + str(nonce) + \\n + str(password) #拼接明文js加密文件中得到
 45     passwd = rsa.encrypt(message, key) #加密
 46     return binascii.b2a_hex(passwd) #将加密信息转换为16进制。
 47 
 48 def login(username, password):
 49     su = get_su(username)
 50     sso_data = get_sso_data(su)
 51     servertime = sso_data["servertime"]
 52     nonce = sso_data[nonce]
 53     rsakv = sso_data["rsakv"]
 54     pubkey = sso_data["pubkey"]
 55     sp = get_sp(password, servertime, nonce, pubkey)
 56 
 57     form_data = {
 58         entry: weibo,
 59         gateway: 1,
 60         from: ‘‘,
 61         savestate: 7,
 62         useticket: 1,
 63         pagerefer: ‘‘,
 64         vsnf: 1,
 65         su: su,
 66         service: miniblog,
 67         servertime: servertime,
 68         nonce: nonce,
 69         pwencode: rsa2,
 70         rsakv: rsakv,
 71         sp: sp,
 72         sr: 1366*768,
 73         encoding: UTF-8,
 74         prelt: ‘‘,
 75         url: http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack,
 76         returntype: META
 77         }
 78 
 79     request_url = http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)
 80     login_page = session.post(request_url, data=form_data, headers=headers)
 81     # login_page.encoding = ‘gbk‘
 82     load_page = login_page.text
 83 
 84     pattern = re.compile(location\\.replace\\([\\‘"](.*?)[\\‘"]\\), re.S)
 85     load_url = re.findall(pattern, load_page)[0]
 86 
 87     login_index = session.get(load_url, headers=headers)
 88     uid_pattern = re.compile("uniqueid":"(.*?)", re.S)
 89     uid = re.findall(uid_pattern, login_index.text)[0] #获得用户uid
 90 
 91     home_page_url = "http://weibo.com/%s/profile?topnav=1&wvr=6&is_all=1" % uid
 92     home_page = session.get(home_page_url, headers=headers)
 93     home_page_pattern = re.compile(<title>(.*?)</title>, re.S)
 94     user_id = re.findall(home_page_pattern, home_page.text)[0]
 95     return u"欢迎%s,登录成功!" % user_id
 96 
 97 def edit_hot_comment(content):
 98     request_url = http://d.weibo.com/aj/v6/comment/add?ajwvr=6&__rnd= + str(int(time.time()*1000))
 99     hot_url = http://d.weibo.com/102803#
100     hot_html = session.get(hot_url, headers=headers).text
101     pa = re.compile(mid=\\\\\\\\"(.*?)\\\\\\\\", re.S)
102     hot_mid = re.findall(pa, hot_html)
103     hot_mid_top5 = hot_mid[0:5]
104     hot_headers = {
105         Host : d.weibo.com,
106         Origin : http://d.weibo.com,
107         Referer : http://d.weibo.com/102803,
108         User-Agent : agent
109     }
110     for i in range(0, 5):
111         hot_form_data = {
112             act : post,
113             mid : hot_mid_top5[i],
114             uid : 3202770263,
115             forward : 0,
116             isroot : 0,
117             content : content,
118             location : page_102803_home,
119             module : scommlist,
120             group_source : ‘‘,
121             tranandcomm : 1,
122             filter_actionlog : 102803_ctg1_99991_-_ctg1_99991,
123             pdetail : 102803,
124             _t : 0
125         }
126         # print hot_mid_top5[i]
127         session.post(request_url, data=hot_form_data, headers=hot_headers)
128         time.sleep(3) #避免请求频繁被限制
129     return u热门批量评论成功!
130 
131 def edit_kw_comment(key_word, content):
132     request_url = http://s.weibo.com/ajax/comment/add?__rnd= + str(int(time.time()*1000))
133     keyword = urllib.quote(key_word.encode(utf8))
134     kw_url = http://s.weibo.com/weibo/%s&Refer=STopic_box % keyword
135     kw_html = session.get(kw_url, headers=headers).text
136     pa = re.compile(mid=\\\\\\\\"(.*?)\\\\\\\\", re.S)
137     kw_mid = re.findall(pa, kw_html)
138     kw_mid_top5 = kw_mid[0:5]
139     kw_headers = {
140         Host : s.weibo.com,
141         Origin : http://s.weibo.com,
142         Referer : kw_url,
143         User-Agent : agent
144     }
145     for i in range(0, 5):
146         kw_form_data = {
147             act : post,
148             mid : kw_mid_top5[i],
149             uid : 3202770263,
150             forward : 0,
151             isroot : 0,
152             content : content,
153             pageid : weibo,
154             _t : 0
155         }
156         # print kw_mid_top5[i]
157         session.post(request_url, data=kw_form_data, headers=kw_headers)
158         time.sleep(3) #避免请求频繁被限制
159 
160     return u关键词批量评论成功!
161 
162 class Bishe(wx.Frame):
163 
164     def __init__(self):
165         wx.Frame.__init__(self, parent=None, id=-1, title=u微博自动评论软件, size=(400, 300))
166         self.panel = wx.Panel(self, -1)
167         self.Center()
168         # self.radio_button = wx.RadioButton(self.panel, -1, u‘是否使用关键词‘, pos=(0, 0))
169         self.radio_button_1 = wx.RadioButton(self.panel, -1, uN, pos=(160, 1))
170         self.radio_button_2 = wx.RadioButton(self.panel, -1, uY, pos=(120, 1))
171         self.static_text_1 = wx.StaticText(self.panel, -1, u是否使用关键词: ,pos=(0,0))
172         self.static_text_2 = wx.StaticText(self.panel, -1, u关键字:, pos=(0, 35))
173         self.static_text_3 = wx.StaticText(self.panel, -1, u内容:, pos=(0, 95))
174         self.static_text_4 = wx.StaticText(self.panel, -1, uauthor : zivchan, pos=(280, 10))
175         self.text_ctrl_1 = wx.TextCtrl(self.panel, -1, pos=(0, 25), size=(250, 100))
176         self.text_ctrl_2 = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, pos=(280, 100), size=(85, 80))
177         self.text_ctrl_3 = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, pos=(1, 120), size=(250, 175))
178         self.button_1 = wx.Button(self.panel, -1, u登录, pos=(280, 50))
179         self.button_2 = wx.Button(self.panel, -1, u评论, pos=(280, 200))
180         self.button_3 = wx.Button(self.panel, -1, u退出, pos=(280, 250))
181         self.Bind(wx.EVT_BUTTON, self.login_gui, self.button_1)
182         self.Bind(wx.EVT_BUTTON, self.comment_gui, self.button_2)
183         self.Bind(wx.EVT_BUTTON, self.exit_gui, self.button_3)
184         self.Bind(wx.EVT_RADIOBUTTON, self.radio_1, self.radio_button_1)
185         self.Bind(wx.EVT_RADIOBUTTON, self.radio_2, self.radio_button_2)
186 
187     def radio_1(self, event):
188         self.radio_button_1.SetValue(True)
189         self.text_ctrl_1.Clear()
190         self.text_ctrl_3.Clear()
191 
192     def radio_2(self, event):
193         self.radio_button_2.SetValue(True)
194         self.text_ctrl_1.Clear()
195         self.text_ctrl_3.Clear()
196 
197     def _login_gui(self, event):
198         username = 18362972928
199         password = xxxxxxxx
200         login_info = login(username, password)
201         self.text_ctrl_2.SetValue(login_info)
202 
203     def _comment_gui(self, event):
204         if self.radio_button_1.GetValue():
205             content = self.text_ctrl_3.GetValue()
206             hot_info = edit_hot_comment(content)
207             self.text_ctrl_3.SetValue(hot_info)
208         elif self.radio_button_2.GetValue():
209             key_word = self.text_ctrl_1.GetValue()
210             kw_content = self.text_ctrl_3.GetValue()
211             kw_info = edit_kw_comment(key_word, kw_content)
212             self.text_ctrl_3.SetValue(kw_info)
213 
214     def login_gui(self,event):
215         import thread
216         thread.start_new_thread(self._login_gui,(event,))
217 
218     def comment_gui(self, event):
219         import thread
220         thread.start_new_thread(self._comment_gui, (event,))
221 
222     def exit_gui(self, event):
223         self.Destroy()
224 
225 
226 if __name__ == "__main__":
227     app = wx.App()
228     frame = Bishe()
229     frame.Show()
230     app.MainLoop()

对了,在处理url时碰到了一个问题:urllib.quote() throws KeyError,参见此页面处理。

最后的最后,我们要使用Pyinstaller将程序打包成可执行文件,操作如下:

下载Pyinstaller2.0,进入解压后的目录,执行:

python pyinstaller.py -F  bi_she_v4.0.py 

这就将 .py 文件打包成单个的可执行文件。

最终,程序长这样:

技术分享

欧了!!

 

以上。

 

到此,已经完成了《新浪微博自动评论软件》的大致设计,后面会陆续带来v2.0...

明天开始写毕业论文,就酱!

 

转载请注明:夜雨寄北丶 » 《新浪微博自动评论软件·设计与实现之UI篇》

以上是关于《新浪微博自动评论软件·设计与实现之UI篇》的主要内容,如果未能解决你的问题,请参考以下文章

《新浪微博自动评论软件·设计与实现之模拟登录篇》

《新浪微博自动评论软件·设计与实现之关键字搜索篇》

新浪微博热门评论爬虫采集

微博API开发

新浪微博UWP UI意见征求

py3 调用微博api实现自动发布评论