import os import requests from . import common from . import tsdb session = requests.Session() def get_styles(subreddit): (database, subreddit) = tsdb.TSDB.for_subreddit(subreddit, fix_name=True) print('Getting styles for /r/%s' % subreddit) subreddit = common.r.subreddit(subreddit) styles = subreddit.stylesheet() database.styles_dir.makedirs(exist_ok=True) stylesheet_filepath = database.styles_dir.with_child('stylesheet.css') print('Downloading %s' % stylesheet_filepath.relative_path) with stylesheet_filepath.open('w', encoding='utf-8') as stylesheet: stylesheet.write(styles.stylesheet) for image in styles.images: image_basename = image['name'] + '.' + image['url'].split('.')[-1] image_filepath = database.styles_dir.with_child(image_basename) print('Downloading %s' % image_filepath.relative_path) with image_filepath.open('wb') as image_file: response = session.get(image['url']) image_file.write(response.content) def get_styles_argparse(args): return get_styles(args.subreddit)