def log(self, permid, decoded_message):        
     lt = T.localtime(T.time())
     timestamp = "%04d-%02d-%02d %02d:%02d:%02d" % (lt[0], lt[1], lt[2], lt[3], lt[4], lt[5])
     ip = self.peer_db.getPeer(permid, "ip")
     #ip = "x.y.z.1"
     s = "%s\t%s\t%s\t%s\n"% (timestamp, bin2str(permid), ip, decoded_message)
     
     print dunno2unicode(s)
     self.logfile.write(dunno2unicode(s)) # bin2str(
     self.logfile.flush()
Exemplo n.º 2
0
    def process_query(self, permid, d, selversion):
        hits = None
        p = None
        sendtorrents = False

        netwq = d['q']
        if netwq.startswith("SIMPLE"): # remote query
            # Format: 'SIMPLE '+string of space separated keywords or
            #         'SIMPLE+METADATA' +string of space separated keywords
            #
            # In the future we could support full SQL queries:
            # SELECT infohash,torrent_name FROM torrent_db WHERE status = ALIVE
            
            if netwq.startswith('SIMPLE+METADATA'):
                q = d['q'][len('SIMPLE+METADATA '):]
                sendtorrents = True
            else:
                q = d['q'][len('SIMPLE '):]
                    
            q = self.clean_netwq(q)
            q = dunno2unicode(q)
            kws = re.split(r'\W+', q.lower())
            hits = self.search_torrents(kws, maxhits=MAX_RESULTS,sendtorrents=sendtorrents)
            p = self.create_remote_query_reply(d['id'],hits,selversion)
            
        elif netwq.startswith("CHANNEL"): # channel query
            q = d['q'][len('CHANNEL '):]
            q = self.clean_netwq(q)
            q = dunno2unicode(q)
            hits = self.channelcast_db.searchChannels(q)
            p = self.create_channel_query_reply(d['id'],hits,selversion)

        # log incoming query, if logfile is set
        if self.logfile:
            self.log(permid, q)        
     
        m = QUERY_REPLY+p

        if self.overlay_log:
            nqueries = self.get_peer_nqueries(permid)
            # RECV_MSG PERMID OVERSION NUM_QUERIES MSG
            self.overlay_log('RECV_QRY', show_permid(permid), selversion, nqueries, repr(d))

            # RPLY_QRY PERMID NUM_HITS MSG
            self.overlay_log('RPLY_QRY', show_permid(permid), len(hits), repr(p))

        self.overlay_bridge.send(permid, m, self.send_callback)
        
        self.inc_peer_nqueries(permid)
    def process_query(self, permid, d, selversion):
        q = d['q'][len('SIMPLE '):]
        q = dunno2unicode(q)
        # Format: 'SIMPLE '+string of space separated keywords
        # In the future we could support full SQL queries:
        # SELECT infohash,torrent_name FROM torrent_db WHERE status = ALIVE
        kws = q.split()
        hits = self.search_manager.search(kws, maxhits=MAX_RESULTS)

        p = self.create_query_reply(d['id'], hits)
        m = QUERY_REPLY + p

        if self.overlay_log:
            nqueries = self.get_peer_nqueries(permid)
            # RECV_MSG PERMID OVERSION NUM_QUERIES MSG
            self.overlay_log('RECV_QRY', show_permid(permid), selversion,
                             nqueries, repr(d))

            # RPLY_QRY PERMID NUM_HITS MSG
            self.overlay_log('RPLY_QRY', show_permid(permid), len(hits),
                             repr(p))

        self.overlay_bridge.send(permid, m, self.send_callback)

        self.inc_peer_nqueries(permid)
Exemplo n.º 4
0
 def get_comment_as_unicode(self):
     """ Returns the comment field of the def as a unicode string.
     @return A Unicode string. """
     return dunno2unicode(self.input['comment'])
