人機介面:讀取「朋友訊息內容」

人機界面

程式列表

# -*- coding: utf-8 -*- import sys import wx import os.path import codecs from datetime import datetime import fbconsole from operator import itemgetter, attrgetter # begin wxGlade: extracode # end wxGlade class MyFacebook(): def __init__(self, messageList): self.messageList = messageList self.messageList.Clear() pass def FB_Login(self): fbconsole.AUTH_SUCCESS_HTML = "<h3>請關閉視窗</h3>" fbconsole.AUTH_SCOPE = ['read_friendlists', 'read_stream', 'publish_checkins', 'publish_actions', 'manage_pages'] fbconsole.authenticate() def FB_Posts(self, who, numOfPosts): totalRecord = 0 for post in fbconsole.iter_pages(fbconsole.get('/' + who + '/posts')): if post.has_key('message'): message_owner_id = post['from']['id'] message = post['message'] self.messageList.Append(message) print '[%s]\t[%s]\t[%s]' % (post['id'], post['created_time'], message) if post['comments'].has_key('data'): for comment in post['comments']['data']: if comment['from']['id']==message_owner_id: message = comment['message'] self.messageList.Append(message) print '[%s]\t[%s]\t[%s]' % (comment['id'], comment['created_time'], message) totalRecord = totalRecord + 1 if totalRecord == numOfPosts: break return totalRecord def FB_Posts(self, who, dateFrom): totalRecord = 0 for post in fbconsole.iter_pages(fbconsole.get('/' + who + '/posts')): if post.has_key('message'): message_owner_id = post['from']['id'] message = post['message'] create_date = datetime.strptime(post['created_time'][:10], '%Y-%m-%d') if create_date < dateFrom: break self.messageList.Append(message) print '[%s]\t[%s]\t[%s]' % (post['id'], post['created_time'], message) if post['comments'].has_key('data'): for comment in post['comments']['data']: if comment['from']['id']==message_owner_id: message = comment['message'] self.messageList.Append(message) print '[%s]\t[%s]\t[%s]' % (comment['id'], comment['created_time'], message) totalRecord = totalRecord + 1 return totalRecord def FB_Logout(self): fbconsole.logout() class MyFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MyFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) # Menu Bar self.frame_Main_menubar = wx.MenuBar() self.menuFacebook = wx.Menu() self.menuQuit = wx.MenuItem(self.menuFacebook, wx.NewId(), u"結束 [&Q]", "", wx.ITEM_NORMAL) self.menuFacebook.AppendItem(self.menuQuit) self.frame_Main_menubar.Append(self.menuFacebook, u"臉書 [&F]") self.SetMenuBar(self.frame_Main_menubar) # Menu Bar end self.frame_Main_statusbar = self.CreateStatusBar(3, wx.ST_SIZEGRIP) self.label_UID = wx.StaticText(self, -1, u"識別碼:") self.text_ctrl_UID = wx.TextCtrl(self, -1, "634954442") self.label_Name = wx.StaticText(self, -1, u"名稱:") self.text_ctrl_Name = wx.TextCtrl(self, -1, "Rich Lee") self.button_LoadFriendList = wx.Button(self, -1, u"載入名單") self.label_Friend = wx.StaticText(self, -1, u"朋友名稱:") self.combo_box_FriendList = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN) self.label_MessageDate = wx.StaticText(self, -1, u"訊息日期:") self.datepicker_ctrl_Message = wx.DatePickerCtrl(self, -1, style=wx.DP_SHOWCENTURY) self.button_LoadMessage = wx.Button(self, -1, u"載入訊息") self.list_box_MessageList = wx.ListBox(self, -1, size=(-1, -1),choices=[], style=wx.LB_SINGLE | wx.LB_HSCROLL | wx.LB_NEEDED_SB) self.__set_properties() self.__do_layout() self.Bind(wx.EVT_MENU, self.menuQuit_onClick, self.menuQuit) self.Bind(wx.EVT_BUTTON, self.LoadFriendList, self.button_LoadFriendList) self.Bind(wx.EVT_BUTTON, self.LoadMessage, self.button_LoadMessage) # end wxGlade self.myFacebook = MyFacebook(self.list_box_MessageList) def __set_properties(self): # begin wxGlade: MyFrame.__set_properties self.SetTitle(u"臉書訊息") self.SetSize((700, 400)) self.frame_Main_statusbar.SetStatusWidths([-1, -1, -1]) # statusbar fields frame_Main_statusbar_fields = ["634954442", "Rich Lee", u"系統訊息"] for i in range(len(frame_Main_statusbar_fields)): self.frame_Main_statusbar.SetStatusText(frame_Main_statusbar_fields[i], i) self.datepicker_ctrl_Message.SetMinSize((160, 38)) # end wxGlade def __do_layout(self): # begin wxGlade: MyFrame.__do_layout sizer_Main = wx.BoxSizer(wx.VERTICAL) grid_sizer_Params = wx.FlexGridSizer(3, 5, 0, 0) grid_sizer_Params.Add(self.label_UID, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.text_ctrl_UID, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.label_Name, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.text_ctrl_Name, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.button_LoadFriendList, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.label_Friend, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.combo_box_FriendList, 0, wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.label_MessageDate, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.datepicker_ctrl_Message, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 0) grid_sizer_Params.Add(self.button_LoadMessage, 0, wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, 0) sizer_Main.Add(grid_sizer_Params, 0, wx.TOP | wx.EXPAND, 0) sizer_Main.Add(self.list_box_MessageList, 0, wx.EXPAND, 0) self.SetSizer(sizer_Main) sizer_Main.Fit(self) self.Layout() # end wxGlade def LoadFriendList(self, event): # wxGlade: MyFrame.<event_handler> fileName = str(self.text_ctrl_UID.GetValue()) + '-' + self.text_ctrl_Name.GetValue() + ".dat" if os.path.isfile(fileName): facebookFile = codecs.open(fileName, "r", encoding='utf-8') friendList = [] for lineText in facebookFile: fields = (lineText).split("|") record = (fields[0], fields[1], fields[2]) friendList.append(record) facebookFile.close() self.data = sorted(friendList, key=itemgetter(1)) self.combo_box_FriendList.Clear() for friend in self.data: self.combo_box_FriendList.Append(friend[1]) else: self.frame_Main_statusbar.SetStatusText(u"檔案不存在!", 1) event.Skip() def LoadMessage(self, event): # wxGlade: MyFrame.<event_handler> friendName = self.combo_box_FriendList.GetValue() if friendName: friendUID = None for friend in self.data: if friendName==friend[1]: friendUID = friend[0] break else: friendUID = 'me' beforeDate = self.datepicker_ctrl_Message.GetValue() messageDate = datetime.strptime(beforeDate.FormatISODate(), '%Y-%m-%d') if friendUID: self.myFacebook.FB_Login() totalMessage = self.myFacebook.FB_Posts(friendUID, messageDate) # self.myFacebook.FB_Logout() self.frame_Main_statusbar.SetStatusText(u"訊息數:" + str(totalMessage), 2) event.Skip() def menuQuit_onClick(self, event): # wxGlade: MyFrame.<event_handler> self.Close() event.Skip() # end of class MyFrame if __name__ == "__main__": app = wx.PySimpleApp(0) wx.InitAllImageHandlers() frame_Main = MyFrame(None, -1, "") app.SetTopWindow(frame_Main) frame_Main.Show() app.MainLoop()