<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
# moehime / saimoe / net resource fetching
#
# Copyright (C) 2012 <NAME> <<EMAIL>>
#
# This file is part of moehime.
#
# moehime is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# moehime is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with moehime.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals, division, print_function

import abc

import requests
from pyquery import PyQuery as pq

UA = 'Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1'


# TODO: make this non-blocking 让这个模块不阻塞
def do_fetch(url, session=None):
    import sys
    print('[GET] %s' % (url, ), file=sys.stderr)
    response = (session if session is not None else requests).get(url)
    print('    -> %d B' % (len(response.content), ), file=sys.stderr)
    return response.content


class ResourceRequester(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self):
        super(ResourceRequester, self).__init__()
        session = self._requests_session = requests.Session()
        session.headers.update({
                'User-Agent': UA,
                })

    @abc.abstractmethod
    def format_url(self, kind, *args, **kwargs):
        pass
<commit_msg>gzip whenever possible 如有可能则接受 Gzip 压缩<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
# moehime / saimoe / net resource fetching
#
# Copyright (C) 2012 <NAME> <<EMAIL>>
#
# This file is part of moehime.
#
# moehime is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# moehime is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with moehime.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import unicode_literals, division, print_function

import abc

import requests
from pyquery import PyQuery as pq

UA = 'Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1'


# TODO: make this non-blocking 让这个模块不阻塞
def do_fetch(url, session=None):
    import sys
    print('[GET] %s' % (url, ), file=sys.stderr)
    response = (session if session is not None else requests).get(url)
    print('    -> %d B' % (len(response.content), ), file=sys.stderr)
    return response.content


class ResourceRequester(object):
    __metaclass__ = abc.ABCMeta

    def __init__(self):
        super(ResourceRequester, self).__init__()
        session = self._requests_session = requests.Session()
        session.headers.update({
                'User-Agent': UA,
                'Accept-Encoding': 'gzip,deflate',
                })

    @abc.abstractmethod
    def format_url(self, kind, *args, **kwargs):
        pass

{'fraction_non_alphanumeric': 0.10113636363636364, 'fraction_numerical': 0.016477272727272726, 'mean_word_length': 3.3957553058676653, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5600943', 'n_tokens_mistral': 1119, 'n_tokens_neox': 1093, 'n_words': 393}