The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

37 lines
1.4 KiB

  1. #!/usr/bin/env python3
  2. import datetime
  3. import http.client
  4. import itertools
  5. import json
  6. import sys
  7. import time
  8. domain = sys.argv[1]
  9. apiUrlBase = 'https://{}/api/v1/accounts/'.format(domain)
  10. connection = http.client.HTTPSConnection(domain)
  11. try:
  12. consecutive404s = 0
  13. for i in itertools.count(start = 1):
  14. connection.request('GET', '/api/v1/accounts/{}'.format(i))
  15. response = connection.getresponse()
  16. data = response.read()
  17. if response.status == 200:
  18. j = json.loads(data)
  19. print(i, j['url'])
  20. if response.status == 404:
  21. consecutive404s += 1
  22. else:
  23. consecutive404s = 0
  24. # If we got enough consecutive 404s, we likely reached the end of the list.
  25. # For large instances, this happens when the last 1 % of scanned IDs don't exist.
  26. # For small instances, at least 100 IDs need to fail.
  27. # 458.211 is the solution to 0.01 * i + 100 / i ** (1 / x) = i with i = 100 (analytical form from Wolfram|Alpha: 2*ln(10)/(2*ln(2)-2*ln(3)+2*ln(5)-ln(11)))
  28. consecutive404threshold = 0.01 * i + 100 / i ** (1 / 458.211)
  29. print(f'{datetime.datetime.now():%Y-%m-%d %H:%M:%S} Account {i}: {response.status} {response.reason} [404s: {consecutive404s}/{consecutive404threshold:.2f}]', file = sys.stderr)
  30. if consecutive404s >= consecutive404threshold:
  31. break
  32. if int(response.getheader('X-RateLimit-Remaining')) < 10:
  33. time.sleep(60) #TODO sleep until X-RateLimit-Reset
  34. finally:
  35. connection.close()