iconfetch.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # python function that takes address
  2. # parses it for the youtube icon
  3. # via wget site.com/favicon.ico
  4. # saves it with propername for reference
  5. # later on
  6. import subprocess as sp
  7. # search via regex
  8. import re, os
  9. def check_icon(sitename):
  10. # checks for site icon in storage
  11. str1 = sitename.replace('/', '')
  12. sitename = f'{str1.replace(":", ".")}.ico'
  13. path = 'static/sitecons/'
  14. # list comp for searching through dir for file
  15. result = [os.path.join(root, sitename) for root, dirs, files in os.walk(path) if sitename in files]
  16. if result:
  17. return f'{sitename}'
  18. else:
  19. return False
  20. def fetch_icon(sitename):
  21. path = 'static/sitecons/'
  22. # search end of string for leading slash
  23. search_slash = re.findall('\/$', sitename)
  24. # if theres a slash, no need to append, otherwise
  25. if search_slash:
  26. parsed_name = f'{sitename}favicon.ico'
  27. else:
  28. parsed_name = f'{sitename}/favicon.ico'
  29. # remove chars for icon filename
  30. str1 = sitename.replace('/', '')
  31. sitename = str1.replace(':', '.')
  32. sp.run(f'wget {parsed_name} -O static/sitecons/{sitename}.ico', shell=True)
  33. return f'{sitename}.ico'