Sunday, October 28, 2007

Dhiraagu edirectory lister 3rd update



Changes:

  • "Get All" will show the results in a single block instead of page by page.
So it's possible to redirect the output to a file in a single go.
For instance
vishah@vishah-desktop:~$ python edir.py >edirlog.txt

If the query gives a large set of results, the program might appear frozen, because threading is not implemented yet..give it some time and it'll hopefully complete.

  • Click the code.



# ____________________________________
#/ dhiraagu edirectory lister 3 rd    \
#\ version                            /
# ------------------------------------
#
import pygtk
pygtk.require("2.0")
import gtk
import urllib2
import urllib
import sgmllib
import re
import cookielib

class MyParser(sgmllib.SGMLParser):
       def __init__(self, verbose=0):
               sgmllib.SGMLParser.__init__(self, verbose)
               self.numbers = []
               self.bumbers = []
               self.inside_td_element = 0
               self.inside_tdno_element = 0
               self
       def parse(self, data):
               self.feed(data)
               self.close()

       def start_td(self, attributes):
               if attributes == []:
                       self.inside_td_element = 1
               else:
                       self.inside_tdno_element = 1

       def end_td(self):
               self.inside_td_element = 0
               self.inside_tdno_element = 0

       def handle_data(self, data):
               if self.inside_td_element == 1:
                       self.numbers.append(data)
               if self.inside_tdno_element == 1:
                       self.bumbers.append(data)

       def get_numbers(self):
               return self.numbers

       def get_bumbers(self):
               return self.bumbers



       phonenumbers=""
class duck:
       def clicky(self, widget, textbuffer, entry, next=None,first = None,listall = None):

               urlobj = None
               urlobjdata = ""
               cmpobjres = None
               cmpobj = None
               global phonenumbers

               if first != None:
                       urlobj = urllib2.urlopen('http://www.dhivehinet.net.mv/index.php', urllib.urlencode({'action':'edir', 'query':entry.get_text(), 'searchtype':'number'}))
                       for i in range(0,len(self.button)):
                               self.button[i].destroy()
                       self.button=[]

               else:
                       self.cj.load('/tmp/edircookie')
                       urlobj = urllib2.urlopen('http://www.dhivehinet.net.mv/index.php', urllib.urlencode({'action':'edir','page':next}))

               self.cj.save('/tmp/edircookie')

               urlobjdata = urlobj.read()
               if (int(self.nextpage) == int(next)) or ( first != None ):
                       cmpobj = re.compile(r'<a id=\"nextPage\" href=\".+?page=([0-9]{1,})\"', re.DOTALL)
                       cmpobjres = cmpobj.search(urlobjdata)

               if cmpobjres is not None:
                       print "sssssssssssss"
                       self.nextpage = cmpobjres.group(1)
                       buttonindex = int(self.nextpage)
                       if listall == None:
                               self.button.append(gtk.Button(self.nextpage))
                               print buttonindex
                               self.hbox.pack_start(self.button[buttonindex-1],True,True,0)
                               self.button[buttonindex-1].connect("clicked",self.clicky,textbuffer, entry, buttonindex )
                               self.button[buttonindex-1].show()
                       else:
                               self.clicky(widget, textbuffer, entry, buttonindex, None, "yes")
               else:
                       ass = "true"

               diParser = MyParser()
               cmpobj = re.compile(r"id=\"edir\".+?table>", re.DOTALL)
               feeddata = cmpobj.search(urlobjdata).group()
               diParser.feed(feeddata)
               asw = diParser.get_numbers()
               asd = diParser.get_bumbers()
               prrr = ''
               if listall == None:
                       for i, j in zip(asw, asd):
                               prrr = prrr + "\n" + j.ljust(12,' ') + i.ljust(20, ' ')
                       textbuffer.set_text(prrr)
               else:
                       for i, j in zip(asw, asd):
                               prrr = prrr + "\n" + j.ljust(12,' ') + i.ljust(20, ' ')
                       phonenumbers = phonenumbers + prrr
                       print phonenumbers
               if int(self.nextpage) != int(next):
                       textbuffer.set_text(phonenumbers)

       def closethingy(self, widget, event, data=None):
               gtk.main_quit()
       def __init__(self):
               self.button = []
               self.cj = cookielib.LWPCookieJar()
               opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
               urllib2.install_opener(opener)
               self.nextpage=0
               self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
               self.window.set_title("Dhiraagu edirectory lister")
               self.window.connect("delete_event", self.closethingy)
               self.vbox = gtk.VBox(False, 0)
               self.hbox = gtk.HBox(True,0)
               entry = gtk.Entry(50)
               self.vbox.pack_start(entry, False, False, 0)
               button = gtk.Button("Get List")
               buttonall = gtk.Button("Get All")
               textview = gtk.TextView()
               textbuffer = textview.get_buffer()
               button.connect("clicked", self.clicky, textbuffer, entry,0,"yes")
               buttonall.connect("clicked", self.clicky, textbuffer, entry,0,"yes","yes")
               scrolledwindow = gtk.ScrolledWindow()
               scrolledwindow.set_policy(gtk.POLICY_ALWAYS,gtk.POLICY_ALWAYS)
               scrolledwindow.add_with_viewport(textview)
               self.vbox.pack_start(button, False, False, 0)
               self.vbox.pack_start(buttonall, False, False, 0)
               self.vbox.pack_start(scrolledwindow, True, True, 10)
               self.vbox.pack_start(self.hbox,False,False,0)
               self.window.add(self.vbox)
               self.window.set_border_width(3)
               self.window.resize(700, 500)
               self.window.show_all()


if __name__ == "__main__":
       duck()
       gtk.main()


3 comments:

@i said...

good one :P

Anonymous said...

tx for sharing :)

Anonymous said...

pal its cool