From 256cfb2aa4197587278079a5a901289bac2c1eaa Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Oct 2022 02:59:01 +0800 Subject: [PATCH] update: combine script --- src/wxsy.net/release.py | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/wxsy.net/release.py diff --git a/src/wxsy.net/release.py b/src/wxsy.net/release.py new file mode 100644 index 0000000..f8df941 --- /dev/null +++ b/src/wxsy.net/release.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +""" +Combine all chapters from json files. + + USAGE: python3 release.py [CATALOG] [JSON_DIR] +""" + +import sys +import json +from logger import logger + + +def loadData(catalog: dict) -> dict: # load data from json files + data = {} + for _, chapterId in catalog.items(): + data[chapterId] = json.loads( + open('%s/%s.json' % (sys.argv[2], chapterId)).read() # read json content + ) + return data + + +def listDiff(list_1: list, list_2: list) -> bool: # compare two lists + diffFlag = False + if len(list_1) != len(list_2): # with different length + diffFlag = True + logger.error('List with different length') + for i in range(0, len(list_1)): # check every items + if list_1[i] == list_2[i]: + continue + diffFlag = True # found different item + logger.error('List diff: `%s` <-> `%s`' % (list_1[i], list_2[i])) + return diffFlag + + +def check(catalog: dict, data: dict) -> None: # check crawler data + titles = [x['title'] for _, x in data.items()] + ids = [x['myId'] for _, x in data.items()] + preIds = [x['preId'] for _, x in data.items()] + nextIds = [x['nextId'] for _, x in data.items()] + nextIds.pop(-1) # remove last item + preIds.pop(0) # remove first item + + # if listDiff(ids, preIds + [ids[-1]]): + # logger.warning('Pre IDs mismatch') + # if listDiff(ids, [ids[0]] + nextIds): + # logger.warning('Next IDs mismatch') + if listDiff(ids, [x for _, x in catalog.items()]): + logger.warning('IDs mismatch') + if listDiff(titles, [x for x in catalog]): + logger.warning('Titles mismatch') + + +def combine() -> dict: # combine all chapters + catalog = json.loads(open(sys.argv[1]).read()) # load catalog + data = loadData(catalog) # load crawler data + check(catalog, data) # check data + + result = {} + for _, info in data.items(): # combine contents + result[info['title']] = info['contents'] + return result + + +print(json.dumps(combine()))