<commit_before># -*- coding: utf-8 -*-

import json
from pyeupi import PyEUPI

misperrors = {'error': 'Error'}
mispattributes = {'input': ['hostname', 'domain', 'url'], 'output': ['freetext']}
moduleinfo = {'version': '0.1', 'author': '<NAME>',
              'description': 'Query the Phishing Initiative service (https://phishing-initiative.lu)',
              'module-type': ['expansion', 'hover']}

moduleconfig = ['apikey', 'url']


def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    if request.get('hostname'):
        toquery = request['hostname']
    elif request.get('domain'):
        toquery = request['domain']
    elif request.get('url'):
        toquery = request['url']
    else:
        misperrors['error'] = "Unsupported attributes type"
        return misperrors

    if not request.get('config') and not (request['config'].get('apikey') and request['config'].et('url')):
        misperrors['error'] = 'EUPI authentication is missing'
        return misperrors

    pyeupi = PyEUPI(request['config']['apikey'], request['config']['url'])

    if 'event_id' in request:
        return handle_expansion(pyeupi, toquery)
    else:
        return handle_hover(pyeupi, toquery)


def handle_expansion(pyeupi, url):
    results = pyeupi.search_url(url=url)

    if results.get('results'):
        to_return = ''
        for r in results['results']:
            if r['tag_label'] != 'phishing':
                continue
            to_return += ' {} {} {} '.format(r['url'], r['domain'], r['ip_address'])
        if to_return:
            return {'results': [{'types': mispattributes['output'], 'values': to_return}]}
        else:
            misperrors['error'] = 'Unknown in the EUPI service'
            return misperrors
    else:
        misperrors['error'] = 'Error in EUPI lookup'
        return misperrors


def handle_hover(pyeupi, url):
    try:
        result = pyeupi.lookup(url=url)['results'][0]
    except (KeyError, IndexError):
        misperrors['error'] = 'Error in EUPI lookup'
        return misperrors

    return {'results': [{'types': mispattributes['output'],
                         'values': result['tag_label'].title()}]}


def introspection():
    return mispattributes


def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo
<commit_msg>[EUPI] Fix expansion for empty EUPI response<commit_after># -*- coding: utf-8 -*-

import json
from pyeupi import PyEUPI

misperrors = {'error': 'Error'}
mispattributes = {'input': ['hostname', 'domain', 'url'], 'output': ['freetext']}
moduleinfo = {'version': '0.1', 'author': '<NAME>',
              'description': 'Query the Phishing Initiative service (https://phishing-initiative.lu)',
              'module-type': ['expansion', 'hover']}

moduleconfig = ['apikey', 'url']


def handler(q=False):
    if q is False:
        return False
    request = json.loads(q)
    if request.get('hostname'):
        toquery = request['hostname']
    elif request.get('domain'):
        toquery = request['domain']
    elif request.get('url'):
        toquery = request['url']
    else:
        misperrors['error'] = "Unsupported attributes type"
        return misperrors

    if not request.get('config') and not (request['config'].get('apikey') and request['config'].et('url')):
        misperrors['error'] = 'EUPI authentication is missing'
        return misperrors

    pyeupi = PyEUPI(request['config']['apikey'], request['config']['url'])

    if 'event_id' in request:
        return handle_expansion(pyeupi, toquery)
    else:
        return handle_hover(pyeupi, toquery)


def handle_expansion(pyeupi, url):
    results = pyeupi.search_url(url=url)

    if results.get('results'):
        to_return = ''
        for r in results['results']:
            if r['tag_label'] != 'phishing':
                continue
            to_return += ' {} {} {} '.format(r['url'], r['domain'], r['ip_address'])
        if to_return:
            return {'results': [{'types': mispattributes['output'], 'values': to_return}]}
        else:
            misperrors['error'] = 'Unknown in the EUPI service'
            return misperrors
    else:
        return {'results': [{'types': mispattributes['output'], 'values': ''}]}


def handle_hover(pyeupi, url):
    try:
        result = pyeupi.lookup(url=url)['results'][0]
    except (KeyError, IndexError):
        misperrors['error'] = 'Error in EUPI lookup'
        return misperrors

    return {'results': [{'types': mispattributes['output'],
                         'values': result['tag_label'].title()}]}


def introspection():
    return mispattributes


def version():
    moduleinfo['config'] = moduleconfig
    return moduleinfo

{'fraction_non_alphanumeric': 0.15953389830508474, 'fraction_numerical': 0.001694915254237288, 'mean_word_length': 2.9638958858102433, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3594200', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1407, 'n_words': 348}