misc.py 661 B

12345678910111213141516171819202122232425262728293031
  1. import os, json, requests
  2. from datetime import datetime
  3. from loguru import logger
  4. def mkdir_if_not_exists(folder):
  5. if not os.path.exists(folder):
  6. os.makedirs(folder)
  7. def expand_url(url):
  8. # expand short URL links
  9. if 'https://t.co/' in url:
  10. try:
  11. r = requests.get(url)
  12. logger.debug(f'Expanded url {url} to {r.url}')
  13. return r.url
  14. except:
  15. logger.error(f'Failed to expand url {url}')
  16. return url
  17. def getattr_or(o: object, prop: str, default=None):
  18. try:
  19. res = getattr(o, prop)
  20. if res is None: raise
  21. return res
  22. except:
  23. return default