# Created by Leo from: C:\Python23\Tom\leo\googollib.leo # <> import googolplex from stripogram import html2text, html2safehtml from sgmllib import SGMLParser #import xreadlines import urlparse from wxPython.wx import * #from wxPython.html import * from wxPython.iewin import * from wxPython.gizmos import * from wxPython.lib.throbber import * import threading import sys,string,time,os from wxPython.lib.buttons import * #sys.path.append('c:\\python22\\HTMLgen') #import HTMLgen import Image import image_view import ImgQueryLib import urllib2 import StringIO from wxPython.lib.evtmgr import eventManager #import metakit #wxInitAllImageHandlers() gs = googolplex.Googolplex() #gs.maxresults(20) #gs.language('lang_es') #gs.setproxy('localhost:8080') #res = gs.search('niburu') #for i in res: # print i['title'] #print 'Total:',len(res) #DcxImagePlugin,EpsImagePlugin, import ArgImagePlugin,BmpImagePlugin,CurImagePlugin,FliImagePlugin,FpxImagePlugin,GbrImagePlugin,GifImagePlugin,IcoImagePlugin,ImImagePlugin,ImtImagePlugin,IptcImagePlugin,JpegImagePlugin,McIdasImagePlugin,MicImagePlugin,MpegImagePlugin,MspImagePlugin,PcdImagePlugin,PcxImagePlugin,PdfImagePlugin,PixarImagePlugin,PngImagePlugin,PpmImagePlugin,PsdImagePlugin,SgiImagePlugin, SunImagePlugin,TgaImagePlugin,TiffImagePlugin,WmfImagePlugin import XVThumbImagePlugin,XbmImagePlugin,XpmImagePlugin # -- end -- <> # << googolstrip methods >> (1 of 8) def is_ord (string): new_text = '' for i in string: if ord(i) > 127: new_text = new_text + '' else: new_text = new_text + i return new_text # << googolstrip methods >> (2 of 8) def WXToPIL(image, mode = 'RGBA'): "convert a wxImage to a PIL RGBA image" imageData = image.GetData() size = (image.GetWidth(), image.GetHeight()) imagePIL = Image.fromstring('RGB', size, imageData) if mode != 'RGB': imagePIL = imagePIL.convert(mode) return imagePIL # << googolstrip methods >> (3 of 8) def PILToWX(image): "convert a PIL image to a wxImage" if (image.mode == 'RGBA'): bk = Image.new("RGB", image.size, (255, 250, 255)) image = Image.composite(image, bk, image) if (image.mode != 'RGB'): image = image.convert('RGB') imageData = image.tostring('raw', 'RGB') imageWx = wxEmptyImage(image.size[0], image.size[1]) imageWx.SetData(imageData) imageWx.SetMaskColour(255, 250, 255) return imageWx # << googolstrip methods >> (4 of 8) class splitframe(wxSplitterWindow): # << class splitframe methods >> def __init__(self, parent, id): wxSplitterWindow.__init__(self, parent, id) self.SetMinimumPaneSize(20) self.searchwind = SearchFrame(self, -1,) self.imageframe = ImgFrame(self, -1) self.SplitVertically(self.searchwind, self.imageframe, -100) #self.searchwind.Show() #self.imageframe.Show() # -- end -- << class splitframe methods >> # << googolstrip methods >> (5 of 8) class ImgFrame(wxNotebook): # << class ImgFrame methods >> (1 of 2) def __init__(self, parent, id): wxNotebook.__init__(self, parent, id)#, size = (500, 500),style = wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE ) #self.notebook = wxNotebook(self, wxNewId(), style=wxNB_TOP) self.ViewList = [] self.ViewList.append(image_view.ImageView(self,id,tools = [image_view.PanTool,image_view.ZoomTool, image_view.InspectTool , image_view.SharpTool , image_view.ColorTool , image_view.BrightTool , image_view.ContTool , image_view.RestoreTool , image_view.RedTool , image_view.GreenTool , image_view.BlueTool, image_view.LoadTool, image_view.SaveTool, image_view.URLTool], image = None, pos=(-1, -1), size=(-1, -1), style=0, name='ImageView')) self.AddPage(self.ViewList[0],'') # << class ImgFrame methods >> (2 of 2) def URLAdd(self,img, path): id = wxNewId() #self.ViewList.append(image_view.ImageView(self.notebook,id))#,tools = [image_view.PanTool, image_view.ZoomTool, image_view.SaveTool])) #self.ViewList[len(self.ViewList)-1].set_image(img) self.ViewList.append(image_view.ImageView(self,id,tools = [image_view.PanTool,image_view.ZoomTool, image_view.InspectTool , image_view.SharpTool , image_view.ColorTool , image_view.BrightTool , image_view.ContTool , image_view.RestoreTool , image_view.RedTool , image_view.GreenTool , image_view.BlueTool, image_view.LoadTool, image_view.SaveTool, image_view.URLTool], image = img, pos=(-1, -1), size=(-1, -1), style=0, name='ImageView')) self.AddPage(self.ViewList[len(self.ViewList)-1],os.path.split(path)[1]) # -- end -- << class ImgFrame methods >> # << googolstrip methods >> (6 of 8) class MyIParser(SGMLParser): # << class MyIParser methods >> def start_img(self,attrs): self.attrs.append(attrs) #print attrs # -- end -- << class MyIParser methods >> # << googolstrip methods >> (7 of 8) def tomparse(page , urlroot): tom = MyIParser() tom.attrs = [] img_list = [] for line in page: try: tom.feed(line) except: pass #del page #print tom.attrs for i in tom.attrs: for j in i: if j[0] == 'src': if j[1][:4] != 'http': img_list.append(urlparse.urljoin(urlroot , j[1])) else: img_list.append(j[1]) return img_list # << googolstrip methods >> (8 of 8) class SearchFrame(wxWindow): # << class MyFrame methods >> (1 of 15) def __init__(self, parent, id): wxWindow.__init__(self, parent, id)#, size = (500, 500),style = wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE ) self.tommytest = Image.new('RGB',(128,128)) self.testimageflag = 0 self.images = {} self.imgmach = [] self.image_dir = '' self.imgdict = {} self.pages = {} self.parent = parent self.imgmach = [] self.mythreads = [] self.imagelist = [] self.seccondtime = 1 self.newcount = 0 self.totalthreadcount = 1 #self.imageframe = ImgFrame(self, -1, 'Maching images') self.imgdatabase = ImgQueryLib.ImgDB()#'c:\\python22\\tom.dfi') id = wxNewId() self.CodeWindow = wxIEHtmlWin(self, id, style = wxNO_FULL_REPAINT_ON_RESIZE)#MyHtmlWindow(self, id, wxPoint(390, 15), wxSize(300, 300)) sizer = wxBoxSizer(wxVERTICAL) btnSizer = wxBoxSizer(wxHORIZONTAL) self.btn1 = wxButton(self, wxNewId(), 'Search', style=wxBU_EXACTFIT)#wxGenBitmapButton(self, -1, self.startsbmp) eventManager.Register(self.OnOpenButton, EVT_BUTTON, self.btn1) #EVT_BUTTON(self, btn.GetId(), self.OnOpenButton) btnSizer.Add(self.btn1, 1, wxEXPAND ) self.main_stories = '' txt1 = wxStaticText(self, -1, 'Sites:')#wxStaticBitmap(self, -1, self.site_bmp) #btnSizer.Add(txt, 1, wxALIGN_RIGHT) sliderSizer = wxBoxSizer(wxHORIZONTAL) self.text = wxTextCtrl(self, -1, '25')#,(-1,-1),(-1, -1)) slid = wxNewId() self.slider = wxSpinButton(self, slid, (-1,-1),(-1, -1), wxSP_HORIZONTAL)#wxSlider(self, 100, 25, 1, 100, (-1,-1),(-1, -1),wxSL_HORIZONTAL | wxSL_LABELS ) self.slider.SetRange(0,200) self.slider.SetValue(25) sliderSizer.Add(txt1, 1, wxEXPAND) sliderSizer.Add(self.text, 2, wxEXPAND) sliderSizer.Add(self.slider, 2, wxEXPAND) eventManager.Register(self.OnSpin, EVT_SPIN, self.slider) #EVT_SPIN(self, slid, self.OnSpin) #self.slider.SetTickFreq(5, 1) #btn = wxButton(self, wxNewId(), "Save", style=wxBU_EXACTFIT) #EVT_BUTTON(self, btn.GetId(), self.OnSaveButton) btnSizer.Add(sliderSizer, 3,wxEXPAND) self.btn = wxButton(self, wxNewId(), 'Add Image', style=wxBU_EXACTFIT)#wxGenBitmapButton(self, -1, self.addimg_bmp) eventManager.Register(self.OnImageButton, EVT_BUTTON, self.btn) #EVT_BUTTON(self, btn.GetId(), self.OnImageButton) btnSizer.Add(self.btn, 1, wxEXPAND) self.current = '' stSizer = wxBoxSizer(wxHORIZONTAL) txt = wxStaticText(self, -1, 'Search Term:')#wxStaticBitmap(self, -1, self.search_bmp ) stSizer.Add(txt, 1, wxALIGN_RIGHT ) #btnSizer.Add(txt, 1, wxALIGN_RIGHT ) self.term = wxTextCtrl(self, -1, '')#, size=(125, -1)) eventManager.Register(self.onchar, EVT_TEXT, self.term) #EVT_TEXT(self, self.term.GetId(), self.onchar) stSizer.Add(self.term, 1, wxEXPAND) btnSizer.Add(stSizer, 2, wxEXPAND) #self.led = wxLEDNumberCtrl(self, -1, (-1,-1), (125, -1)) #btnSizer.Add(self.led, 1, wxEXPAND) self.g2 = wxGauge(self, -1, 50, wxPoint(110, 95), wxSize(250, 25), wxGA_HORIZONTAL|wxGA_SMOOTH) self.g2.SetBezelFace(3) self.g2.SetShadowWidth(3) #self.otherThrobber = Throbber(self, -1,self.throb_img)#, size=(48, 48), frameDelay = 0.15,frames = 12,frameWidth = 48, label = "Stop") #btnSizer.Add(self.otherThrobber, 1, wxEXPAND) btnSizer.Add(self.g2, 1, wxEXPAND) sizer.Add(btnSizer,1, wxEXPAND) sizer.Add(self.CodeWindow,30, wxEXPAND) self.SetSizer(sizer) self.SetAutoLayout(true) eventManager.Register(self.OnSize, EVT_SIZE, self) #EVT_SIZE(self, self.OnSize) eventManager.Register(self.onIdle, EVT_IDLE, self) #EVT_IDLE(self, self.onIdle) #self.otherThrobber.Start() # << class MyFrame methods >> (2 of 15) def OnOpenButton(self, event): self.pages = {} self.seccondtime = 1 #self.otherThrobber.Start() #if self.testimageflag: # self.imageframe.Show() sitenum = int(self.slider.GetValue()) gs.maxresults(sitenum) self.g2.SetRange(int(sitenum)) try: self.res = gs.search(self.current) except: dlg = wxMessageDialog(frame, 'Comunications not resonding captain!', 'Engineers Report', wxOK | wxICON_INFORMATION) dlg.ShowModal() dlg.Destroy() self.otherThrobber.Stop() return #print self.res self.main_stories = "" for i in self.res: self.mythreads.append(threading.Thread(group=None, target=self.launchthread, name=i['title'], args=(i['url'],i['title']), kwargs={})) self.mythreads[len(self.mythreads)-1].start() #self.launchthread(i['url'],i['title']) #tenpercent = int(.1*len(self.res)) # tempcount = threading.activeCount() # while threading.activeCount() > 2: # wxSafeYield()#(1 + tenpercent): # if tempcount != threading.activeCount(): # tempcount = threading.activeCount() # print 'threading.activeCount()',threading.activeCount() # #print 'threads', threading.enumerate() # pageindex = self.pages.keys() # pageindex.sort() # pageindex.reverse() # for i in pageindex: # #print 'index: ',i # self.main_stories = self.main_stories + self.pages[i] # self.main_stories + self.main_stories + '' # self.CodeWindow.SetPage(self.main_stories) # wxSafeYield() # if self.testimageflag: # print 'checking images' # os.chdir('c:\\python22\\urlimg') # for path in os.listdir(os.getcwd()): # print path # self.imgdatabase.addFile(os.path.join(os.getcwd(),path)) # self.imgmach = self.imgdatabase.eng.query(self.tommytest) # for i in self.imgmach: # aImage = Image.open(i.pair[0]) # self.imageframe.URLAdd(aImage, i.pair[0]) # << class MyFrame methods >> (3 of 15) def OnSize(self, evt): self.Layout() # << class MyFrame methods >> (4 of 15) def OnSpin(self, evt): self.text.SetValue(str(evt.GetPosition())) # << class MyFrame methods >> (5 of 15) def OnDestroy(self,evt): evt.Skip() # << class MyFrame methods >> (6 of 15) def getcount(self,text,search): countthing = 0 for i in string.split(search): countthing = countthing + text.count(i) return countthing # << class MyFrame methods >> (7 of 15) def onchar(self,evt): self.current = evt.GetString() # << class MyFrame methods >> (8 of 15) def launchthread(self, url, title): gettencount = 0 counterthing = 0.1 imgthreads = [] try: page = urllib2.urlopen(url) except: return try: stories = page.read() cleanhtml = html2safehtml(stories,valid_tags=('br','p'))#, 'a', 'i', 'br', 'p')) #cleanhtml = html2text(cleanhtml,ignore_tags=('br','p',),indent_width=4,page_width=80) cleanhtml = is_ord(cleanhtml) gettencount = self.getcount(cleanhtml,self.current) if gettencount == 0: pass else: if self.pages.has_key(gettencount): while self.pages.has_key(gettencount + counterthing): counterthing = counterthing + .1 self.images[gettencount + counterthing] = tomparse(StringIO.StringIO(stories) , url) self.pages[gettencount + counterthing] = '

