#!/usr/bin/env python # # csv2urltester.py - Simple script to convert CSV file to urltester (JSON) formatted file # # Copyright 2018 Todd Shadburn # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # import sys import json if len(sys.argv) < 3: print 'Usage: %s csvfile outputfile' % (sys.argv[0]) sys.exit(2) ifd = open(sys.argv[1], 'r') ofd = open(sys.argv[2], 'w') ostruct = { 'type': 'urltester', 'version': '2018-02-19', 'urls': [] } ulist = [] for ln in ifd: f = ln.strip('\r\n').split(',') nu = { 'url': f[0], 'tests': { 'positive': [], 'negative': [] } } if len(f) > 1: for pt in f[1:]: if pt != '': nu['tests']['positive'].append(pt) ulist.append(nu) ostruct['urls'] = ulist ofd.write(json.dumps(ostruct)) ofd.close() ifd.close() sys.exit(0)