|
| 1 | +import json |
| 2 | +import binascii |
| 3 | +import np |
| 4 | +import ezodf |
| 5 | +import pandas_ods_reader |
| 6 | +import io |
| 7 | + |
| 8 | +misperrors = {'error': 'Error'} |
| 9 | +mispattributes = {'input': ['attachment'], |
| 10 | + 'output': ['freetext', 'text']} |
| 11 | +moduleinfo = {'version': '0.1', 'author': 'Sascha Rommelfangen', |
| 12 | + 'description': '.ods to freetext-import IOC extractor', |
| 13 | + 'module-type': ['expansion']} |
| 14 | + |
| 15 | +moduleconfig = [] |
| 16 | + |
| 17 | + |
| 18 | +def handler(q=False): |
| 19 | + if q is False: |
| 20 | + return False |
| 21 | + q = json.loads(q) |
| 22 | + filename = q['attachment'] |
| 23 | + try: |
| 24 | + ods_array = np.frombuffer(binascii.a2b_base64(q['data']), np.uint8) |
| 25 | + except Exception as e: |
| 26 | + print(e) |
| 27 | + err = "Couldn't fetch attachment (JSON 'data' is empty). Are you using the 'Query enrichment' action?" |
| 28 | + misperrors['error'] = err |
| 29 | + print(err) |
| 30 | + return misperrors |
| 31 | + |
| 32 | + ods_content = "" |
| 33 | + ods_file = io.BytesIO(ods_array) |
| 34 | + doc = ezodf.opendoc(ods_file) |
| 35 | + num_sheets = len(doc.sheets) |
| 36 | + try: |
| 37 | + for i in range(0, num_sheets): |
| 38 | + ods = pandas_ods_reader.read_ods(ods_file, i, headers=False) |
| 39 | + ods_content = ods_content + "\n" + ods.to_string(max_rows=None) |
| 40 | + print(ods_content) |
| 41 | + return {'results': [{'types': ['freetext'], 'values': ods_content, 'comment': ".ods-to-text from file " + filename}, |
| 42 | + {'types': ['text'], 'values': ods_content, 'comment': ".ods-to-text from file " + filename}]} |
| 43 | + except Exception as e: |
| 44 | + print(e) |
| 45 | + err = "Couldn't analyze file as .ods. Error was: " + str(e) |
| 46 | + misperrors['error'] = err |
| 47 | + return misperrors |
| 48 | + |
| 49 | + |
| 50 | +def introspection(): |
| 51 | + return mispattributes |
| 52 | + |
| 53 | + |
| 54 | +def version(): |
| 55 | + moduleinfo['config'] = moduleconfig |
| 56 | + return moduleinfo |
0 commit comments