get_styles.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. import requests
  3. from . import common
  4. from . import tsdb
  5. session = requests.Session()
  6. def get_styles(subreddit):
  7. (database, subreddit) = tsdb.TSDB.for_subreddit(subreddit, fix_name=True)
  8. print('Getting styles for /r/%s' % subreddit)
  9. subreddit = common.r.subreddit(subreddit)
  10. styles = subreddit.stylesheet()
  11. database.styles_dir.makedirs(exist_ok=True)
  12. stylesheet_filepath = database.styles_dir.with_child('stylesheet.css')
  13. print('Downloading %s' % stylesheet_filepath.relative_path)
  14. with stylesheet_filepath.open('w', encoding='utf-8') as stylesheet:
  15. stylesheet.write(styles.stylesheet)
  16. for image in styles.images:
  17. image_basename = image['name'] + '.' + image['url'].split('.')[-1]
  18. image_filepath = database.styles_dir.with_child(image_basename)
  19. print('Downloading %s' % image_filepath.relative_path)
  20. with image_filepath.open('wb') as image_file:
  21. response = session.get(image['url'])
  22. image_file.write(response.content)
  23. def get_styles_argparse(args):
  24. return get_styles(args.subreddit)