<commit_before>import logging
import requests

__version__ = '1.0'

RELS = {
    'activity_streams': 'http://activitystrea.ms/spec/1.0',
    'avatar': 'http://webfinger.net/rel/avatar',
    'hcard': 'http://microformats.org/profile/hcard',
    'open_id': 'http://specs.openid.net/auth/2.0/provider',
    'opensocial': 'http://ns.opensocial.org/2008/opensocial/activitystreams',
    'portable_contacts': 'http://portablecontacts.net/spec/1.0',
    'profile': 'http://webfinger.net/rel/profile-page',
    'webfist': 'http://webfist.org/spec/rel',
    'xfn': 'http://gmpg.org/xfn/11',
}

WEBFINGER_TYPE = 'application/jrd+json'
LEGACY_WEBFINGER_TYPES = ['application/json']

UNOFFICIAL_ENDPOINTS = {
    'facebook.com': 'facebook-webfinger.appspot.com',
    'twitter.com': 'twitter-webfinger.appspot.com',
}

logger = logging.getLogger("webfinger")


class WebFingerException(Exception):
    pass


class WebFingerResponse(object):
    """ Response that wraps an RD object. It provides attribute-style access
        to links for specific rels, responding with the href attribute
        of the matched element.
    """

    def __init__(self, jrd):
        self.jrd = jrd

    def __getattr__(self, name):
        if name in RELS:
            return self.rel(RELS[name])
        return getattr(self.jrd, name)

    @property
    def subject(self):
        return self.jrd.get('subject')

    @property
    def aliases(self):
        return self.jrd.get('aliases', [])

    @property
    def properties(self):
        return self.jrd.get('properties', {})

    @property
    def links(self):
        return self.jrd.get('links', [])

    def rel(self, relation, attr='href'):
        for link in self.links:
            if link.get('rel') == relation:
                if attr:
                    return link.get(attr)
                return link


class WebFingerClient(object):

    def __init__(self, timeout=None, official=False):
        self.official = official
        self.timeout = timeout

    def _parse_host(self, resource):

        host = resource.split("@")[-1]

        if host in UNOFFICIAL_ENDPOINTS and not self.official:
            unofficial_host = UNOFFICIAL_ENDPOINTS[host]
            logging.debug('host %s is not supported, using unofficial endpoint %s' % (host, unofficial_host))
            host = unofficial_host

        return host

    def finger(self, resource, host=None, rel=None, raw=False):

        if not host:
            host = self._parse_host(resource)

        url = "https://%s/.well-known/webfinger" % host

        headers = {
            'User-Agent': 'python-webfinger/%s' % __version__,
            'Accept': WEBFINGER_TYPE,
        }
<commit_msg>Move requests import into finger method so that pypi installation works<commit_after>import logging

__version__ = '1.0'

RELS = {
    'activity_streams': 'http://activitystrea.ms/spec/1.0',
    'avatar': 'http://webfinger.net/rel/avatar',
    'hcard': 'http://microformats.org/profile/hcard',
    'open_id': 'http://specs.openid.net/auth/2.0/provider',
    'opensocial': 'http://ns.opensocial.org/2008/opensocial/activitystreams',
    'portable_contacts': 'http://portablecontacts.net/spec/1.0',
    'profile': 'http://webfinger.net/rel/profile-page',
    'webfist': 'http://webfist.org/spec/rel',
    'xfn': 'http://gmpg.org/xfn/11',
}

WEBFINGER_TYPE = 'application/jrd+json'
LEGACY_WEBFINGER_TYPES = ['application/json']

UNOFFICIAL_ENDPOINTS = {
    'facebook.com': 'facebook-webfinger.appspot.com',
    'twitter.com': 'twitter-webfinger.appspot.com',
}

logger = logging.getLogger("webfinger")


class WebFingerException(Exception):
    pass


class WebFingerResponse(object):
    """ Response that wraps an RD object. It provides attribute-style access
        to links for specific rels, responding with the href attribute
        of the matched element.
    """

    def __init__(self, jrd):
        self.jrd = jrd

    def __getattr__(self, name):
        if name in RELS:
            return self.rel(RELS[name])
        return getattr(self.jrd, name)

    @property
    def subject(self):
        return self.jrd.get('subject')

    @property
    def aliases(self):
        return self.jrd.get('aliases', [])

    @property
    def properties(self):
        return self.jrd.get('properties', {})

    @property
    def links(self):
        return self.jrd.get('links', [])

    def rel(self, relation, attr='href'):
        for link in self.links:
            if link.get('rel') == relation:
                if attr:
                    return link.get(attr)
                return link


class WebFingerClient(object):

    def __init__(self, timeout=None, official=False):
        self.official = official
        self.timeout = timeout

    def _parse_host(self, resource):

        host = resource.split("@")[-1]

        if host in UNOFFICIAL_ENDPOINTS and not self.official:
            unofficial_host = UNOFFICIAL_ENDPOINTS[host]
            logging.debug('host %s is not supported, using unofficial endpoint %s' % (host, unofficial_host))
            host = unofficial_host

        return host

    def finger(self, resource, host=None, rel=None, raw=False):

        import requests

        if not host:
            host = self._parse_host(resource)

        url = "https://%s/.well-known/webfinger" % host

        headers = {
            'User-Agent': 'python-webfinger/%s' % __version__,
            'Accept': WEBFINGER_TYPE,
        }

{'fraction_non_alphanumeric': 0.13340663368150998, 'fraction_numerical': 0.005497526113249038, 'mean_word_length': 2.6145695364238413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2792330', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1722, 'n_words': 366}