#!/usr/bin/env python # # This is derived from Kenytt Avery's YAGOB: # ##################################################### # YAGOB v0.2 -- # Yet Another Google Outline Browser # (see http://www.google.com/apis/) # # Screenshot http://kavery.ecs.fullerton.edu/images/yagob.jpg # # Requires wxPython and PyGoogle # (see http://www.wxpython.org/ # and http://diveintomark.org/projects/#pygoogle) # # Set the GOOGLE_LICENSE_KEY environment variable # or see the PyGoogle docs for alternatives # # Copyright (C) 2002 by Kenytt Avery, # Willing Minds LLC # (see http://www.willingminds.com/ # and http://kavery.ecs.fullerton.edu/) ######################################################### import sys import google import re from wxPython.wx import * from wxPython.gizmos import * #---------------------------------------------------------------------- class URLTree(wxRemotelyScrolledTreeCtrl): def __init__(self, parent, ID, pos=wxDefaultPosition, size=wxDefaultSize,style=wxTR_HAS_BUTTONS): wxRemotelyScrolledTreeCtrl.__init__(self, parent, ID, pos, size, style) EVT_TREE_ITEM_EXPANDING(self, self.GetId(), self.OnItemExpanding) EVT_RIGHT_DCLICK(self, self.OnRightDclick) EVT_LEFT_DCLICK(self, self.OnLeftDclick) def OnItemExpanding(self, event): parent = event.GetItem() if self.GetChildrenCount(parent) > 0: return term = self.GetPyData(parent)[1] data = google.doGoogleSearch(term) count = data.meta.estimatedTotalResultsCount self.SetPyData(parent, [count, term]) for result in data.results : item = self.AppendItem(parent, result.URL) term = re.sub('<[^>]+>','',result.title) term = re.sub('&','&',term) term = re.sub('>','>',term) self.SetPyData(item,[0,term]) self.SetItemHasChildren(item, true) self.SelectItem(parent) def OnLeftDclick(self, event): point = event.GetPosition() item, flags = self.HitTest(point) self.targetURL = self.GetItemText(item) wxShell('start ' + self.targetURL) def OnRightDclick(self, event): dialog = wxTextEntryDialog(None, 'Search for?', \ 'Restart with', '', wxOK) if dialog.ShowModal() == wxID_OK: searchTerm = dialog.GetValue() frame.SetRoot(searchTerm) class CompanionWindow(wxTreeCompanionWindow): def __init__(self, parent, ID, pos=wxDefaultPosition, size=wxDefaultSize, style=0): wxTreeCompanionWindow.__init__(self, parent, ID, pos, size, style) self.SetBackgroundColour("WHITE") # This method is called to draw each item in the value window def DrawItem(self, dc, itemId, rect): tree = self.GetTreeCtrl() if tree: try: l = tree.GetPyData(itemId) count = l[0] if ( count == 0 ): count = '' term = l[1] text = "(%s) %s" % (count, term) pen = wxPen(wxSystemSettings_GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID) dc.SetPen(pen) dc.SetBrush(wxBrush(self.GetBackgroundColour(), wxSOLID)) dc.DrawRectangle(rect.x, rect.y, rect.width+1, rect.height+1) dc.SetTextForeground("BLACK") dc.SetBackgroundMode(wxTRANSPARENT) tw, th = dc.GetTextExtent(text) x = 5 y = rect.y + max(0, (rect.height - th) / 2) dc.DrawText(text, x, y) except: 1 class MainWindow(wxFrame): def __init__(self, parent, id, title): wxFrame.__init__(self, parent, -1, title) scroller = wxSplitterScrolledWindow(self, -1, (50,50), (350, 250), style=wxNO_BORDER | wxCLIP_CHILDREN | wxVSCROLL) splitter = wxThinSplitterWindow(scroller, -1, style=wxSP_3DBORDER | wxCLIP_CHILDREN) splitter.SetSashSize(2) self.tree = URLTree(splitter, -1, style = wxTR_HAS_BUTTONS|wxTR_NO_LINES|wxTR_ROW_LINES|wxNO_BORDER ) valueWindow = CompanionWindow(splitter, -1, style=wxNO_BORDER|wxTR_EDIT_LABELS ) splitter.SplitVertically(self.tree, valueWindow) splitter.SetSashPosition(150) scroller.SetTargetWindow(self.tree) scroller.EnableScrolling(FALSE, FALSE) valueWindow.SetTreeCtrl(self.tree) self.tree.SetCompanionWindow(valueWindow) def SetRoot(self, term): self.tree.DeleteAllItems() root = self.tree.AddRoot(term) self.tree.SetPyData(root, [0, term]) self.tree.SetItemHasChildren(root, true) self.tree.Expand(root) if __name__ == '__main__': app = wxPySimpleApp() frame = MainWindow(None, -1, "Googling by Doctitle") if len(sys.argv) < 2: dialog = wxTextEntryDialog(None, 'Search for?', \ 'Start with', '', wxOK) if dialog.ShowModal() == wxID_OK: searchTerm = dialog.GetValue() else: frame.Destroy() dialog.Destroy() app.ExitMainLoop() sys.exit(0) dialog.Destroy() else: searchTerm = sys.argv[1] frame.SetRoot(searchTerm) frame.Show(true) app.MainLoop()