Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import requests
  2. import json
  3. import time
  4.  
  5. base_url = 'https://bitnodes.earn.com/api/v1/snapshots/'
  6. timestamp_dict = {}
  7. stop_date = 1551398088
  8.  
  9. def collect_timestamps_dict(page):
  10. print('Collecting page', page, '...')
  11. data = requests.get(url = base_url, params = {'page':page,'limit':100}).json()
  12. next_page = data['next']
  13. for d in data['results']:
  14. timestamp = d['timestamp']
  15. url = d['url']
  16. print(timestamp, url)
  17. timestamp_dict[timestamp] = url
  18. if int(timestamp) <= stop_date:
  19. return
  20. if timestamp > stop_date:
  21. time.sleep(1)
  22. collect_timestamps_dict(page + 1)
  23.  
  24. def collect_specific_snapshot(snapshot):
  25. print('Collecting snapshot', snapshot, '...')
  26. raw_data = requests.get(url = base_url + str(snapshot)).json()
  27. saved_ip = ''
  28. nodes = raw_data['nodes']
  29. for n in nodes:
  30. if ']' in n:
  31. n = n.split(']:')[0].replace('[','')
  32. else:
  33. n = n.split(':')[0]
  34. saved_ip = saved_ip + n + '\n'
  35. open('2019-03-' + snapshot + '.txt', 'w').write(saved_ip)
  36.  
  37. # First, collect all timestamps as json file
  38. collect_timestamps_dict(1)
  39. with open('timestamps.json', 'w') as fp:
  40. json.dump(timestamp_dict, fp)
  41.  
  42. # Then collect all snapshot as json file
  43. count = 0
  44. with open('timestamps.json') as f:
  45. data = json.load(f)
  46. for d in data:
  47. count += 1
  48. time.sleep(1)
  49. print(count)
  50. collect_specific_snapshot(d)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement