<commit_before>""" Class to interact with StopForumSpam's API

The API is documented at http://www.stopforumspam.com/usage

This client implementation supports only email address and IP addresses
reputation lookup. This class does not support submitting an entry to the
StopForumSpam service.

EXAMPLE USAGE:::

from stopforumspam_client import StopforumspamClient

client = StopforumspamClient()
response_ip = client.get_data('ip', '8.8.8.8')
print json.dumps(response_ip, sort_keys=False, indent=4)

response_email = client.get_data('mail', '<EMAIL>')
print json.dumps(response_email, sort_keys=False, indent=4)
"""

import requests


class StopforumspamClient:

    _type_conversion = {'ip': 'ip', 'mail': 'email'}

    def __init__(self):
        self.client = requests.Session()

    def _set_payload(type, data):
        return {
            'json': True,
            'confidence': True,
            StopforumspamClient._type_conversion[type]: data
        }

    def data_conversion(self, data):
        if 'appears' in data:
            data['appears'] = (data['appears'] == 1)
        if 'torexit' in data:
            data['torexit'] = (data['torexit'] == 1)
        return data

    def get_data(self, datatype, data):
        """ Look for an IP address or an email address in the spammer database.

        :param datatype: Which type of data is to be looked up.
                         Allowed values are 'ip' or 'mail'.
        :param data: The value to be looked up through the API.
        :type datatype: str
        :type data: str
        :return: Data relative to the looked up artifact.
        :rtype: list
        """
        result = []
        params = StopforumspamClient._set_payload(datatype, data)
        response = self.client.get(
            'https://api.stopforumspam.org/api', params=params)
        response.raise_for_status()
        report = response.json()
        if report['success']:
            data = report[StopforumspamClient._type_conversion[datatype]]
            result.append(self.data_conversion(data))
        else:
            pass
        return result
<commit_msg>Set StopforumspamClient.data_conversion method as "protected"<commit_after>""" Class to interact with StopForumSpam's API

The API is documented at http://www.stopforumspam.com/usage

This client implementation supports only email address and IP addresses
reputation lookup. This class does not support submitting an entry to the
StopForumSpam service.

EXAMPLE USAGE:::

from stopforumspam_client import StopforumspamClient

client = StopforumspamClient()
response_ip = client.get_data('ip', '8.8.8.8')
print json.dumps(response_ip, sort_keys=False, indent=4)

response_email = client.get_data('mail', '<EMAIL>')
print json.dumps(response_email, sort_keys=False, indent=4)
"""

import requests


class StopforumspamClient:

    _type_conversion = {'ip': 'ip', 'mail': 'email'}

    def __init__(self):
        self.client = requests.Session()

    def _set_payload(type, data):
        return {
            'json': True,
            'confidence': True,
            StopforumspamClient._type_conversion[type]: data
        }

    def _data_conversion(self, data):
        if 'appears' in data:
            data['appears'] = (data['appears'] == 1)
        if 'torexit' in data:
            data['torexit'] = (data['torexit'] == 1)
        return data

    def get_data(self, datatype, data):
        """ Look for an IP address or an email address in the spammer database.

        :param datatype: Which type of data is to be looked up.
                         Allowed values are 'ip' or 'mail'.
        :param data: The value to be looked up through the API.
        :type datatype: str
        :type data: str
        :return: Data relative to the looked up artifact.
        :rtype: list
        """
        result = []
        params = StopforumspamClient._set_payload(datatype, data)
        response = self.client.get(
            'https://api.stopforumspam.org/api', params=params)
        response.raise_for_status()
        report = response.json()
        if report['success']:
            data = report[StopforumspamClient._type_conversion[datatype]]
            result.append(self._data_conversion(data))
        else:
            pass
        return result

{'fraction_non_alphanumeric': 0.10233100233100233, 'fraction_numerical': 0.0037296037296037296, 'mean_word_length': 2.6738013698630136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 2, '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': '6194207', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1221, 'n_words': 368}