2013.10.18 - final
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import wx
import wx.html2
import urllib2
import re
import thread
import time
news_url = u""
#----------------------------------------------------------------------
class ReaderPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.current = news_hosts[8]
self.frame = self.GetTopLevelParent()
self.titleBase = self.frame.GetTitle()
sizer = wx.BoxSizer(wx.VERTICAL)
grpSizer = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(self, label=u"뉴스카테고리 : ")
grpSizer.Add(st1, 0, wx.CENTER|wx.ALL, 2)
btn = wx.Button(self, -1, u"새로고침", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnReload, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"헤드라인", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo0, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"전체뉴스", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo1, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"경제", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo2, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"정치", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo3, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"사회", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo4, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"국제", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo5, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"기업경영", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo6, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"증권", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo7, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"부동산", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo8, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"문화연예", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo9, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"패션", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo10, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"스포츠", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo11, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"게임", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo12, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"오피니언", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo13, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
sizer.Add(grpSizer, 0, wx.EXPAND)
sizer.Add((-1, 10))
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
#btn = wx.Button(self, -1, u"Open", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#btn = wx.Button(self, -1, u"<--", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoBack, btn)
#btn = wx.Button(self, -1, u"-->", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoForward, btn)
#btn = wx.Button(self, -1, u"Stop", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#btn = wx.Button(self, -1, u"Refresh", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
txt = wx.StaticText(self, -1, u"Location:")
btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)
self.location = wx.ComboBox(
self, -1, "", style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.location.AppendItems([u"http://www.mk.co.kr/"])
self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)
self.location.Bind(wx.EVT_TEXT_ENTER, self.OnLocationEnter)
btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)
sizer.Add(btnSizer, 0, wx.EXPAND)
sizer.Add((-1, 10))
self.wv = wx.html2.WebView.New(self)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.OnWebViewNavigating, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATED, self.OnWebViewNavigated, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnWebViewLoaded, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_TITLE_CHANGED, self.OnWebViewTitleChanged, self.wv)
sizer.Add(self.wv, 1, wx.EXPAND)
self.SetSizer(sizer)
#self.wv.LoadURL(self.current)
self.wv.SetPage(news_rss[8], news_hosts[8])
# WebView events
def OnWebViewNavigating(self, evt):
# this event happens prior to trying to get a resource
print u"OnWebViewNavigating URL is : %s" % evt.GetURL()
#wx.MessageBox(evt.GetURL(), u"Target URL is...", wx.OK|wx.ICON_INFORMATION, self)
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if (u"http://mk.co.kr.feedsportal.com/c/" in evt.GetURL()) and (not u"/index.rss" in evt.GetURL()):
news_url = evt.GetURL()
news_script = SetPage(evt.GetURL(), u"MK")
self.wv.SetPage(u"<html><div>" + news_script + u"</div></html>", news_url)
evt.Veto()
self.current = news_url
self.location.SetString(0, self.current)
elif (u"http://mk.co.kr.feedsportal.com/c/" in evt.GetURL()) and (u"/index.rss" in evt.GetURL()):
news_url = evt.GetURL()
news_script = SetPage(evt.GetURL(), u"TITLE")
self.wv.SetPage(news_script, news_url)
evt.Veto()
self.current = news_url
self.location.SetString(0, self.current)
def OnWebViewNavigated(self, evt):
self.frame.SetStatusText(u"Loading %s..." % evt.GetURL())
def OnTestButton(self, evt):
pass
def OnWebViewLoaded(self, evt):
# The full document has loaded
self.current = evt.GetURL()
if self.current == u"http:///":
self.current = self.location.GetString(0)
self.location.SetValue(self.location.GetString(0))
else:
self.location.SetValue(self.current)
self.frame.SetStatusText(u"Loaded")
#self.wv.RunScript("""alert('""" + self.current + """');""")
#self.wv.RunScript("""
#//call logIn();
#//document.getElementsByName('q')[0].value="hist";
#//document.getElementById('openididentifier').value="ident";
#""")
#self.wv.SetPage(self, """<html>하하하</html>""", """http://www.naver.com""")
def OnWebViewTitleChanged(self, evt):
# Set the frame's title to include the document's title
self.frame.SetTitle("%s -- %s" % (self.titleBase, evt.GetString()))
# Control bar events
def OnLocationSelect(self, evt):
url = self.location.GetStringSelection()
print(u"OnLocationSelect: %s\n" % url)
self.wv.LoadURL(url)
def OnLocationEnter(self, evt):
url = self.location.GetValue()
self.location.Append(url)
self.wv.LoadURL(url)
def OnOpenButton(self, event):
dlg = wx.TextEntryDialog(self, u"Input Url or local file path", u"URL", self.current, wx.OK|wx.CANCEL)
dlg.CentreOnParent()
if dlg.ShowModal() == wx.ID_OK:
self.current = dlg.GetValue()
self.wv.LoadURL(self.current)
dlg.Destroy()
def OnPrevPageButton(self, event):
for i in self.wv.GetBackwardHistory():
print i.Url, i.Title
self.wv.GoBack()
#self.wv.LoadURL(u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss")
def OnNextPageButton(self, event):
for i in self.wv.GetForwardHistory():
print i.URL, i.Title
self.wv.GoForward()
def OnCheckCanGoBack(self, event):
event.Enable(self.wv.CanGoBack())
def OnCheckCanGoForward(self, event):
event.Enable(self.wv.CanGoForward())
def OnStopButton(self, evt):
self.wv.Stop()
def OnRefreshPageButton(self, evt):
self.wv.Reload()
def OnReload(self, evt):
grap_news_title()
self.current = news_hosts[8]
self.wv.SetPage(news_rss[8], self.current)
def OnGoTo0(self, evt):
self.current = news_hosts[0]
self.wv.SetPage(news_rss[0], self.current)
def OnGoTo1(self, evt):
self.current = news_hosts[1]
self.wv.SetPage(news_rss[1], self.current)
def OnGoTo2(self, evt):
self.current = news_hosts[2]
self.wv.SetPage(news_rss[2], self.current)
def OnGoTo3(self, evt):
self.current = news_hosts[3]
self.wv.SetPage(news_rss[3], self.current)
def OnGoTo4(self, evt):
self.current = news_hosts[4]
self.wv.SetPage(news_rss[4], self.current)
def OnGoTo5(self, evt):
self.current = news_hosts[5]
self.wv.SetPage(news_rss[5], self.current)
def OnGoTo6(self, evt):
self.current = news_hosts[6]
self.wv.SetPage(news_rss[6], self.current)
def OnGoTo7(self, evt):
self.current = news_hosts[7]
self.wv.SetPage(news_rss[7], self.current)
def OnGoTo8(self, evt):
self.current = news_hosts[8]
self.wv.SetPage(news_rss[8], self.current)
def OnGoTo9(self, evt):
self.current = news_hosts[9]
self.wv.SetPage(news_rss[9], self.current)
def OnGoTo10(self, evt):
self.current = news_hosts[10]
self.wv.SetPage(news_rss[10], self.current)
def OnGoTo11(self, evt):
self.current = news_hosts[11]
self.wv.SetPage(news_rss[11], self.current)
def OnGoTo12(self, evt):
self.current = news_hosts[12]
self.wv.SetPage(news_rss[12], self.current)
def OnGoTo13(self, evt):
self.current = news_hosts[13]
self.wv.SetPage(news_rss[13], self.current)
#----------------------------------------------------------------------
def GetUrlSrc(as_url, as_decoding):
html_src = urllib2.urlopen(as_url).read()
return html_src.decode(as_decoding, 'ignore')
def SetPage(as_url, as_site, return_val=None, return_idx=None):
ls_temp_header = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
.news {
font-family: "돋움체";
font-size: 14px;
}
</style>
</head>
<body>"""
ls_temp_footer = u"""</body></html>"""
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if as_site == u"MK":
title_pattern = "<title>(.*?)</title>".decode("utf-8", "ignore")
script_pattern = "<div\sclass=\'read_txt\'>(.*?)\s*?<!--이미지".decode("utf-8", "ignore")
result_html = GetUrlSrc(as_url, u"cp949")
#wx.MessageBox(result_html, u"Test", wx.OK|wx.ICON_INFORMATION, self)
result_text = re.findall(script_pattern, result_html, re.DOTALL)
result_title = re.findall(title_pattern, result_html, re.DOTALL)
print result_title
if result_text:
#wx.MessageBox(result_text[0], u"Test", wx.OK|wx.ICON_INFORMATION, self)
#ls_return = " ".join(str(re.sub("<[^>]*>".decode("utf-8", "ignore"), "".decode("utf-8", "ignore"), result_text[0]).encode("cp949", "ignore")).split())
#wx.MessageBox(result_text[0], u"News", wx.OK|wx.ICON_INFORMATION)
return ls_temp_header + u"""<title>""" + result_title[0] + u"""</title>""" + result_text[0] + ls_temp_footer
elif as_site == u"TITLE":
html_src_uni = GetUrlSrc(as_url, 'utf-8')
soup = BeautifulSoup(html_src_uni)
ls_temp = str(soup('title')).decode('utf-8', 'ignore')
pattern = u'<title>(.*?)</title>'
ls_result_title = re.findall(pattern, ls_temp, re.DOTALL)
ls_temp = str(soup('link')).decode('utf-8', 'ignore')
pattern = u'<link>(.*?)</link>'
ls_result_link = re.findall(pattern, ls_temp, re.DOTALL)
ls_temp = str(soup('description')).decode('utf-8', 'ignore')
ls_temp = ls_temp.replace(u'신문기사</description>', u'신문기사..<img')
pattern = u'<description>(.*?)..<img'
ls_result_description = re.findall(pattern, ls_temp, re.DOTALL)
ls_temp_body_header = u"""<title>""" + ls_result_title[0] + u"""</title>""" + u"""<table width="1010" height="25" border="0" cellpadding="0" cellspacing="0"><tr><td width="1000" height="10" class="news">"""
ls_temp_body_footer = u"""</a></td></tr></table>"""
ls_temp_body = u""
for ll_idx in range(len(ls_result_title)):
if ll_idx == 0:
continue
ls_temp_body = ls_temp_body + ls_temp_body_header + str(ll_idx) + u" : " + u"""<a href=" """ + ls_result_link[ll_idx] + u""" ">""" + ls_result_title[ll_idx] + ls_temp_body_footer
if return_val == None:
return ls_temp_header + ls_temp_body + ls_temp_footer
else:
return_val[return_idx] = ls_temp_header + ls_temp_body + ls_temp_footer
threadexit()
def threadexit():
global threadsleft
lock.acquire()
threadsleft -= 1
lock.release()
def grap_news_title():
global threadsleft
threadsleft = len(news_hosts)
print len(news_hosts)
for i in range(len(news_hosts)):
news_rss.append(u"")
thread.start_new_thread(SetPage, (news_hosts[i], u"TITLE", news_rss, i ))
while threadsleft:
pass
def main():
global news_rss, threadsleft, lock, news_hosts
news_rss = [u""]
lock = thread.allocate_lock()
news_hosts = [u"http://mk.co.kr.feedsportal.com/c/34147/f/618372/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618373/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618374/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618375/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618376/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618377/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618378/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618379/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618381/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/651700/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/651701/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618383/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618384/index.rss"]
start = time.time()
grap_news_title()
print time.time() - start
app = wx.App()
frm = wx.Frame(None, style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.FRAME_EX_METAL, title=u"MK_RSS Reader v0.9", size=(1048,768))
frm.CreateStatusBar()
ico = wx.Icon(u"MK_RSS.ico", wx.BITMAP_TYPE_ICO)
frm.SetIcon(ico)
pnl = ReaderPanel(frm)
frm.Show()
frm.Center()
app.MainLoop()
#----------------------------------------------------------------------
if __name__ == '__main__':
main()
2013.10.15
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import wx
import wx.html2
import urllib2
import re
import thread
import time
news_url = u""
#----------------------------------------------------------------------
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.current = news_hosts[8]
self.frame = self.GetTopLevelParent()
self.titleBase = self.frame.GetTitle()
sizer = wx.BoxSizer(wx.VERTICAL)
grpSizer = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(self, label='News Category : ')
grpSizer.Add(st1, 0, wx.CENTER|wx.ALL, 2)
btn = wx.Button(self, -1, u"헤드라인", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo0, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"전체뉴스", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo1, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"경제", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo2, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"정치", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo3, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"사회", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo4, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"국제", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo5, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"기업경영", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo6, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"증권", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo7, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"부동산", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo8, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"문화연예", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo9, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"패션", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo10, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"스포츠", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo11, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"게임", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo12, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"오피니언", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoTo13, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
sizer.Add(grpSizer, 0, wx.EXPAND)
sizer.Add((-1, 10))
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
#btn = wx.Button(self, -1, u"Open", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#btn = wx.Button(self, -1, u"<--", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoBack, btn)
#btn = wx.Button(self, -1, u"-->", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoForward, btn)
#btn = wx.Button(self, -1, u"Stop", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
#btn = wx.Button(self, -1, u"Refresh", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
txt = wx.StaticText(self, -1, u"Location:")
btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)
self.location = wx.ComboBox(
self, -1, "", style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.location.AppendItems([u"http://www.mk.co.kr/"])
self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)
self.location.Bind(wx.EVT_TEXT_ENTER, self.OnLocationEnter)
btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)
sizer.Add(btnSizer, 0, wx.EXPAND)
sizer.Add((-1, 10))
self.wv = wx.html2.WebView.New(self)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.OnWebViewNavigating, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATED, self.OnWebViewNavigated, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnWebViewLoaded, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_TITLE_CHANGED, self.OnWebViewTitleChanged, self.wv)
sizer.Add(self.wv, 1, wx.EXPAND)
self.SetSizer(sizer)
#self.wv.LoadURL(self.current)
self.wv.SetPage(news_rss[8], news_hosts[8])
# WebView events
def OnWebViewNavigating(self, evt):
# this event happens prior to trying to get a resource
print u"OnWebViewNavigating URL is : %s" % evt.GetURL()
#wx.MessageBox(evt.GetURL(), u"Target URL is...", wx.OK|wx.ICON_INFORMATION, self)
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if (u"http://mk.co.kr.feedsportal.com/c/" in evt.GetURL()) and (not u"/index.rss" in evt.GetURL()):
news_url = evt.GetURL()
news_script = SetPage(evt.GetURL(), u"MK")
self.wv.SetPage(u"<html><div>" + news_script + u"</div></html>", news_url)
evt.Veto()
self.current = news_url
self.location.SetString(0, self.current)
elif (u"http://mk.co.kr.feedsportal.com/c/" in evt.GetURL()) and (u"/index.rss" in evt.GetURL()):
news_url = evt.GetURL()
news_script = SetPage(evt.GetURL(), u"TITLE")
self.wv.SetPage(news_script, news_url)
evt.Veto()
self.current = news_url
self.location.SetString(0, self.current)
def OnWebViewNavigated(self, evt):
self.frame.SetStatusText(u"Loading %s..." % evt.GetURL())
def OnTestButton(self, evt):
pass
def OnWebViewLoaded(self, evt):
# The full document has loaded
self.current = evt.GetURL()
if self.current == u"http:///":
self.current = self.location.GetString(0)
self.location.SetValue(self.location.GetString(0))
else:
self.location.SetValue(self.current)
self.frame.SetStatusText(u"Loaded")
#self.wv.RunScript("""alert('""" + self.current + """');""")
#self.wv.RunScript("""
#//call logIn();
#//document.getElementsByName('q')[0].value="hist";
#//document.getElementById('openididentifier').value="ident";
#""")
#self.wv.SetPage(self, """<html>하하하</html>""", """http://www.naver.com""")
def OnWebViewTitleChanged(self, evt):
# Set the frame's title to include the document's title
self.frame.SetTitle("%s -- %s" % (self.titleBase, evt.GetString()))
# Control bar events
def OnLocationSelect(self, evt):
url = self.location.GetStringSelection()
print(u"OnLocationSelect: %s\n" % url)
self.wv.LoadURL(url)
def OnLocationEnter(self, evt):
url = self.location.GetValue()
self.location.Append(url)
self.wv.LoadURL(url)
def OnOpenButton(self, event):
dlg = wx.TextEntryDialog(self, u"Input Url or local file path", u"URL", self.current, wx.OK|wx.CANCEL)
dlg.CentreOnParent()
if dlg.ShowModal() == wx.ID_OK:
self.current = dlg.GetValue()
self.wv.LoadURL(self.current)
dlg.Destroy()
def OnPrevPageButton(self, event):
for i in self.wv.GetBackwardHistory():
print i.Url, i.Title
self.wv.GoBack()
#self.wv.LoadURL(u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss")
def OnNextPageButton(self, event):
for i in self.wv.GetForwardHistory():
print i.URL, i.Title
self.wv.GoForward()
def OnCheckCanGoBack(self, event):
event.Enable(self.wv.CanGoBack())
def OnCheckCanGoForward(self, event):
event.Enable(self.wv.CanGoForward())
def OnStopButton(self, evt):
self.wv.Stop()
def OnRefreshPageButton(self, evt):
self.wv.Reload()
def OnGoTo0(self, evt):
self.current = news_hosts[0]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[0], self.current)
def OnGoTo1(self, evt):
self.current = news_hosts[1]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[1], self.current)
def OnGoTo2(self, evt):
self.current = news_hosts[2]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[2], self.current)
def OnGoTo3(self, evt):
self.current = news_hosts[3]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[3], self.current)
def OnGoTo4(self, evt):
self.current = news_hosts[4]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[4], self.current)
def OnGoTo5(self, evt):
self.current = news_hosts[5]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[5], self.current)
def OnGoTo6(self, evt):
self.current = news_hosts[6]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[6], self.current)
def OnGoTo7(self, evt):
self.current = news_hosts[7]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[7], self.current)
def OnGoTo8(self, evt):
self.current = news_hosts[8]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[8], self.current)
def OnGoTo9(self, evt):
self.current = news_hosts[9]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[9], self.current)
def OnGoTo10(self, evt):
self.current = news_hosts[10]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[10], self.current)
def OnGoTo11(self, evt):
self.current = news_hosts[11]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[11], self.current)
def OnGoTo12(self, evt):
self.current = news_hosts[12]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[12], self.current)
def OnGoTo13(self, evt):
self.current = news_hosts[13]
#self.wv.LoadURL(self.current)
#wx.MessageBox(economy_rss, u"Test", wx.OK|wx.ICON_INFORMATION, self)
self.wv.SetPage(news_rss[13], self.current)
#----------------------------------------------------------------------
def GetUrlSrc(as_url, as_decoding):
html_src = urllib2.urlopen(as_url).read()
return html_src.decode(as_decoding, 'ignore')
def SetPage(as_url, as_site, return_val=None, return_idx=None):
ls_temp_header = u"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
margin-left: 10px;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
}
.news {
font-family: "돋움체";
font-size: 14px;
}
</style>
</head>
<body>"""
ls_temp_footer = u"""</body></html>"""
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if as_site == u"MK":
title_pattern = "<title>(.*?)</title>".decode("utf-8", "ignore")
script_pattern = "<div\sclass=\'read_txt\'>(.*?)\s*?<!--이미지".decode("utf-8", "ignore")
result_html = GetUrlSrc(as_url, u"cp949")
#wx.MessageBox(result_html, u"Test", wx.OK|wx.ICON_INFORMATION, self)
result_text = re.findall(script_pattern, result_html, re.DOTALL)
result_title = re.findall(title_pattern, result_html, re.DOTALL)
print result_title
if result_text:
#wx.MessageBox(result_text[0], u"Test", wx.OK|wx.ICON_INFORMATION, self)
#ls_return = " ".join(str(re.sub("<[^>]*>".decode("utf-8", "ignore"), "".decode("utf-8", "ignore"), result_text[0]).encode("cp949", "ignore")).split())
#wx.MessageBox(result_text[0], u"News", wx.OK|wx.ICON_INFORMATION)
return ls_temp_header + u"""<title>""" + result_title[0] + u"""</title>""" + result_text[0] + ls_temp_footer
elif as_site == u"TITLE":
html_src_uni = GetUrlSrc(as_url, 'utf-8')
soup = BeautifulSoup(html_src_uni)
ls_temp = str(soup('title')).decode('utf-8', 'ignore')
pattern = u'<title>(.*?)</title>'
ls_result_title = re.findall(pattern, ls_temp, re.DOTALL)
ls_temp = str(soup('link')).decode('utf-8', 'ignore')
pattern = u'<link>(.*?)</link>'
ls_result_link = re.findall(pattern, ls_temp, re.DOTALL)
ls_temp = str(soup('description')).decode('utf-8', 'ignore')
ls_temp = ls_temp.replace(u'신문기사</description>', u'신문기사..<img')
pattern = u'<description>(.*?)..<img'
ls_result_description = re.findall(pattern, ls_temp, re.DOTALL)
ls_temp_body_header = u"""<title>""" + ls_result_title[0] + u"""</title>""" + u"""<table width="1010" height="25" border="0" cellpadding="0" cellspacing="0"><tr><td width="1000" height="10" class="news">"""
ls_temp_body_footer = u"""</a></td></tr></table>"""
ls_temp_body = u""
for ll_idx in range(len(ls_result_title)):
if ll_idx == 0:
continue
ls_temp_body = ls_temp_body + ls_temp_body_header + str(ll_idx) + u" : " + u"""<a href=" """ + ls_result_link[ll_idx] + u""" ">""" + ls_result_title[ll_idx] + ls_temp_body_footer
if return_val == None:
return ls_temp_header + ls_temp_body + ls_temp_footer
else:
return_val[return_idx] = ls_temp_header + ls_temp_body + ls_temp_footer
threadexit()
def threadexit():
global threadsleft
lock.acquire()
threadsleft -= 1
lock.release()
def main():
global news_rss, threadsleft, lock, news_hosts
news_rss = [u""]
lock = thread.allocate_lock()
news_hosts = [u"http://mk.co.kr.feedsportal.com/c/34147/f/618372/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618373/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618374/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618375/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618376/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618377/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618378/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618379/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618381/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/651700/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/651701/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618383/index.rss",
u"http://mk.co.kr.feedsportal.com/c/34147/f/618384/index.rss"]
start = time.time()
threadsleft = len(news_hosts)
print len(news_hosts)
for i in range(len(news_hosts)):
news_rss.append(u"")
thread.start_new_thread(SetPage, (news_hosts[i], u"TITLE", news_rss, i ))
while threadsleft:
pass
print time.time() - start
app = wx.App()
frm = wx.Frame(None, style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.FRAME_EX_METAL, title=u"MK_RSS Reader v0.9", size=(1048,768))
frm.CreateStatusBar()
ico = wx.Icon(u"MK_RSS.ico", wx.BITMAP_TYPE_ICO)
frm.SetIcon(ico)
pnl = TestPanel(frm)
frm.Show()
frm.Center()
app.MainLoop()
#----------------------------------------------------------------------
if __name__ == '__main__':
main()
2013.10.07
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import wx
import wx.html2
import urllib2
import re
news_url = u""
#----------------------------------------------------------------------
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.current = u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss"
self.frame = self.GetTopLevelParent()
self.titleBase = self.frame.GetTitle()
sizer = wx.BoxSizer(wx.VERTICAL)
grpSizer = wx.BoxSizer(wx.HORIZONTAL)
st1 = wx.StaticText(self, label='News Category : ')
grpSizer.Add(st1, 0, wx.CENTER|wx.ALL, 2)
btn = wx.Button(self, -1, u"경제", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoToEconomy, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"증권", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoToStock, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"부동산", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnGoToRealEState, btn)
grpSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
sizer.Add(grpSizer, 0, wx.EXPAND)
sizer.Add((-1, 10))
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
#btn = wx.Button(self, -1, u"Open", style=wx.BU_EXACTFIT)
#self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
#btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"<--", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoBack, btn)
btn = wx.Button(self, -1, u"-->", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoForward, btn)
btn = wx.Button(self, -1, u"Stop", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, u"Refresh", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
txt = wx.StaticText(self, -1, u"Location:")
btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)
self.location = wx.ComboBox(
self, -1, "", style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.location.AppendItems([u"http://file.mk.co.kr/news/rss/rss_50300009.xml",
u"http://news.mk.co.kr/rss/rss.php",
u"http://www.mk.co.kr/"])
self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)
self.location.Bind(wx.EVT_TEXT_ENTER, self.OnLocationEnter)
btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)
sizer.Add(btnSizer, 0, wx.EXPAND)
sizer.Add((-1, 10))
self.wv = wx.html2.WebView.New(self)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.OnWebViewNavigating, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATED, self.OnWebViewNavigated, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnWebViewLoaded, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_TITLE_CHANGED, self.OnWebViewTitleChanged, self.wv)
sizer.Add(self.wv, 1, wx.EXPAND)
self.SetSizer(sizer)
self.wv.LoadURL(self.current)
# WebView events
def OnWebViewNavigating(self, evt):
# this event happens prior to trying to get a resource
print u"OnWebViewNavigating URL is : %s" % evt.GetURL()
#wx.MessageBox(evt.GetURL(), u"Target URL is...", wx.OK|wx.ICON_INFORMATION, self)
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if (u"http://mk.co.kr.feedsportal.com/c/" in evt.GetURL()) and (not u"/index.rss" in evt.GetURL()):
news_url = evt.GetURL()
news_script = SetPage(evt.GetURL(), u"MK")
self.wv.SetPage(u"<html><div>" + news_script + u"</div></html>", news_url)
evt.Veto()
self.current = news_url
self.location.SetString(0, self.current)
def OnWebViewNavigated(self, evt):
self.frame.SetStatusText(u"Loading %s..." % evt.GetURL())
def OnTestButton(self, evt):
pass
def OnWebViewLoaded(self, evt):
# The full document has loaded
self.current = evt.GetURL()
if self.current == u"http:///":
self.current = self.location.GetString(0)
self.location.SetValue(self.location.GetString(0))
else:
self.location.SetValue(self.current)
self.frame.SetStatusText(u"Loaded")
#self.wv.RunScript("""alert('""" + self.current + """');""")
#self.wv.RunScript("""
#//call logIn();
#//document.getElementsByName('q')[0].value="hist";
#//document.getElementById('openididentifier').value="ident";
#""")
#self.wv.SetPage(self, """<html>하하하</html>""", """http://www.naver.com""")
def OnWebViewTitleChanged(self, evt):
# Set the frame's title to include the document's title
self.frame.SetTitle("%s -- %s" % (self.titleBase, evt.GetString()))
# Control bar events
def OnLocationSelect(self, evt):
url = self.location.GetStringSelection()
print(u"OnLocationSelect: %s\n" % url)
self.wv.LoadURL(url)
def OnLocationEnter(self, evt):
url = self.location.GetValue()
self.location.Append(url)
self.wv.LoadURL(url)
def OnOpenButton(self, event):
dlg = wx.TextEntryDialog(self, u"Input Url or local file path", u"URL", self.current, wx.OK|wx.CANCEL)
dlg.CentreOnParent()
if dlg.ShowModal() == wx.ID_OK:
self.current = dlg.GetValue()
self.wv.LoadURL(self.current)
dlg.Destroy()
def OnPrevPageButton(self, event):
for i in self.wv.GetBackwardHistory():
print i.Url, i.Title
self.wv.GoBack()
#self.wv.LoadURL(u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss")
def OnNextPageButton(self, event):
for i in self.wv.GetForwardHistory():
print i.URL, i.Title
self.wv.GoForward()
def OnCheckCanGoBack(self, event):
event.Enable(self.wv.CanGoBack())
def OnCheckCanGoForward(self, event):
event.Enable(self.wv.CanGoForward())
def OnStopButton(self, evt):
self.wv.Stop()
def OnRefreshPageButton(self, evt):
self.wv.Reload()
def OnGoToEconomy(self, evt):
self.current = u"http://mk.co.kr.feedsportal.com/c/34147/f/618374/index.rss"
self.wv.LoadURL(self.current)
def OnGoToStock(self, evt):
self.current = u"http://mk.co.kr.feedsportal.com/c/34147/f/618379/index.rss"
self.wv.LoadURL(self.current)
def OnGoToRealEState(self, evt):
self.current = u"http://mk.co.kr.feedsportal.com/c/34147/f/618380/index.rss"
self.wv.LoadURL(self.current)
#----------------------------------------------------------------------
def GetUrlSrc(as_url, as_decoding):
html_src = urllib2.urlopen(as_url).read()
return html_src.decode(as_decoding, 'ignore')
def SetPage(as_url, as_site):
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if as_site == u"MK":
script_pattern = "<div\sclass=\'read_txt\'>(.*?)\s*?<!--이미지".decode("utf-8", "ignore")
result_html = GetUrlSrc(as_url, u"cp949")
#wx.MessageBox(result_html, u"Test", wx.OK|wx.ICON_INFORMATION, self)
result_text = re.findall(script_pattern, result_html, re.DOTALL)
if result_text:
#wx.MessageBox(result_text[0], u"Test", wx.OK|wx.ICON_INFORMATION, self)
#ls_return = " ".join(str(re.sub("<[^>]*>".decode("utf-8", "ignore"), "".decode("utf-8", "ignore"), result_text[0]).encode("cp949", "ignore")).split())
#wx.MessageBox(result_text[0], u"News", wx.OK|wx.ICON_INFORMATION)
return result_text[0]
def main():
app = wx.App()
frm = wx.Frame(None, style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.FRAME_EX_METAL, title=u"MK_RSS Reader v0.9", size=(1048,768))
frm.CreateStatusBar()
pnl = TestPanel(frm)
frm.Show()
frm.Center()
app.MainLoop()
#----------------------------------------------------------------------
if __name__ == '__main__':
main()
2013.10.06
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import wx
import wx.html2
import urllib2
import re
#----------------------------------------------------------------------
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.current = u"http://file.mk.co.kr/news/rss/rss_50300009.xml"
self.frame = self.GetTopLevelParent()
self.titleBase = self.frame.GetTitle()
sizer = wx.BoxSizer(wx.VERTICAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
self.wv = wx.html2.WebView.New(self)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING, self.OnWebViewNavigating, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATED, self.OnWebViewNavigated, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnWebViewLoaded, self.wv)
self.Bind(wx.html2.EVT_WEBVIEW_TITLE_CHANGED, self.OnWebViewTitleChanged, self.wv)
btn = wx.Button(self, -1, "Open", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, "<--", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnPrevPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoBack, btn)
btn = wx.Button(self, -1, "-->", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnNextPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
self.Bind(wx.EVT_UPDATE_UI, self.OnCheckCanGoForward, btn)
btn = wx.Button(self, -1, "Stop", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnStopButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
btn = wx.Button(self, -1, "Refresh", style=wx.BU_EXACTFIT)
self.Bind(wx.EVT_BUTTON, self.OnRefreshPageButton, btn)
btnSizer.Add(btn, 0, wx.EXPAND|wx.ALL, 2)
txt = wx.StaticText(self, -1, "Location:")
btnSizer.Add(txt, 0, wx.CENTER|wx.ALL, 2)
self.location = wx.ComboBox(
self, -1, "", style=wx.CB_DROPDOWN|wx.TE_PROCESS_ENTER)
self.location.AppendItems(['http://wxPython.org',
'http://wxwidgets.org',
'http://google.com'])
self.Bind(wx.EVT_COMBOBOX, self.OnLocationSelect, self.location)
self.location.Bind(wx.EVT_TEXT_ENTER, self.OnLocationEnter)
btnSizer.Add(self.location, 1, wx.EXPAND|wx.ALL, 2)
sizer.Add(btnSizer, 0, wx.EXPAND)
sizer.Add(self.wv, 1, wx.EXPAND)
self.SetSizer(sizer)
self.wv.LoadURL(self.current)
# WebView events
def OnWebViewNavigating(self, evt):
# this event happens prior to trying to get a resource
print u"Target URL is : %s" % evt.GetURL()
#wx.MessageBox(evt.GetURL(), u"Target URL is...", wx.OK|wx.ICON_INFORMATION, self)
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if u"http://mk.co.kr.feedsportal.com/c/" in evt.GetURL():
news_script = SetPage(evt.GetURL(), u"MK")
print type(news_script)
self.wv.SetPage(u"<html><div>" + news_script + u"</div></html>", "")
evt.Veto()
def OnWebViewNavigated(self, evt):
self.frame.SetStatusText("Loading %s..." % evt.GetURL())
def OnTestButton(self, evt):
pass
def OnWebViewLoaded(self, evt):
# The full document has loaded
self.current = evt.GetURL()
self.location.SetValue(self.current)
self.frame.SetStatusText("Loaded")
#self.wv.RunScript("""alert('""" + self.current + """');""")
#self.wv.RunScript("""
#//call logIn();
#//document.getElementsByName('q')[0].value="hist";
#//document.getElementById('openididentifier').value="ident";
#""")
#self.wv.SetPage(self, """<html>하하하</html>""", """http://www.naver.com""")
def OnWebViewTitleChanged(self, evt):
# Set the frame's title to include the document's title
self.frame.SetTitle("%s -- %s" % (self.titleBase, evt.GetString()))
# Control bar events
def OnLocationSelect(self, evt):
url = self.location.GetStringSelection()
print('OnLocationSelect: %s\n' % url)
self.wv.LoadURL(url)
def OnLocationEnter(self, evt):
url = self.location.GetValue()
self.location.Append(url)
self.wv.LoadURL(url)
def OnOpenButton(self, event):
dlg = wx.TextEntryDialog(self, "Open Location",
"Enter a full URL or local path",
self.current, wx.OK|wx.CANCEL)
dlg.CentreOnParent()
if dlg.ShowModal() == wx.ID_OK:
self.current = dlg.GetValue()
self.wv.LoadURL(self.current)
dlg.Destroy()
def OnPrevPageButton(self, event):
#for i in self.wv.GetBackwardHistory():
# print i.Url, i.Title
#self.wv.GoBack()
self.wv.LoadURL(u"http://file.mk.co.kr/news/rss/rss_50300009.xml")
def OnNextPageButton(self, event):
#for i in self.wv.GetForwardHistory():
# print i.URL, i.Title
self.wv.GoForward()
def OnCheckCanGoBack(self, event):
event.Enable(self.wv.CanGoBack())
def OnCheckCanGoForward(self, event):
event.Enable(self.wv.CanGoForward())
def OnStopButton(self, evt):
self.wv.Stop()
def OnRefreshPageButton(self, evt):
self.wv.Reload()
#----------------------------------------------------------------------
def GetUrlSrc(as_url, as_decoding):
html_src = urllib2.urlopen(as_url).read()
return html_src.decode(as_decoding, 'ignore')
def SetPage(as_url, as_site):
# 1. URL이 기사 URL타입과 일치하면... 파싱 아니면 그냥 웹표시
if as_site == u"MK":
script_pattern = "<div\sclass=\'read_txt\'>(.*?)\s*?<!--이미지".decode("utf-8", "ignore")
result_html = GetUrlSrc(as_url, u"cp949")
#wx.MessageBox(result_html, u"Test", wx.OK|wx.ICON_INFORMATION, self)
result_text = re.findall(script_pattern, result_html, re.DOTALL)
if result_text:
#wx.MessageBox(result_text[0], u"Test", wx.OK|wx.ICON_INFORMATION, self)
#ls_return = " ".join(str(re.sub("<[^>]*>".decode("utf-8", "ignore"), "".decode("utf-8", "ignore"), result_text[0]).encode("cp949", "ignore")).split())
#wx.MessageBox(result_text[0], u"News", wx.OK|wx.ICON_INFORMATION)
return result_text[0]
def main():
app = wx.App()
frm = wx.Frame(None, style=wx.DEFAULT_FRAME_STYLE | wx.RESIZE_BORDER | wx.FRAME_EX_METAL, title=u"MK_RSS Reader v0.9", size=(1048,768))
frm.CreateStatusBar()
pnl = TestPanel(frm)
frm.Show()
frm.Center()
app.MainLoop()
#----------------------------------------------------------------------
if __name__ == '__main__':
main()
'Study > python' 카테고리의 다른 글
[연습]AutoPOKO ~~ (0) | 2013.11.11 |
---|---|
[펌]python thread 사용예 (0) | 2013.10.16 |
wxpython webview test (0) | 2013.10.05 |
[연습중]MK RSS 파싱 (0) | 2013.09.23 |
[연습]손바닥삼국지 오토 (0) | 2013.09.22 |