def __getattr__(self, attr): if attr in self.safe_attrs: return super(UserAgent, self).__getattr__(attr) try: for value, replacement in settings.REPLACEMENTS.items(): attr = attr.replace(value, replacement) attr = attr.lower() if attr == 'random': browser = random.choice(self.data_randomize) else: browser = settings.SHORTCUTS.get(attr, attr) return random.choice(self.data_browsers[browser]) except (KeyError, IndexError): if self.fallback is None: raise FakeUserAgentError('Error occurred during getting browser') # noqa else: logger.warning( 'Error occurred during getting browser, ' 'but was suppressed with fallback.', ) return self.fallback
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.', )
def __getattr__(self, attr): if attr in self.safe_attrs: return super(UserAgent, self).__getattr__(attr) try: for value, replacement in settings.REPLACEMENTS.items(): attr = attr.replace(value, replacement) attr = attr.lower() if attr == 'random': browser = random.choice(self.data_randomize) else: browser = settings.SHORTCUTS.get(attr, attr) return random.choice(self.data_browsers[browser]) except (KeyError, IndexError): if self.fallback is None: raise FakeUserAgentError( 'Error occurred during getting browser') # noqa else: logger.warning( 'Error occurred during getting browser, ' 'but was suppressed with fallback.', ) return self.fallback
def load(use_cache_server=True): browsers_dict = {} randomize_dict = {} try: for item in get_browsers(): browser, percent = item browser_key = browser for value, replacement in settings.REPLACEMENTS.items(): browser_key = browser_key.replace(value, replacement) browser_key = browser_key.lower() browsers_dict[browser_key] = get_browser_versions(browser) # it is actually so bad way for randomizing, simple list with # browser_key's is event better # I've failed so much a lot of years ago. # Ideas for refactoring # {'chrome': <percantage|int>, 'firefox': '<percatage|int>'} for _ in range(int(float(percent) * 10)): randomize_dict[str(len(randomize_dict))] = browser_key except Exception as exc: if not use_cache_server: raise exc logger.warning( 'Error occurred during loading data. ' 'Trying to use cache server %s', settings.CACHE_SERVER, exc_info=exc, ) try: ret = json.loads(get(settings.CACHE_SERVER).decode('utf-8')) except (TypeError, ValueError): raise FakeUserAgentError('Can not load data from cache server') else: ret = { 'browsers': browsers_dict, 'randomize': randomize_dict, } if not isinstance(ret, dict): raise FakeUserAgentError('Data is not dictionary ', ret) for param in ['browsers', 'randomize']: if param not in ret: raise FakeUserAgentError('Missing data param: ', param) if not isinstance(ret[param], dict): raise FakeUserAgentError('Data param is not dictionary', ret[param]) # noqa if not ret[param]: raise FakeUserAgentError('Data param is empty', ret[param]) return ret
def load(): browsers_dict = {} randomize_dict = {} try: for item in get_browsers(): browser, percent = item browser_key = browser for value, replacement in settings.REPLACEMENTS.items(): browser_key = browser_key.replace(value, replacement) browser_key = browser_key.lower() browsers_dict[browser_key] = get_browser_versions(browser) # it is actually so bad way for randomizing, simple list with # browser_key's is event better # I've failed so much a lot of years ago. # Ideas for refactoring # {'chrome': <percantage|int>, 'firefox': '<percatage|int>'} for _ in range(int(float(percent) * 10)): randomize_dict[str(len(randomize_dict))] = browser_key except Exception as exc: logger.warning( 'Error occurred during formatting data. ' 'Trying to use fallback server %s', settings.CACHE_SERVER, exc_info=exc, ) try: ret = json.loads(get(settings.CACHE_SERVER).decode('utf-8')) except (UnicodeDecodeError, TypeError, ValueError): raise FakeUserAgentError('Can not load data from cached server') else: ret = { 'browsers': browsers_dict, 'randomize': randomize_dict, } if not isinstance(ret, dict): raise FakeUserAgentError('Data is not dictionary ', ret) for param in ['browsers', 'randomize']: if param not in ret: raise FakeUserAgentError('Missing data param: ', param) if not isinstance(ret[param], dict): raise FakeUserAgentError('Data param is not dictionary', ret[param]) # noqa if not ret[param]: raise FakeUserAgentError('Data param is empty', ret[param]) return ret
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.', )