Skip to content

Instantly share code, notes, and snippets.

@tsekityam
Created August 17, 2016 15:50
Show Gist options
  • Save tsekityam/ce64df36f84b2b87851b4b17315291b9 to your computer and use it in GitHub Desktop.
Save tsekityam/ce64df36f84b2b87851b4b17315291b9 to your computer and use it in GitHub Desktop.
Convert host.txt to JSON format used by iOS content block list
import sys
Hosts = sys.argv[1]
Rules = './blockerList.json'
in_file = open(Hosts, 'r')
out_file = open(Rules, 'w')
items = []
for line in in_file.readlines():
strippedLine = line.strip()
if len(strippedLine) == 0:
# do nothing on empty lines
continue
elif strippedLine.startswith('#'):
# do nothing on comments
continue
else:
host = strippedLine.split(' ')[1]
if len(host) > 0:
item = ' {\n' \
' "action": {\n' \
' "type": "block"\n' \
' },\n' \
' "trigger": {\n' \
' "url-filter": "' + host + '"\n' \
' }\n' \
' }'
items.append(item)
out_file.write('[\n' + ',\n'.join(items)+ '\n]\n')
in_file.close()
out_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment