Changeset 3:48f51eae9e71 in misc


Ignore:
Timestamp:
02/10/07 06:35:43 (5 years ago)
Author:
Eriol
Branch:
misc
Convert:
svn:e60e002c-0983-44b9-b2ae-8842d539f768/misc@6
Message:

Changed save file format of the graph: now it's a dot file.
Added caching in both channels and nicks to avoid flood.
There is a problem with SwircBot?.knownchannels.append(channel)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • swirc.py

    r2 r3  
    2929 
    3030import sys 
     31import pydot 
    3132from twisted.words.protocols import irc 
    3233from twisted.internet import reactor, protocol 
     
    4344 
    4445    def addNode(self, node, reachable): 
    45         print node, reachable 
    4646        try: 
    4747            self.data[node] 
     
    4949            self.data[node] = reachable 
    5050        else: 
    51             self.data[node] += reachable 
     51            for e in reachable: 
     52                if e not in self.data[node]: 
     53                    self.data[node] += reachable 
    5254 
    5355class MakeGraph(object): 
     
    5658        self.graph = Graph() 
    5759 
    58     def fromList(self, params): 
    59         ''' ['swircbot', '=', '#etna', 'swircbot @mancausoft 
    60             @Eriol @KatolaZ @substa @AlpT_ '] ''' 
    61  
    62         nickslist = [] 
    63         for nick in params[3].split(): 
    64             if nick == BOT_NICKNAME: 
    65                 continue 
    66             if nick[0] in '@+': 
    67                 nick = nick[1:] 
    68  
    69             nickslist.append(nick) 
     60    def addNick(self, nickslist): 
    7061 
    7162        for nick in nickslist: 
     
    7364                               [node for node in nickslist if node != nick]) 
    7465 
    75     def toPicture(self): 
    76         import pydot 
    77         edges = [(nick, x) for nick in self.graph.data for x in self.graph.data[nick]] 
     66    def _cleanEdges(self): 
     67        edges = [(nick, x) for nick in self.graph.data 
     68                            for x in self.graph.data[nick]] 
    7869 
    79         bon = edges[:] 
     70        cleanedEdges = list(edges) 
    8071 
    8172        for x in edges: 
    82             if (x[1], x[0]) in bon: 
    83                 del bon[bon.index(x)] 
     73            if (x[1], x[0]) in cleanedEdges: 
     74                del cleanedEdges[cleanedEdges.index(x)] 
    8475 
    85         gr = pydot.graph_from_edges(bon) 
    86         gr.write_jpeg('swirc.jpg', prog='dot') 
     76        return cleanedEdges 
    8777 
    88 mgi = MakeGraph() #ugly hack to make the picture of the graph after CTRL + C 
     78    def saveDot(self, fname): 
     79        gr = pydot.graph_from_edges(self._cleanEdges()) 
     80        gr.write_dot(fname, prog='dot') 
     81 
     82mgi = MakeGraph() #ugly hack to make the dot file of the graph after CTRL + C 
    8983 
    9084class SwircBot(irc.IRCClient): 
     
    9387 
    9488    knownchannels = [] 
     89    knownnicks = [] 
    9590    maxdepth = 10 
    9691 
     
    108103        SwircBot.knownchannels.append(channel) 
    109104 
     105    def irc_RPL_ENDOFWHOIS(self, prefix, params): 
     106        nick = params[1] 
     107        SwircBot.knownnicks.append(nick) 
     108 
    110109    def irc_RPL_WHOISCHANNELS(self, prefix, params): 
    111110        for channel in params[2].split(): 
     
    116115                len(SwircBot.knownchannels) < SwircBot.maxdepth): 
    117116                self.join(channel[1:]) 
     117                #print channel 
     118                #print SwircBot.knownchannels 
     119 
    118120 
    119121    def irc_RPL_NAMREPLY(self, prefix, params): 
    120         self.graphmaker.fromList(params) 
     122        ''' ['swirc', '=', '#etna', 'swirc @Eriol @AlpT @mancausoft @markgreene '] ''' 
     123        nickslist = [] 
     124        for nick in params[3].split(): 
     125            if nick == BOT_NICKNAME: 
     126                continue 
     127            if nick[0] in '@+': 
     128                nick = nick[1:] 
    121129 
    122         #print self.graphmaker.graph.data 
     130            nickslist.append(nick) 
    123131 
    124         for nick in self.graphmaker.graph.data: 
    125             self.sendLine("WHOIS %s" % nick) 
     132        self.graphmaker.addNick(nickslist) 
     133 
     134        for nick in nickslist: 
     135            if nick not in SwircBot.knownnicks: 
     136                self.sendLine("WHOIS %s" % nick) 
    126137 
    127138class SwircBotFactory(protocol.ClientFactory): 
     
    147158    reactor.run() 
    148159 
    149     mgi.toPicture() 
     160    mgi.saveDot('swirc.dot') 
Note: See TracChangeset for help on using the changeset viewer.