2017-11-14 03:13:19 +00:00
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from . import common
|
|
|
|
from . import tsdb
|
|
|
|
|
|
|
|
|
2020-01-28 02:53:19 +00:00
|
|
|
def get_styles(subreddit):
|
2017-12-13 22:42:56 +00:00
|
|
|
(database, subreddit) = tsdb.TSDB.for_subreddit(subreddit, fix_name=True)
|
|
|
|
|
2017-11-14 03:13:19 +00:00
|
|
|
print('Getting styles for /r/%s' % subreddit)
|
|
|
|
subreddit = common.r.subreddit(subreddit)
|
|
|
|
styles = subreddit.stylesheet()
|
|
|
|
|
|
|
|
os.makedirs(database.styles_dir.absolute_path, exist_ok=True)
|
|
|
|
|
|
|
|
stylesheet_filepath = database.styles_dir.with_child('stylesheet.css')
|
|
|
|
print('Downloading %s' % stylesheet_filepath.relative_path)
|
|
|
|
with open(stylesheet_filepath.absolute_path, '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 open(image_filepath.absolute_path, 'wb') as image_file:
|
|
|
|
response = requests.get(image['url'])
|
|
|
|
image_file.write(response.content)
|
|
|
|
|
2020-01-28 02:53:19 +00:00
|
|
|
def get_styles_argparse(args):
|
|
|
|
return get_styles(args.subreddit)
|