Преглед изворни кода

figure out if files are from a search instead of storing state

david пре 7 година
родитељ
комит
c7080a42dc
2 измењених фајлова са 13 додато и 6 уклоњено
  1. 9 3
      bot.py
  2. 4 3
      ircclient.py

+ 9 - 3
bot.py

@@ -29,10 +29,16 @@ def get_books_from_list(filename):
     ret = list(set(ret)) # dedup
     return ret
 
-
-def handle_files(files, mode):
+def mode_from_files(files):
+    for f in files:
+        f = f.lower()
+        if 'searchbot' in f or 'searchook' in f.lower():
+            return MODE_SEARCH
+    return MODE_BOOK
+def handle_files(files):
     log.info("Unarchived files %s", files)
     out = []
+    mode = mode_from_files(files)
     if mode == MODE_SEARCH:
         for f in files:
             if "searchbot" not in f.lower() and "searchook" not in f.lower():
@@ -59,7 +65,7 @@ def handle_results(q):
         item = q.get()
         log.info("Got a result! %s", item)
         if item['type'] == 'files':
-            handle_files(item['files'], item['mode'])
+            handle_files(item['files'])
 def main():
     q = queue.Queue()
     rq = queue.Queue()

+ 4 - 3
ircclient.py

@@ -103,7 +103,6 @@ class IRCClient(Thread):
             return
         command = self.command_queue.get()
         log.info("command %s", command)
-        self.mode = command['mode']
 
         if command['mode'] == MODE_SEARCH:
             self.send_queue.put("PRIVMSG %s :@searchook %s " % (self.CHANNEL, command['query']))
@@ -178,7 +177,7 @@ class IRCClient(Thread):
                 downloaded_filename = self.netcat(ip, port, size, filename)
                 # TODO save state in redis on ip port size filename + output of handle files
                 files = unar(downloaded_filename, self.PATH)
-                self.results_queue.put({'type': 'files', 'files': files, 'mode': self.mode})
+                self.results_queue.put({'type': 'files', 'files': files})
 
             if comm == "PING" or msg_from == "PING":  # respond ping to avoid getting kicked
                 self.pong(line)
@@ -199,6 +198,7 @@ class IRCClient(Thread):
         fname = os.path.join(self.PATH, filename)
         f = open(fname, 'wb')
         count = 0
+        last_perc = 0
         while True:
             data = s.recv(16384)
             if len(data) == 0:
@@ -208,8 +208,9 @@ class IRCClient(Thread):
             f.write(data)
             perc = int(100 * count / size)
             self.set_status("DOWNLOADING")  # % perc #progress
-            if perc % 10 == 0:
+            if perc % 10 == 0 and perc != last_perc:
                 log.info("Download percentage: %d", perc)
+                last_perc = perc
             if count >= size:
                 break
         s.close()