Advertisement
Guest User

RPi_Transmission_Torrent_Data_Collection

a guest
Jun 11th, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # import pdb
  4.  
  5. import re
  6. from time import gmtime, strftime, sleep
  7.  
  8.  
  9.  
  10. def write_data(new_datapoint):
  11.         output_path = '/media/USBHDD/PythonStudy/torrent_data_collection/data_one.csv'
  12.         outfile = open(output_path, 'a')
  13.         outfile.write(new_datapoint)
  14.         outfile.close()
  15.  
  16.  
  17. forever = 0
  18. previous_data = "0"
  19.  
  20. # for debugging: pdb.set_trace()
  21.  
  22. while forever < 1:
  23.         input_path = '/var/lib/transmission-daemon/info/stats.json'
  24.         infile = open(input_path, "r")
  25.         infile.seek(0)
  26.         contents = infile.read()
  27.  
  28.         uploaded_bytes = re.search('"uploaded-bytes":\s(\d+)', contents)
  29.  
  30.         if uploaded_bytes:
  31.                 current_time = strftime("%Y-%m-%d %X", gmtime())
  32.                 current_data = uploaded_bytes.group(1)
  33.                 if current_data != previous_data:
  34.                         write_data(","+ current_time + "$" + uploaded_bytes.group(1))
  35.                         previous_data = uploaded_bytes.group(1)
  36.                 infile.close()
  37.                 sleep(5)
  38.         else:
  39.                 print "couldn't write" + current_time
  40.                 infile.close
  41.                 sleep(60)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement