Example #1
0
def test_utils_load_use_cache_server_down(path):
    denied_urls = [
        'https://www.w3schools.com/browsers/browsers_stats.asp',
        'http://useragentstring.com/pages/useragentstring.php',
        settings.CACHE_SERVER,
    ]

    with mock.patch(
        'fake_useragent.utils.Request',
        side_effect=partial(_request, denied_urls=denied_urls),
    ):
        with pytest.raises(errors.FakeUserAgentError):
            utils.load(use_cache_server=True)
Example #2
0
def test_utils_load_use_cache_server_down(path):
    denied_urls = [
        'https://www.w3schools.com/browsers/browsers_stats.asp',
        'http://useragentstring.com/pages/useragentstring.php',
        settings.CACHE_SERVER,
    ]

    with mock.patch(
            'fake_useragent.utils.Request',
            side_effect=partial(_request, denied_urls=denied_urls),
    ):
        with pytest.raises(errors.FakeUserAgentError):
            utils.load(use_cache_server=True)
Example #3
0
    def load(self):
        try:
            with self.load.lock:
                if self.cache:
                    self.data = load_cached(
                        self.path,
                        use_cache_server=self.use_cache_server,
                        verify_ssl=self.verify_ssl,
                    )
                else:
                    self.data = load(
                        use_cache_server=self.use_cache_server,
                        verify_ssl=self.verify_ssl,
                    )

                # TODO: change source file format
                # version 0.1.4+ migration tool
                self.data_randomize = list(self.data['randomize'].values())
                self.data_browsers = self.data['browsers']
        except FakeUserAgentError:
            if self.fallback is None:
                raise
            else:
                logger.warning(
                    'Error occurred during fetching data, '
                    'but was suppressed with fallback.', )
Example #4
0
    def load(self):
        try:
            with self.load.lock:
                if self.cache:
                    self.data = load_cached(
                        self.path,
                        use_cache_server=self.use_cache_server,
                        verify_ssl=self.verify_ssl,
                    )
                else:
                    self.data = load(
                        use_cache_server=self.use_cache_server,
                        verify_ssl=self.verify_ssl,
                    )

                # TODO: change source file format
                # version 0.1.4+ migration tool
                self.data_randomize = list(self.data['randomize'].values())
                self.data_browsers = self.data['browsers']
        except FakeUserAgentError:
            if self.fallback is None:
                raise
            else:
                logger.warning(
                    'Error occurred during fetching data, '
                    'but was suppressed with fallback.',
                )
Example #5
0
    def __init__(self, cache=True):
        super(UserAgent, self).__init__()

        if cache:
            self.data = load_cached()
        else:
            self.data = load()

        self.cache = cache
Example #6
0
def test_utils_load_no_use_cache_server(path):
    denied_urls = [
        'https://www.w3schools.com/browsers/browsers_stats.asp',
        'http://useragentstring.com/pages/useragentstring.php',
    ]

    with mock.patch(
            'fake_useragent.utils.Request',
            side_effect=partial(_request, denied_urls=denied_urls),
    ):
        with pytest.raises(errors.FakeUserAgentError):
            utils.load(use_cache_server=False)

        with pytest.raises(errors.FakeUserAgentError):
            utils.load_cached(path, use_cache_server=False)

        with pytest.raises(errors.FakeUserAgentError):
            utils.update(path, use_cache_server=False)
Example #7
0
def test_utils_load_no_use_cache_server(path):
    denied_urls = [
        'https://www.w3schools.com/browsers/browsers_stats.asp',
        'http://useragentstring.com/pages/useragentstring.php',
    ]

    with mock.patch(
        'fake_useragent.utils.Request',
        side_effect=partial(_request, denied_urls=denied_urls),
    ):
        with pytest.raises(errors.FakeUserAgentError):
            utils.load(use_cache_server=False)

        with pytest.raises(errors.FakeUserAgentError):
            utils.load_cached(path, use_cache_server=False)

        with pytest.raises(errors.FakeUserAgentError):
            utils.update(path, use_cache_server=False)
Example #8
0
    def load(self):
        try:
            with self.load.lock:
                if self.cache:
                    self.data = load_cached(self.path)
                else:
                    self.data = load()

                # TODO: change source file format
                # version 0.1.4- migration tool
                self.data_randomize = list(self.data['randomize'].values())
                self.data_browsers = self.data['browsers']
        except FakeUserAgentError:
            if self.fallback is None:
                logger.error('Error occurred during fetching data...', )

                raise
            else:
                logger.warning(
                    'Error occurred during fetching data but was suppressed with fallback.',  # noqa
                )
Example #9
0
def test_utils_load(path):
    _load = utils.load

    with mock.patch(
        'fake_useragent.utils.load',
        side_effect=_load,
    ) as mocked:
        data = utils.load(use_cache_server=False)

        mocked.assert_called()

    expected = {
        'randomize': mock.ANY,
        'browsers': {
            'chrome': mock.ANY,
            'firefox': mock.ANY,
            'opera': mock.ANY,
            'safari': mock.ANY,
            'internetexplorer': mock.ANY,
        },
    }

    assert expected == data
Example #10
0
def test_utils_load(path):
    _load = utils.load

    with mock.patch(
            'fake_useragent.utils.load',
            side_effect=_load,
    ) as mocked:
        data = utils.load(use_cache_server=False)

        mocked.assert_called()

    expected = {
        'randomize': mock.ANY,
        'browsers': {
            'chrome': mock.ANY,
            'firefox': mock.ANY,
            'opera': mock.ANY,
            'safari': mock.ANY,
            'internetexplorer': mock.ANY,
        },
    }

    assert expected == data
Example #11
0
    def load(self):
        try:
            with self.load.lock:
                if self.cache:
                    self.data = load_cached(
                        self.path,
                        use_cache_server=self.use_cache_server,
                        verify_ssl=self.verify_ssl,
                    )
                else:
                    self.data = load(
                        use_cache_server=self.use_cache_server,
                        verify_ssl=self.verify_ssl,
                    )

                # TODO: change source file format
                # version 0.1.4+ migration tool
                self.data_randomize = list(self.data['randomize'].values())
                self.data_browsers = self.data['browsers']

                for family in self.data_browsers:
                    for browser in self.data_browsers[family]:
                        if re.search('(Mobile|Android)', browser):
                            if family not in self.data_browsers_mobile:
                                self.data_browsers_mobile.update({family: []})
                            self.data_browsers_mobile[family].append(browser)
                        else:
                            if family not in self.data_browsers_desktop:
                                self.data_browsers_desktop.update({family: []})
                            self.data_browsers_desktop[family].append(browser)
        except FakeUserAgentError:
            if self.fallback is None:
                raise
            else:
                logger.warning(
                    'Error occurred during fetching data, '
                    'but was suppressed with fallback.', )
Example #12
0
def test_utils_load_use_cache_server(path):
    denied_urls = [
        'https://www.w3schools.com/browsers/browsers_stats.asp',
        'http://useragentstring.com/pages/useragentstring.php',
    ]

    with mock.patch(
        'fake_useragent.utils.Request',
        side_effect=partial(_request, denied_urls=denied_urls),
    ):
        data = utils.load(use_cache_server=True)

        expected = {
            'randomize': mock.ANY,
            'browsers': {
                'chrome': mock.ANY,
                'firefox': mock.ANY,
                'opera': mock.ANY,
                'safari': mock.ANY,
                'internetexplorer': mock.ANY,
            },
        }

        assert expected == data
Example #13
0
def test_utils_load_use_cache_server(path):
    denied_urls = [
        'https://www.w3schools.com/browsers/browsers_stats.asp',
        'http://useragentstring.com/pages/useragentstring.php',
    ]

    with mock.patch(
            'fake_useragent.utils.Request',
            side_effect=partial(_request, denied_urls=denied_urls),
    ):
        data = utils.load(use_cache_server=True)

        expected = {
            'randomize': mock.ANY,
            'browsers': {
                'chrome': mock.ANY,
                'firefox': mock.ANY,
                'opera': mock.ANY,
                'safari': mock.ANY,
                'internetexplorer': mock.ANY,
            },
        }

        assert expected == data
Example #14
0
 def load(self):
     if self.cache:
         self.data = load_cached()
     else:
         self.data = load()
Example #15
0
def test_load():
    fake_useragent_dict = utils.load()

    check_dict(fake_useragent_dict)

    global fake_useragent_dict
Example #16
0
def test_load():
    fake_useragent_dict = utils.load()

    check_dict(fake_useragent_dict)

    global fake_useragent_dict
Example #17
0
 def load(self):
     if self.cache:
         self.data = load_cached()
     else:
         self.data = load()
Example #18
0
# with open('./user-agent.json','r') as f:
#     with open('./user_agent_list.txt','w') as f2:
#         true_user_agent_json = json.loads(f.read())
#         print(type(true_user_agent_json))
#         print(true_user_agent_json.keys())
#         # dict_keys(['browsers', 'randomize'])
#         # dict_keys(['chrome', 'opera', 'firefox', 'internetexplorer', 'safari'])
#         print(true_user_agent_json['browsers'].keys())
#         for x in true_user_agent_json['browsers']:
#             for browser in true_user_agent_json['browsers'][x]:
#                 f2.write('\'')
#                 f2.write(browser)
#                 f2.write('\'')
#                 f2.write(',')
#                 f2.write('\n')
#
#
#         # 总共有200;
from fake_useragent import utils

# ua = UserAgent()
# print(ua)
# print(ua.ie)
# print(help(ua))

ret = utils.load()
print(utils.get_browsers())
print(len(utils.get_browser_versions('Chrome')))

print(ret)