TITLE: ' + title + '
LINK: ' + url + '

' + cleanhtml else: self.images[gettencount] = tomparse(StringIO.StringIO(stories) , url) self.pages[gettencount] = '

TITLE: ' + title + '
LINK: ' + url + '

' + cleanhtml if self.testimageflag: print 'self.testimageflag' try: parsdir = urlparse.urlparse(url)[2] except: parsdir = '/unknown/' + str(time.time()) print 'parsdir',parsdir #newdir = os.path.split(parsdir)[1] pathtext = self.current.strip().replace(' ','_') pathtext = pathtext.replace('.','_') parsdir = pathtext #+ os.path.splitext(newdir)[0].replace('.','_') + str(len(self.pages)) newdir = os.path.join('c:\\python23\\urlimg',parsdir) print 'newdir',newdir if not os.path.exists(newdir): os.mkdir(newdir) print 'Made directory ',newdir newdir = str(newdir) self.image_dir = newdir imagelist = [] for path in tomparse(StringIO.StringIO(stories) , url): print 'appended',path imagelist.append((path,newdir)) self.imgdict[url] = imagelist #self.imgthread(path,newdir) #imgthreads.append(threading.Thread(group=None, target=self.imgthread, name=url, args=((path),(newdir)), kwargs={})) #imgthreads[len(imgthreads)-1].start() except: pass # << class MyFrame methods >> (9 of 15) def imgthread(self, fname,rootpath): print 'saveing ',fname,' at ',rootpath newpath = '' typofimage = os.path.splitext(fname)[1] if typofimage not in ('.gif','.jpg','.png','.bmp'): #print 'not in image type' return try: tom = urllib2.urlopen(fname) except: #print "Error opening/decoding image file " + fname return tomstr = tom.read() try: aImage = Image.open(StringIO.StringIO(tomstr)).convert('RGB') #WXToPIL(wxImageFromStream(StringIO.StringIO(tomstr)),'RGB') newpath = os.path.join(rootpath,os.path.split(fname)[1]) except: #print 'PIL open error ',newpath return if aImage.size[0] > 30 and aImage.size[1] > 30: try: aImage.save(newpath) except: #print 'PIL save problem ',newpath pass #try: # self.imgdatabase.addImage(aImage,newpath) #except: # #print 'Database add problem ',newpath # pass del aImage del tom # << class MyFrame methods >> (10 of 15) def Savesearch(self): name = self.current.strip().replace(' ','_') + '.htm' path = os.path.join('c:\\python23\\tom2\\urlimg',name) tom = file(path,'w') tom.write(self.main_stories) tom.close() del tom # << class MyFrame methods >> (11 of 15) def Saveimage(self): name = self.current.strip().replace(' ','_') + '.idb' path = os.path.join('c:\\python23\\tom2\\urlimg',name) self.imgdatabase.changefname(path) self.imgdatabase.savedb() # << class MyFrame methods >> (12 of 15) def OnImageButton(self,evt): wildcard = "Jpeg (*.jpg)|*.jpg|Giff (*.gif)|*.gif|Bitmap (*.bmp)|*.bmp|All files (*.*)|*.*" dlg = wxFileDialog(self, "Choose an image", "", "", wildcard, wxOPEN) if dlg.ShowModal() == wxID_OK: path = str(dlg.GetPath()) self.tommytest = Image.open(path).convert('RGB') self.parent.imageframe.SetPageText(0,os.path.split(path)[1]) self.parent.imageframe.ViewList[0].set_image(self.tommytest) #self.parent.imageframe.URLAdd(self.tommytest, path) self.tommytest = self.tommytest.resize((128,128)) self.testimageflag = 1 dlg.Destroy() #print path # << class MyFrame methods >> (13 of 15) def onIdle(self,evt): if self.totalthreadcount != threading.activeCount(): self.totalthreadcount = threading.activeCount() self.g2.SetValue(int(threading.activeCount())) #print 'threading.activeCount()',threading.activeCount() #self.led.SetValue(str(threading.activeCount())) #print 'threads', threading.enumerate() pageindex = self.pages.keys() pageindex.sort() pageindex.reverse() self.main_stories = '' for i in pageindex: #print 'index: ',i self.main_stories = self.main_stories + self.pages[i] self.main_stories + self.main_stories + '' self.CodeWindow.LoadString(self.main_stories) if self.totalthreadcount == 1: self.Savesearch() #self.otherThrobber.Stop() if self.testimageflag and self.seccondtime: print 'running test image flag',len(self.imgdict.values()) #print 'threading.activeCount()',threading.activeCount() #self.led.SetValue(str(threading.activeCount())) #self.otherThrobber.Start() lastthred = threading.Thread(group=None, target=self.GetwebImages, name='imgsv', args=(), kwargs={}) lastthred.start() #for i in self.imagelist:self.imgthread(i[0],i[1]) #self.imgage_testing = 1 #self.testImage() #if self.totalthreadcount == 1: #self.Saveimage() elif self.testimageflag and not self.seccondtime: self.testImage() self.Saveimage() # << class MyFrame methods >> (14 of 15) def GetwebImages(self): count = 1 self.g2.SetRange(len(self.imgdict.values())) for j in self.imgdict.values(): for i in j: self.g2.SetValue(count) self.imgthread(i[0],i[1]) count = count + 1 self.seccondtime = 0 # << class MyFrame methods >> (15 of 15) def testImage(self): mythreads = [] #print 'checking images',len(self.image_dir_list) #os.chdir('c:\\python22\\urlimg') #for direc in self.image_dir_list: for path in os.listdir(self.image_dir): #print direc,'directory',path,'path' if path != 'Thumbs.db': #mythreads.append(threading.Thread(group=None, target=self.imgdatabase.addFile, name=path, args=((os.path.join(direc,path)),), kwargs={})) #mythreads[len(mythreads)-1].start() self.imgdatabase.addFile(os.path.join(self.image_dir,path)) #while mythreads[len(mythreads)-1].isAlive(): #pass #self.imgdatabase.addFile(os.path.join(os.getcwd(),path)) self.imgmach = self.imgdatabase.eng.query(self.tommytest) for i in self.imgmach: aImage = Image.open(i.pair[0]) self.parent.imageframe.URLAdd(aImage, i.pair[0]) #self.otherThrobber.Stop() # -- end -- << class MyFrame methods >> # -- end -- << googolstrip methods >>