<commit_before>import re
from datetime import datetime, timezone, timedelta
from collections import namedtuple
from xml.etree import ElementTree
import requests
from bs4 import BeautifulSoup
import parsedatetime
from pytz import UTC
from .._common import _make_float


__all__ = ['search_userid', 'TwoPlusTwoForumMember',
           'FORUM_URL', 'FORUM_MEMBER_URL', 'AJAX_USERSEARCH_URL']


FORUM_URL = 'http://forumserver.twoplustwo.com'
FORUM_MEMBER_URL = FORUM_URL + '/members'
AJAX_USERSEARCH_URL = FORUM_URL + '/ajax.php?do=usersearch'

_tz_re = re.compile('GMT (.*?)\.')


class AmbiguousUserNameError(Exception):
    """Exception when username is not unique, there are more starting with the same."""

class UserNotFoundError(Exception):
    """User cannot be found."""


def search_userid(username):
    headers = {'X-Requested-With': 'XMLHttpRequest',
               'Origin': FORUM_URL,
               'Referer': FORUM_URL + '/search.php'
               }

    data = {'securitytoken': 'guest',
            'do': 'usersearch',
            'fragment': username
            }

    response = requests.post(AJAX_USERSEARCH_URL, data, headers=headers)
    root = ElementTree.fromstring(response.text)

    try:
        found_name = root[0].text
    except IndexError:
        raise UserNotFoundError(username) from None

    # The request is basically a search, can return multiple userids
    # for users starting with username. Make sure we got the right one!
    if found_name.upper() != username.upper():
        exc = AmbiguousUserNameError(username)
        # attach the extra users to the exception
        ExtraUser = namedtuple('ExtraUser', 'name, id')
        exc.users = tuple(ExtraUser(name=child.text, id=child.attrib['userid']) for child in root)
        raise exc

    userid = root[0].attrib['userid']
    return userid<commit_msg>changed stdlib xml.etree.ElementTree to lxml.etree<commit_after>import re
from datetime import datetime, timezone, timedelta
from collections import namedtuple
from lxml import etree
import requests
from bs4 import BeautifulSoup
import parsedatetime
from pytz import UTC
from .._common import _make_float


__all__ = ['search_userid', 'TwoPlusTwoForumMember',
           'FORUM_URL', 'FORUM_MEMBER_URL', 'AJAX_USERSEARCH_URL']


FORUM_URL = 'http://forumserver.twoplustwo.com'
FORUM_MEMBER_URL = FORUM_URL + '/members'
AJAX_USERSEARCH_URL = FORUM_URL + '/ajax.php?do=usersearch'

_tz_re = re.compile('GMT (.*?)\.')


class AmbiguousUserNameError(Exception):
    """Exception when username is not unique, there are more starting with the same."""

class UserNotFoundError(Exception):
    """User cannot be found."""


def search_userid(username):
    headers = {'X-Requested-With': 'XMLHttpRequest',
               'Origin': FORUM_URL,
               'Referer': FORUM_URL + '/search.php'
               }

    data = {'securitytoken': 'guest',
            'do': 'usersearch',
            'fragment': username
            }

    response = requests.post(AJAX_USERSEARCH_URL, data, headers=headers)
    root = etree.fromstring(response.content)

    try:
        found_name = root[0].text
    except IndexError:
        raise UserNotFoundError(username) from None

    # The request is basically a search, can return multiple userids
    # for users starting with username. Make sure we got the right one!
    if found_name.upper() != username.upper():
        exc = AmbiguousUserNameError(username)
        # attach the extra users to the exception
        ExtraUser = namedtuple('ExtraUser', 'name, id')
        exc.users = tuple(ExtraUser(name=child.text, id=child.attrib['userid']) for child in root)
        raise exc

    userid = root[0].attrib['userid']
    return userid
{'fraction_non_alphanumeric': 0.10458567980691874, 'fraction_numerical': 0.0016090104585679806, 'mean_word_length': 3.3934040047114253, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '1524721', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1095, 'n_words': 309}