<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import itertools
import urlparse
import re
import requests
from bs4 import BeautifulSoup

class WebmentionSend():

    LINK_HEADER_RE = re.compile(
        r'''<([^>]+)>; rel=["'](http://)?webmention(\.org/?)?["']''')

    def __init__(self, source, target, endpoint=None):
        self.source_url = source
        self.target_url = target
        self.receiver_endpoint = endpoint

    def send(self, **kwargs):
        self.error = None
        self.requests_kwargs = kwargs
        if not self.receiver_endpoint:
            self._discoverEndpoint()
        if not self.receiver_endpoint:
            return False
        return self._notifyReceiver()

    def _discoverEndpoint(self):
        r = requests.get(self.target_url, verify=False, **self.requests_kwargs)
        if r.status_code != 200:
            self.error = {
                'code':'BAD_TARGET_URL',
                'error_description': 'Unable to get target URL.',
                'request': 'GET %s' % self.target_url,
                'http_status': r.status_code,
            }
            return

        # look in the headers
        # XXX: it looks like requests doesn't handle multiple headers with the
        # same name, e.g. 'Link'. from skimming the code, it looks like the last
        # one wins. ugh. :/
        for link in r.headers.get('link', '').split(','):
            match = self.LINK_HEADER_RE.search(link)
            if match:
                self.receiver_endpoint = match.group(1)
                return

        # look in the content
        html = r.text
        soup = BeautifulSoup(html)
        tag = None
        for name, rel in itertools.product(('link', 'a'), ('webmention', 'http://webmention.org/')):
            tag = soup.find(name, attrs={'rel': rel})
            if tag:
                break
<commit_msg>when discovering webmention endpoint, store the fetched HTML in WebmentionSend.html<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import itertools
import urlparse
import re
import requests
from bs4 import BeautifulSoup

class WebmentionSend():

    LINK_HEADER_RE = re.compile(
        r'''<([^>]+)>; rel=["'](http://)?webmention(\.org/?)?["']''')

    def __init__(self, source, target, endpoint=None):
        self.source_url = source
        self.target_url = target
        self.receiver_endpoint = endpoint

    def send(self, **kwargs):
        self.error = None
        self.requests_kwargs = kwargs
        if not self.receiver_endpoint:
            self._discoverEndpoint()
        if not self.receiver_endpoint:
            return False
        return self._notifyReceiver()

    def _discoverEndpoint(self):
        r = requests.get(self.target_url, verify=False, **self.requests_kwargs)
        if r.status_code != 200:
            self.error = {
                'code':'BAD_TARGET_URL',
                'error_description': 'Unable to get target URL.',
                'request': 'GET %s' % self.target_url,
                'http_status': r.status_code,
            }
            return
        self.html = r.text

        # look in the headers
        # XXX: it looks like requests doesn't handle multiple headers with the
        # same name, e.g. 'Link'. from skimming the code, it looks like the last
        # one wins. ugh. :/
        for link in r.headers.get('link', '').split(','):
            match = self.LINK_HEADER_RE.search(link)
            if match:
                self.receiver_endpoint = match.group(1)
                return

        # look in the content
        soup = BeautifulSoup(self.html)
        tag = None
        for name, rel in itertools.product(('link', 'a'), ('webmention', 'http://webmention.org/')):
            tag = soup.find(name, attrs={'rel': rel})
            if tag:
                break

{'fraction_non_alphanumeric': 0.12193850964043773, 'fraction_numerical': 0.003126628452318916, 'mean_word_length': 2.001563721657545, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '480438', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1090, 'n_words': 295}