Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 11th, 2014  |  syntax: None  |  size: 5.38 KB  |  views: 77  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. This text file consists of three Python files. The first is a very slightly adapted code fount on github, which listens for new blocks from the blockchain.info websocket API. Change the file output location to whatever you prefer, and let it run as long as you want to collect as many blocks as you need. The next two files must be run in sequence (you can leave the first running if you wish). File 2 takes the output of the listener routine and turns it into CSV timestamps. File 3 takes that and turns it into a CSV of time-between-blocks. This output can then be analyzed in whatever way you wish.
  2.  
  3. # FILE 1: bitcoin_listener
  4. # -*- coding: utf-8 -*-
  5. # by fcicq <fcicq@fcicq.net>, 2013.4.7, GPLv2
  6. # Tornado 3.0 is required.
  7. """
  8. Original code at: https://gist.github.com/fcicq/5328876/
  9. Extremely minor modifications by Matt Springer
  10. """
  11.  
  12. from tornado.websocket import websocket_connect, WebSocketClientConnection
  13. from tornado.ioloop import IOLoop
  14. from datetime import timedelta
  15. import time
  16.  
  17. #echo_uri = 'ws://echo.websocket.org'
  18. echo_uri = 'ws://ws.blockchain.info/inv'
  19. PING_TIMEOUT = 15
  20. class myws():
  21.   conn = None
  22.   keepalive = None
  23.   def __init__(self, uri):
  24.     self.uri = uri
  25.     self.doconn()
  26.   def doconn(self):
  27.     w = websocket_connect(self.uri)
  28.     w.add_done_callback(self.wsconnection_cb)
  29.   def dokeepalive(self):
  30.     stream = self.conn.protocol.stream
  31.     if not stream.closed():
  32.       self.keepalive = stream.io_loop.add_timeout(timedelta(seconds=PING_TIMEOUT), self.dokeepalive)
  33.       self.conn.protocol.write_ping("")
  34.     else:
  35.       self.keepalive = None # should never happen
  36.   def wsconnection_cb(self, conn):
  37.     self.conn = conn.result()
  38.     self.conn.on_message = self.message
  39.     self.conn.write_message('{"op":"ping_block"}')
  40.     self.conn.write_message('{"op":"blocks_sub"}')
  41. #    self.conn.write_message('{"op":"unconfirmed_sub"}')
  42.     self.keepalive = IOLoop.instance().add_timeout(timedelta(seconds=PING_TIMEOUT), self.dokeepalive)
  43.   def message(self, message):
  44.     if message is not None:
  45.       output = str(message) + str(time.time())  
  46.       print(output)
  47.       with open('c:\users\matt\desktop\log.txt', 'a') as the_file:
  48.           the_file.write(output)
  49.           the_file.write('\n')
  50.           the_file.close()
  51.     else:
  52.       self.close()
  53.   def close(self):
  54.     print('conn closed')
  55.     if self.keepalive is not None:
  56.       keepalive = self.keepalive
  57.       self.keepalive = None
  58.       IOLoop.instance().remove_timeout(keepalive)
  59.     self.doconn()
  60.  
  61. import signal
  62. def main():
  63.   try:
  64.     io_loop = IOLoop.instance()
  65.     signal.signal(signal.SIGTERM, io_loop.stop)
  66.     myws(echo_uri)
  67.     IOLoop.instance().start()
  68.   except KeyboardInterrupt:
  69.     io_loop.stop()
  70.  
  71. if __name__ == '__main__':
  72.   main()
  73.  
  74. # FILE 2: bitcoin_parser
  75. # -*- coding: utf-8 -*-
  76. """
  77. Coded by Matt Springer
  78. This work is licensed under a Creative Commons Attribution 4.0 International License.
  79. """
  80. from __future__ import print_function
  81. import re
  82.  
  83. def getHeight(string):
  84.     '''Regex which matches the 6 digits following "height" in the received block'''
  85.     regex = re.compile("(?<=\"height\":)[0-9]{6}")
  86.     result = regex.findall(string)
  87.     return int(result[0])
  88.  
  89. def getReportedTime(string):
  90.     '''Extacts the number after the first "time" in the received block'''
  91.     regex = re.compile("(?<=\"time\":)[0-9]{10}")
  92.     result = regex.findall(string)
  93.     return int(result[0])
  94.    
  95. def getBlockchainTime(string):
  96.     '''Extacts the number after the second "time" in the received block '''
  97.     regex = re.compile("(?<=\"time\":)[0-9]{10}")
  98.     result = regex.findall(string)
  99.     return int(result[1])
  100.    
  101. def getMyTime(string):
  102.     '''Extacts the number after the three}}}, which is the local machine timestamp I appended in bitcoin_listener'''
  103.     regex = re.compile("(?<=\}\}\})[0-9]{10}")
  104.     result = regex.findall(string)
  105.     return int(result[0])
  106.  
  107.  
  108. def main():
  109.     '''prints comma-separated data in the form [block height, internal block timestamp, blockchain.info timestame, local machine timestamp] I only use the local timestamp'''
  110.     f = open('c:\users\matt\desktop\log.txt', 'r')
  111.     g = open('c:\users\matt\desktop\processed.txt', 'w')
  112.     counter = 0    
  113.     for line in f:
  114.         print(str(getHeight(line)) + ',' + str(getReportedTime(line)) + ',' + str(getBlockchainTime(line)) + ',' + str(getMyTime(line)))
  115.         g.write(str(getHeight(line)) + ',' + str(getReportedTime(line)) + ',' + str(getBlockchainTime(line)) + ',' + str(getMyTime(line)) +'\n')
  116.         counter += 1
  117.     f.close()
  118.     g.close()
  119.     print(counter)
  120.  
  121. main()
  122.  
  123. # FILE 3: bitcoin_parser
  124. # -*- coding: utf-8 -*-
  125. """
  126. Coded by Matt Springer
  127. This work is licensed under a Creative Commons Attribution 4.0 International License.
  128. """
  129. from __future__ import print_function
  130. import csv
  131.  
  132. def main():
  133.     indexArray = []
  134.     timeArray = []    
  135.     with open('c:\users\matt\desktop\processed.txt', 'rb') as csvfile:
  136.         reader = csv.reader(csvfile, delimiter=',')
  137.         for row in reader:
  138.             indexArray.append(int(row[0]))            
  139.             timeArray.append(int(row[3]))
  140.        
  141.     for index in range(len(timeArray)-1):
  142.         '''the "if" statement rejects non-consecutive blocks'''
  143.         if(indexArray[index + 1] == indexArray[index] + 1):
  144.             print(indexArray[index], end=',')            
  145.             print(timeArray[index + 1] - timeArray[index])
  146. main()
clone this paste RAW Paste Data