Skip to content

Instantly share code, notes, and snippets.

@arshcaria
Last active August 20, 2018 12:16
Show Gist options
  • Save arshcaria/c573743a8fd2f7c26b194a420d8b2032 to your computer and use it in GitHub Desktop.
Save arshcaria/c573743a8fd2f7c26b194a420d8b2032 to your computer and use it in GitHub Desktop.
Convert exported stylebot JSON to stylish/stylus-ready JSON
import argparse
import json
def stylebot_to_stylus(source='sb.json', target='stylus.json', encoding='utf-8'):
with open(source, encoding=encoding) as f:
data = json.load(f)
result_list = []
item_id = 1
for domain_name in data:
item_dict = {}
item_dict["id"] = item_id
item_id += 1
item_dict["enabled"] = data[domain_name]['_enabled']
item_dict["name"] = domain_name
# only one section for each domain (group)
sections = []
section0 = {}
section0['urls'] = []
section0['domains'] = []
# add the domain or the group of domains
if ',' in domain_name:
for addr in domain_name.split(sep=','):
section0['domains'].append(addr.strip())
else:
section0['domains'].append(domain_name)
css_rule = ''
# construct a css rule for each domain (group)
for selector in data[domain_name]['_rules']:
css_rule += selector + '{'
for attr in data[domain_name]['_rules'][selector]:
css_rule += attr + ':'
css_rule += data[domain_name]['_rules'][selector][attr] + ';'
css_rule += '}'
section0['code'] = css_rule
sections.append(section0)
item_dict['sections'] = sections
result_list.append(item_dict)
with open(target, "w", encoding=encoding) as of:
of.write(json.dumps(result_list))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("source",
help="input json file, exported from Stylebot[Lite]")
parser.add_argument('-o', '--output',
help='output json file, can be imported to Stylus')
parser.add_argument('-e', '--encoding',
help='output json file, can be imported to Stylus')
args = parser.parse_args()
src = args.source
out = args.output if args.output else 'stylus_output.json'
enc = args.encoding if args.encoding else 'utf-8'
stylebot_to_stylus(src, out, enc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment