Last active
May 8, 2018 16:35
-
-
Save gschizas/2e15be2e4c2a31cb013542e7805f9a0b to your computer and use it in GitHub Desktop.
Download spritesheet from subreddit and separate images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import io | |
import PIL.Image | |
import requests | |
import tinycss | |
from praw_wrapper import praw_wrapper | |
SUBREDDIT_NAME = input("Enter your subreddit name here: ") | |
SPRITESHEET_NAME = input("What's the image name for the spritesheet called (e.g. 'spritesheet'): ") | |
r = praw_wrapper() | |
sr = r.subreddit(SUBREDDIT_NAME) | |
ss = sr.stylesheet() | |
spritesheet_url = [img for img in ss.images if img['name'] == SPRITESHEET_NAME][0]['url'] | |
spritesheet_page = requests.get(spritesheet_url) | |
img = PIL.Image.open(io.BytesIO(spritesheet_page.content)) | |
parser = tinycss.make_parser('page3') | |
stylesheet = parser.parse_stylesheet(ss.stylesheet) | |
os.makedirs('flair-images/' + sr.display_name, exist_ok=True) | |
for rule in stylesheet.rules: | |
if rule.selector[0].type != 'DELIM': continue | |
if rule.selector[1].type != 'IDENT': continue | |
if not rule.selector[1].value.startswith('flair-'): continue | |
flag_name = rule.selector[1].value[6:] | |
image_height = None | |
image_width = None | |
for d in rule.declarations: | |
position = [decl for decl in rule.declarations if decl.name == 'background-position'][0].value | |
width = [decl for decl in rule.declarations if decl.name in ('width', 'min-width')][0].value[0].value | |
height = [decl for decl in rule.declarations if decl.name in ('height', 'min-height')][0].value[0].value | |
image_x, image_y = position[0].value, position[2].value | |
image2 = img.crop((-image_x, -image_y, -image_x + width, -image_y + height)) | |
with open(os.path.join('flair-images', sr.display_name, flag_name + '.png'), 'wb') as img_file: | |
image2.save(img_file, format='PNG') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
import uuid | |
from getpass import getpass | |
from urllib.parse import urlparse, parse_qs | |
import praw | |
from ruamel import yaml | |
import json | |
import keyring | |
# Go to https://www.reddit.com/prefs/apps to fill these out. | |
# Use https://example.com/authorize_callback as your callback URL. | |
# It won't matter, you're pasting the URL at the end. | |
CLIENT_ID = 'xxxxxxxxxxxxxx' | |
CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx' | |
def praw_wrapper(): | |
refresh_token = keyring.get_password('reddit-scratch', 'refresh_token') | |
user_agent='python:put.your.own.agent.here:v'+datetime.date.today().isoformat()+' (by /u/~~your~~name~~here)' | |
if refresh_token: | |
r = praw.Reddit( | |
client_id=CLIENT_ID, | |
client_secret=CLIENT_SECRET, | |
refresh_token=refresh_token, | |
user_agent=user_agent) | |
else: | |
r = praw.Reddit( | |
client_id=CLIENT_ID, | |
client_secret=CLIENT_SECRET, | |
redirect_uri='https://example.com/authorize_callback', | |
user_agent=user_agent) | |
state = uuid.uuid4().hex | |
print('Visit the following URL:', r.auth.url(['*'], state)) | |
url = input('Result URL: ') | |
query = parse_qs(urlparse(url).query) | |
assert state == query['state'][0] | |
code = query['code'][0] | |
refresh_token = r.auth.authorize(code) | |
keyring.set_password('reddit-scratch', 'refresh_token', refresh_token) | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment