certstream_consumer.py 863 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import redis
  2. import json
  3. r = redis.Redis()
  4. count = 0
  5. rules = {}
  6. while True:
  7. if count % 1000 == 0:
  8. count = 0
  9. todel = 1
  10. while (toadd := r.lpop('toadd')) is not None:
  11. toadd = json.loads(toadd.decode('ascii'))
  12. rules[toadd['id']] = toadd['value']
  13. while (todel := r.lpop('todel')) is not None:
  14. try:
  15. del rules[todel.decode('ascii')]
  16. except:
  17. pass
  18. domain = r.blpop('certstream')[1].decode('ascii')
  19. for rule in rules.values():
  20. notify = False
  21. if rule['t'] == 0:
  22. if rule['v'] in domain:
  23. notify = True
  24. elif rule['t'] == 1:
  25. if domain.startswith(rule['v']):
  26. notify = True
  27. elif rule['t'] == 2:
  28. if domain.endswith(rule['v']):
  29. notify = True
  30. if notify:
  31. print(domain)
  32. r.rpush('notifications', json.dumps({"domain": domain, "chats": rule['c']}))
  33. count += 1