Exemplo n.º 5
0
    def reSortAll(self, data, parent=None):

        max = len(data)
        if max == 0:
            return
        import wx
        dlgHolder = []
        event = Event()

        def makeDialog():
            dlg = wx.ProgressDialog("Upgrading Database",
                                    "Upgrading Old Database to New Database",
                                    maximum=max,
                                    parent=None,
                                    style=wx.PD_AUTO_HIDE
                                    | wx.PD_ELAPSED_TIME
                                    | wx.PD_REMAINING_TIME)
            dlgHolder.append(dlg)
            event.set()

        self.invoker.invokeLater(makeDialog)

        # Wait for dialog to be ready
        event.wait()
        dlg = dlgHolder[0]

        count = 0
        step = int(float(max) / 20) + 1

        # sort each torrent file
        for i in xrange(len(data)):
            count += 1
            if count % step == 0:
                self.invoker.invokeLater(dlg.Update, [count])
            try:
                # try alternative dir if bsddb doesnt match with current Tribler install
                rec = data[i]
                (torrent_dir, torrent_name
                 ) = self.metadata_handler.get_std_torrent_dir_name(rec)

                # read the torrent file
                filesrc = os.path.join(torrent_dir, torrent_name)

                #                print filesrc
                f = open(filesrc, "rb")
                torrentdata = f.read()  # torrent decoded string
                f.close()
            except IOError:  # torrent file not found
                # delete the info from db
                self.torrent_db.deleteTorrent(data[i]['infohash'])
                continue

            # decode the data
            torrent_dict = bencode.bdecode(torrentdata)
            content_name = dunno2unicode(torrent_dict["info"].get('name', '?'))

            category_belong = []
            category_belong = self.calculateCategory(torrent_dict,
                                                     content_name)

            if (category_belong == []):
                category_belong = ['other']

            data[i][
                'category'] = category_belong  # should have updated self.data
            self.torrent_db.updateTorrent(data[i]['infohash'],
                                          updateFlag=False,
                                          category=category_belong)
        self.torrent_db.sync()
        self.invoker.invokeLater(dlg.Destroy)
Exemplo n.º 6
0
 def get_comment_as_unicode(self):
     """ Returns the comment field of the def as a unicode string.
     @return A Unicode string. """
     return dunno2unicode(self.input['comment'])
Exemplo n.º 7
0
    def reSortAll(self, data, parent = None):
         
        max = len(data)
        if max == 0:
            return
        import wx
        dlgHolder = []
        event = Event()
        def makeDialog():
            dlg = wx.ProgressDialog("Upgrading Database",
                                    "Upgrading Old Database to New Database",
                                    maximum = max,
                                    parent = None,
                                    style = wx.PD_AUTO_HIDE 
                                    | wx.PD_ELAPSED_TIME
                                    | wx.PD_REMAINING_TIME
                                    )
            dlgHolder.append(dlg)
            event.set()
            
            
        self.invoker.invokeLater(makeDialog)
        
        # Wait for dialog to be ready
        event.wait()
        dlg = dlgHolder[0]
        
        count = 0
        step = int(float(max) / 20) + 1
        
        # sort each torrent file
        for i in xrange(len(data)):
            count += 1
            if count % step == 0:
                self.invoker.invokeLater(dlg.Update, [count])
            try:
                # try alternative dir if bsddb doesnt match with current Tribler install
                rec = data[i]
                (torrent_dir,torrent_name) = self.metadata_handler.get_std_torrent_dir_name(rec)
                    
                # read the torrent file
                filesrc = os.path.join(torrent_dir,torrent_name)
                
#                print filesrc
                f = open(filesrc, "rb")
                torrentdata = f.read()          # torrent decoded string
                f.close()
            except IOError:                     # torrent file not found
                # delete the info from db
                self.torrent_db.deleteTorrent(data[i]['infohash'])
                continue   
            
            # decode the data
            torrent_dict = bencode.bdecode(torrentdata)
            content_name = dunno2unicode(torrent_dict["info"].get('name', '?'))
            
            category_belong = []
            category_belong = self.calculateCategory(torrent_dict, content_name)
            
            if (category_belong == []):
                category_belong = ['other']
            
            data[i]['category'] = category_belong    # should have updated self.data
            self.torrent_db.updateTorrent(data[i]['infohash'], updateFlag=False, category=category_belong)
        self.torrent_db.sync()
        self.invoker.invokeLater(dlg.Destroy)   
Exemplo n.º 8
0
 def test_unicode_dunno_3(self):
     self.assertIsInstance(dunno2unicode({}), unicode)
Exemplo n.º 9
0
 def test_unicode_dunno_2(self):
     self.assertIsInstance(dunno2unicode(u"test"), unicode)