#!/usr/bin/env python
import sys
+import getopt
import re
-from auto.db.DatabaseWriter import DatabaseWriter
+from DatabaseWriter import DatabaseWriter
+DEBUG = False
def usage():
print "Usage: python StatusParser.py -i|--id id status_file"
return False
return True
-def libtorrent_parse_status_line(line):
+def libtorrent_canon_num_peers(non_canon_value):
+ return int(non_canon_value)
+
+def libtorrent_canon_dht(non_canon_value):
+ return int(non_canon_value)
+
+# 119.51kb/s -> 119
+def libtorrent_canon_download_speed(non_canon_value):
+ return int(float(non_canon_value.strip("kb/s")))
+
+# 12119.51kb/s -> 12119
+def libtorrent_canon_upload_speed(non_canon_value):
+ return int(float(non_canon_value.strip("kb/s")))
+
+# 698mb -> 698*1024*1024
+def libtorrent_canon_download_size(non_canon_value):
+ return int(non_canon_value.strip("mb")) * 1024 * 1024
+
+# 492mb -> 492*1024*1024
+def libtorrent_canon_upload_size(non_canon_value):
+ return int(non_canon_value.strip("mb")) * 1024 * 1024
+
+# 1h 38m 37s -> [0, 1, 38, 37]
+# 3d 5h 24m 34s -> [3, 5, 24, 34]
+def libtorrent_canon_eta(non_canon_value):
+ eta_string_array = re.split('\ *[dhms]\ *', non_canon_value)
+ eta_string_array.remove('')
+ eta = []
+ for i in range(0, len(eta_string_array)):
+ eta.append(int(eta_string_array[i]))
+ for i in range(len(eta_string_array), 4):
+ eta.insert(0, 0)
+ return eta
+
+#
+# sample libtorrent status line
+# ps: 1, dht: 8 <> dl: 119.51kb/s, ul: 3.63kb/s <> dld: 1mb, uld: 0mb, size: 698mb <> eta: 1h 39m 37s
+#
-def libtorrent_parse_status_file(database, client_sesion_id, filename):
+def libtorrent_parse_status_line(line):
+ num_peers = 0
+ dht = 0
+ download_speed = 0
+ upload_speed = 0
+ download_size = 0
+ upload_size = 0
+ eta = 0
+
+ string_array = re.split("\ *[,<>]+\ *", line)
+ if DEBUG == True:
+ print "string_array is: ", string_array
+
+ for string in string_array:
+ pair = re.split("\ *:\ *", string)
+ if pair[0] == "ps":
+ num_peers = libtorrent_canon_num_peers(pair[1])
+ if pair[0] == "dht":
+ dht = libtorrent_canon_dht(pair[1])
+ if pair[0] == "dl":
+ download_speed = libtorrent_canon_download_speed(pair[1])
+ if pair[0] == "ul":
+ upload_speed = libtorrent_canon_upload_speed(pair[1])
+ if pair[0] == "dld":
+ download_size = libtorrent_canon_download_size(pair[1])
+ if pair[0] == "uld":
+ upload_size = libtorrent_canon_upload_size(pair[1])
+ if pair[0] == "size":
+ pass
+ if pair[0] == "eta":
+ eta = libtorrent_canon_eta(pair[1])
+
+ return (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta)
+
+def libtorrent_parse_status_file(database, client_session_id, filename):
dbw = DatabaseWriter(database)
try:
fin = open(filename, "r")
while 1:
- line = fin.readline.strip()
+ line = fin.readline()
+ if not line:
+ break
+ line = line.strip()
if libtorrent_is_status_line(line) == False:
continue
- (date, time, peer_nume, dht, download_speed, upload_speed, download_size, upload_size, eta_time) = libtorrent_parse_status_line(line)
-
- dbw.add_status_message(client_session_id, date, time, peer_num, dht, download_speed, upload_speed, download_size, upload_size, eta_time):
+ (num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta_time) = libtorrent_parse_status_line(line)
+ if DEBUG == True:
+ print "(%d, %d, %d kb/s, %d kb/s, %d bytes, %d bytes)" % (num_peers, dht, download_speed, upload_speed, download_size, upload_size)
+
+# dbw.add_status_message(client_session_id, date, time, num_peers, dht, download_speed, upload_speed, download_size, upload_size, eta_time):
except IOError:
print "Error processing file %s." %filename