spacepaste

  1.  
  2. $ cat mymail.py
  3. #!/usr/bin/env python
  4. import os
  5. import imaplib
  6. import subprocess
  7. import argparse
  8. import time
  9. import pathlib
  10. from httplib2 import ServerNotFoundError
  11. from oauth2client import client, file
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument('-b', '--badge', default='\uf0e0')
  14. parser.add_argument('-c', '--color', default='#ff69b4')
  15. parser.add_argument('-m', '--mute', action='store_true')
  16. args = parser.parse_args()
  17. passwordstore = subprocess.check_output(['which', 'pass'])
  18. unread_badge = '%{F' + args.color + '}' + args.badge + ' %{F-}'
  19. error_badge = '%{F' + args.color + '}\uf06a %{F-}'
  20. count_was = 0
  21. def update(count_was):
  22. completed_process = subprocess.run(['pass', 'mail/ak'], capture_output=True, check=True, text=True);
  23. password = completed_process.stdout[:-1]
  24. obj = imaplib.IMAP4_SSL('mail.cock.li',993)
  25. obj.login('ak@redchan.it', password)
  26. obj.select()
  27. count = len(obj.search(None, 'UnSeen')[1][0].split())
  28. if count>0:
  29. print(unread_badge + str(count), flush=True)
  30. else:
  31. print(args.badge, flush=True)
  32. if not args.mute and count_was < count and count > 0:
  33. subprocess.run(['canberra-gtk-play', '-f', '/home/z/bin/music4scripts/deedan.ogg'])
  34. if count > 0:
  35. open('/home/z/.config/polybar/inbox/foo', 'w').close()
  36. else:
  37. if os.path.exists('/home/z/.config/polybar/inbox/foo'):
  38. os.remove('/home/z/.config/polybar/inbox/foo')
  39. return count
  40. while True:
  41. try:
  42. if b'/usr/bin/pass\n' in passwordstore:
  43. count_was = update(count_was)
  44. time.sleep(10)
  45. else:
  46. print(error_badge + 'pass is not installed', flush=True)
  47. time.sleep(2)
  48. except (errors.HttpError, ServerNotFoundError, OSError) as error:
  49. print(error_badge + str(error), flush=True)
  50. time.sleep(5)
  51.