├── anti_useragent ├── exceptions.py ├── utils │ ├── __init__.py │ ├── misc.py │ ├── scrapy_contextfactory.py │ ├── log.py │ └── cipers.py ├── __init__.py ├── useragent │ ├── browser │ │ ├── __init__.py │ │ ├── uc.py │ │ ├── firefox.py │ │ ├── opera.py │ │ ├── baidu.py │ │ ├── wechat.py │ │ └── chrome.py │ ├── __init__.py │ └── ua.py └── settings │ ├── default_settings.py │ └── __init__.py ├── test.py ├── .gitignore ├── setup.py ├── doc └── README_ZH.md ├── README.md └── LICENSE /anti_useragent/exceptions.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, unicode_literals 2 | 3 | 4 | class AntiUserAgentError(Exception): 5 | pass 6 | 7 | 8 | UserAgentError = AntiUserAgentError 9 | -------------------------------------------------------------------------------- /anti_useragent/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.utils.log import LogFormatter 2 | 3 | logging = LogFormatter() 4 | 5 | from anti_useragent.utils.misc import Utils 6 | 7 | misc = Utils() 8 | 9 | __all__ = [logging, misc] 10 | -------------------------------------------------------------------------------- /anti_useragent/__init__.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, unicode_literals 2 | 3 | from anti_useragent.useragent.ua import AntiUserAgent, UserAgent 4 | 5 | __version__ = '1.0.10' 6 | 7 | VERSION = __version__ 8 | 9 | __all__ = [ 10 | AntiUserAgent, 11 | UserAgent, 12 | VERSION, 13 | ] 14 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/__init__.py: -------------------------------------------------------------------------------- 1 | from .baidu import BaiduAndroidUA, BaiduIphoneUA 2 | from .chrome import ChromeAndroidUA, ChromeIphoneUA, ChromeUA 3 | from .firefox import FirefoxUA 4 | from .opera import OperaUA 5 | from .uc import UcUA 6 | from .wechat import WechatIphoneUA, WechatAndroidUA 7 | 8 | 9 | __all__ = [ 10 | BaiduAndroidUA, BaiduIphoneUA, 11 | ChromeAndroidUA, ChromeIphoneUA, ChromeUA, 12 | FirefoxUA, 13 | OperaUA, 14 | UcUA, 15 | WechatIphoneUA, WechatAndroidUA 16 | ] 17 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/uc.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.useragent import BaseUserAgent 2 | 3 | 4 | class UcUA(BaseUserAgent): 5 | 6 | def __init__(self, *args, **kwargs): 7 | self.min_version = getattr(self, 'min_version', 55) 8 | self.max_version = getattr(self, 'max_version', 90) 9 | super().__init__(*args, **kwargs) 10 | 11 | @property 12 | def ua(self): 13 | self.set_platform(platform='android') 14 | _ua = self.settings.get('BASE_USER_AGENT_UC') % { 15 | 'android_version': self.utils.android_version(), 16 | 'android_phone': self.utils.system_version(self.s_info, self.s_bit)[0], 17 | 'safari': self.utils.safari() 18 | } 19 | if self.logger: 20 | self.logger.debug(_ua) 21 | return _ua 22 | 23 | def __repr__(self): 24 | return "" % ("android", self.max_version) 25 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | from anti_useragent import UserAgent 2 | import anti_useragent 3 | # from fake_useragent import FakeUserAgent 4 | 5 | 6 | if __name__ == '__main__': 7 | # print(UserAgent(platform='mac').random) 8 | # print(UserAgent()['firefox']) 9 | 10 | # print(UserAgent(platform='windows', min_version=90, max_version=101).chrome) 11 | print(UserAgent().chrome) 12 | print(UserAgent(platform='windows').chrome) 13 | print(UserAgent(platform='windows', versions=(100, 101)).chrome) 14 | # print(UserAgent(platform='windows', versions=(90, 100)).chrome) 15 | # # print(FakeUserAgent().chrome) 16 | 17 | # import anti_useragent 18 | 19 | # print(anti_useragent.VERSION) 20 | 21 | # print(UserAgent()['chrome']) 22 | # print(UserAgent()['firefox']) 23 | # print(UserAgent()['opera']) 24 | # print(UserAgent()['chrome_android']) 25 | # print(UserAgent()['chrome_iphone']) 26 | # print(UserAgent()['wechat_android']) 27 | # print(UserAgent()['wechat_iphone']) 28 | # print(UserAgent()['baidu_android']) 29 | # print(UserAgent()['uc']) 30 | # print(UserAgent()['baidu_iphone']) 31 | 32 | # print(UserAgent().wechat) 33 | # print(sslgen()) 34 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/firefox.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.useragent import BaseUserAgent 2 | 3 | 4 | class FirefoxUA(BaseUserAgent): 5 | """ 6 | Mozilla/5.0 是一个通用标记符号,用来表示与 Mozilla 兼容,这几乎是现代浏览器的标配。 7 | platform 用来说明浏览器所运行的原生系统平台(例如 Windows、Mac、Linux 或 Android),以及是否运行在手机上。搭载 Firefox OS 的手机仅简单地使用了 "Mobile" 这个字符串;因为 web 本身就是平台。注意 platform 可能会包含多个使用 "; " 隔开的标记符号。参见下文获取更多的细节信息及示例。 8 | rv:geckoversion 表示 Gecko 的发布版本号(例如 "17.0")。在近期发布的版本中,geckoversion 表示的值与 firefoxversion 相同。 9 | Gecko/geckotrail 表示该浏览器基于 Gecko 渲染引擎。 10 | 在桌面浏览器中, geckotrail 是固定的字符串 "20100101" 。 11 | Firefox/firefoxversion 表示该浏览器是 Firefox,并且提供了版本号信息(例如 "17.0")。 12 | 13 | eg: Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion 14 | example: 15 | Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0 16 | Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.0 17 | platform: 18 | windows 19 | mac 20 | linux 21 | """ 22 | 23 | def __init__(self, *args, **kwargs): 24 | self.min_version = getattr(self, 'min_version', 10) 25 | self.max_version = getattr(self, 'max_version', 50) 26 | super().__init__(*args, **kwargs) 27 | 28 | @property 29 | def ua(self): 30 | self.set_platform(self.platform) 31 | _system = self.utils.system_version(self.s_info, self.s_bit) 32 | _firefox = self.utils.firefox(self.min_version, self.max_version) 33 | _ua = self.settings.get('BASE_USER_AGENT_FIREFOX') % { 34 | 'system_info': _system[0], 35 | 'system_bit': _system[1], 36 | 'r_version': _firefox[0], 37 | 'version': _firefox[1] 38 | } 39 | if self.logger: 40 | self.logger.debug(_ua) 41 | return _ua 42 | 43 | def __repr__(self): 44 | return "" % (self.platform, self.max_version) 45 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/opera.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.useragent import BaseUserAgent 2 | 3 | 4 | class OperaUA(BaseUserAgent): 5 | """ 6 | Mozilla/5.0 是一个通用标记符号,用来表示与 Mozilla 兼容,这几乎是现代浏览器的标配。 7 | platform 用来说明浏览器所运行的原生系统平台(例如 Windows、Mac、Linux 或 Android),以及是否运行在手机上。搭载 Firefox OS 的手机仅简单地使用了 "Mobile" 这个字符串;因为 web 本身就是平台。注意 platform 可能会包含多个使用 "; " 隔开的标记符号。参见下文获取更多的细节信息及示例。 8 | Chrome Opera 也是一款基于 blink 引擎的浏览器,这也是为什么它的 UA 看起来(和 Chrome 的)几乎一样的原因,不过,它添加了一个 "OPR/"。 9 | 10 | example: 11 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36 OPR/38.0.2220.41 12 | platform: 13 | windows 14 | mac 15 | linux 16 | """ 17 | 18 | def __init__(self, *args, **kwargs): 19 | self.min_version = getattr(self, 'min_version', 55) 20 | self.max_version = getattr(self, 'max_version', 90) 21 | super().__init__(*args, **kwargs) 22 | 23 | 24 | @property 25 | def ua(self): 26 | self.set_platform(self.platform) 27 | _system = self.utils.system_version(self.s_info, self.s_bit) 28 | _version = self.utils.chrome(self.min_version, self.max_version).split('.') 29 | _o_version = self.utils.opera(self.max_version).split('.') 30 | _ua = self.settings.get('BASE_USER_AGENT_OPERA') % { 31 | 'system_info': _system[0], 32 | 'system_bit': _system[1], 33 | 'big_version': _version[0], 34 | 'mid_version': _version[1], 35 | 'small_version': _version[2], 36 | 'beta_version': _version[3], 37 | 'o_version': _o_version[0], 38 | 'os_version': _o_version[1], 39 | 'ob_version': _o_version[2], 40 | } 41 | if self.logger: 42 | self.logger.debug(_ua) 43 | return _ua 44 | 45 | def __repr__(self): 46 | return "" % (self.platform, self.max_version) 47 | 48 | 49 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/baidu.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.useragent import BaseUserAgent 2 | 3 | 4 | class BaiduAndroidUA(BaseUserAgent): 5 | 6 | def __init__(self, *args, **kwargs): 7 | self.min_version = getattr(self, 'min_version', 55) 8 | self.max_version = getattr(self, 'max_version', 90) 9 | super().__init__(*args, **kwargs) 10 | 11 | 12 | @property 13 | def ua(self): 14 | self.set_platform(platform='android') 15 | _ua = self.settings.get('BASE_USER_AGENT_BAIDU_BOX_ANDROID') % { 16 | 'android_version': self.utils.android_version(), 17 | 'android_phone': self.utils.system_version(self.s_info, self.s_bit)[0], 18 | 'chrome': self.utils.chrome(), 19 | 'baidu_box_app_android': self.utils.baidu_box_app(p='android'), 20 | 'safari': self.utils.safari() 21 | } 22 | if self.logger: 23 | self.logger.debug(_ua) 24 | return _ua 25 | 26 | def __repr__(self): 27 | return "" % ('android', self.max_version) 28 | 29 | 30 | class BaiduIphoneUA(BaseUserAgent): 31 | 32 | def __init__(self, *args, **kwargs): 33 | self.min_version = getattr(self, 'min_version', 55) 34 | self.max_version = getattr(self, 'max_version', 90) 35 | super().__init__(*args, **kwargs) 36 | 37 | 38 | @property 39 | def ua(self): 40 | self.set_platform(platform='iphone') 41 | _ua = self.settings.get('BASE_USER_AGENT_BAIDU_BOX_IPHONE').format(**{ 42 | 'mac_version': self.utils.mac_version(), 43 | 'safari': self.utils.safari(), 44 | 'mobile': self.utils.key(6), 45 | 'baidu_box_app_iphone': self.utils.baidu_box_app(p='iphone'), 46 | 'key': self.utils.key(51), 47 | }) 48 | if self.logger: 49 | self.logger.debug(_ua) 50 | return _ua 51 | 52 | def __repr__(self): 53 | return "" % ('iphone', self.max_version) 54 | -------------------------------------------------------------------------------- /anti_useragent/useragent/__init__.py: -------------------------------------------------------------------------------- 1 | from typing import Set, Optional 2 | from anti_useragent.settings import Settings 3 | from anti_useragent.utils import logging 4 | from anti_useragent.utils import misc 5 | 6 | 7 | class BaseUserAgent: 8 | _INSTANCE = None 9 | 10 | def __new__(cls, *args, **kwargs): 11 | if not cls._INSTANCE: 12 | cls._INSTANCE = super().__new__(cls) 13 | return cls._INSTANCE 14 | 15 | def __init__(self, 16 | platform: Optional[str] = None, 17 | logger: object = False, 18 | min_version: Optional[int] = None, 19 | max_version: Optional[int] = None, 20 | *args, **kwargs 21 | ): 22 | self.settings = self._settings 23 | 24 | if min_version: 25 | self.min_version = min_version 26 | assert isinstance(self.min_version, int), 'Min Version must be int' 27 | if max_version: 28 | self.max_version = max_version 29 | assert isinstance(self.max_version, int), 'Max Version must be int' 30 | 31 | if all([min_version, max_version]) and min_version > max_version: 32 | raise Exception("Max Version must lg than Min Version") 33 | assert isinstance(platform or "", str), 'Platform must be string or NoneType' 34 | 35 | if all([platform, (platform not in self.settings.get('PLATFORM'))]): 36 | raise TypeError('Unknown platform type: %s' % platform) 37 | self.logger = logger 38 | self.platform = platform or 'windows' 39 | self.s_info = None 40 | self.s_bit = None 41 | 42 | if logger: 43 | self.logger = logging.get_logger('anti_useragent') 44 | 45 | self.utils = misc 46 | 47 | 48 | def set_platform(self, platform: Optional[str]): 49 | self.platform = platform 50 | self.s_info = self.settings.get('PLATFORM_OVERRIDES')[platform][0] 51 | self.s_bit = self.settings.get('PLATFORM_OVERRIDES')[platform][1] 52 | 53 | @property 54 | def _settings(self) -> Settings: 55 | return Settings() 56 | -------------------------------------------------------------------------------- /anti_useragent/utils/misc.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import string 3 | import subprocess 4 | 5 | from random import randint, choice 6 | 7 | 8 | class Utils(object): 9 | 10 | def install(self, package): 11 | subprocess.call([sys.executable, "-m", "pip", "install", package]) 12 | 13 | def android_version(self) -> str: 14 | return '{0}.{1}.{2}'.format(randint(4, 11), randint(0, 9), randint(0, 9)) 15 | 16 | def system_version(self, info, bit) -> tuple: 17 | return choice(info), choice(bit) 18 | 19 | def chrome(self, miv=50, mav=90) -> str: 20 | return '{0}.{1}.{2}.{3}'.format(randint(miv, mav), randint(0, 9), randint(3000, 9999), randint(10, 99)) 21 | 22 | def safari(self) -> str: 23 | return '{0}.{1}'.format(randint(100, 999), randint(0, 99)) 24 | 25 | def mac_version(self) -> str: 26 | return '{}_{}_{}'.format(randint(6, 12), randint(1, 9), randint(1, 9)) 27 | 28 | def windows_version(self) -> str: 29 | return '{}.{}'.format(randint(6, 10), randint(0, 9)) 30 | 31 | def key(self, length=6) -> str: 32 | return ''.join(choice(string.ascii_uppercase + string.digits) for _ in range(length)) 33 | 34 | def firefox(self, miv=10, mav=50) -> tuple: 35 | return '{}.0'.format(randint(miv, mav)), '{}.0'.format(randint(20, 60)) 36 | 37 | def opera(self, mav=90) -> str: 38 | return '{}.{}.{}'.format(randint(30, mav), randint(1000, 9999), randint(10, 99)) 39 | 40 | def micro_message(self) -> str: 41 | return '6.{0}.{1}.{2}'.format(randint(0, 9), randint(0, 9), randint(1, 9999)) 42 | 43 | def tbs(self) -> str: 44 | return str(randint(1, 999999)).zfill(6) 45 | 46 | def baidu_box_app(self, p='android'): 47 | if p == 'android': 48 | return '{}.{}'.format(randint(1, 8), randint(0, 9)) 49 | elif p == 'iphone': 50 | return '0_{}.{}.{}.{}_enohpi_{}_{}'.format( 51 | randint(1, 20), 52 | randint(0, 9), 53 | randint(0, 9), 54 | randint(0, 9), 55 | str(randint(999, 9999)).zfill(4), 56 | str(randint(1, 999)).zfill(3) 57 | ) 58 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/wechat.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.useragent import BaseUserAgent 2 | 3 | 4 | class WechatAndroidUA(BaseUserAgent): 5 | 6 | def __init__(self, *args, **kwargs): 7 | self.min_version = getattr(self, 'min_version', 55) 8 | self.max_version = getattr(self, 'max_version', 90) 9 | super().__init__(*args, **kwargs) 10 | 11 | 12 | @property 13 | def ua(self): 14 | self.set_platform(platform='android') 15 | _ua = self.settings.get('BASE_USER_AGENT_CHROME_ANDROID') % { 16 | 'android_version': self.utils.android_version(), 17 | 'android_phone': self.utils.system_version(self.s_info, self.s_bit)[0], 18 | 'chrome': self.utils.chrome(), 19 | 'TBS': self.utils.tbs(), 20 | 'safari': self.utils.safari(), 21 | 'micro_messenger': self.utils.micro_message(), 22 | 'net_type': self.utils.system_version(self.s_info, self.s_bit)[1] 23 | } 24 | if self.logger: 25 | self.logger.debug(_ua) 26 | return _ua 27 | 28 | def __repr__(self): 29 | return "" % (self.platform, self.max_version) 30 | 31 | 32 | class WechatIphoneUA(BaseUserAgent): 33 | 34 | def __init__(self, *args, **kwargs): 35 | self.min_version = getattr(self, 'min_version', 55) 36 | self.max_version = getattr(self, 'max_version', 90) 37 | super().__init__(*args, **kwargs) 38 | 39 | 40 | @property 41 | def ua(self): 42 | self.set_platform(platform='iphone') 43 | _ua = self.settings.get('BASE_USER_AGENT_WECHAT_IPHONE') % { 44 | 'mac_version': self.utils.mac_version(), 45 | 'safari': self.utils.safari(), 46 | 'mobile': self.utils.key(6), 47 | 'micro_messenger': self.utils.micro_message(), 48 | 'net_type': self.utils.system_version(self.s_info, self.s_bit)[1] 49 | } 50 | if self.logger: 51 | self.logger.debug(_ua) 52 | return _ua 53 | 54 | def __repr__(self): 55 | return "" % (self.platform, self.max_version) 56 | -------------------------------------------------------------------------------- /anti_useragent/utils/scrapy_contextfactory.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | from twisted.internet.ssl import CertificateOptions, AcceptableCiphers 4 | from scrapy.core.downloader.contextfactory import ScrapyClientContextFactory 5 | 6 | ORIGIN_CIPHERS = 'ECDHE-ECDSA-AES256-CCM:ECDHE-ECDSA-AES128-CCM8:ECDHE-ECDSA-AES256-CCM8:DHE-RSA-AES128-CCM:DHE-RSA-AES256-CCM:AES128-CCM8:AES256-CCM8:DHE-RSA-AES128-CCM8:DHE-RSA-AES256-CCM8:ADH-AES128-SHA256:ADH-AES256-SHA256:ADH-AES128-GCM-SHA256:ADH-AES256-GCM-SHA384:AES128-CCM:AES256-CCM:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-DSS-AES128-SHA256:DHE-DSS-AES256-SHA256:DHE-DSS-AES128-GCM-SHA256:DHE-DSS-AES256-GCM-SHA384:DHE-RSA-AES128-SHA256:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:RSA+AES128:ALL:!ADH:@STRENGTH:HIGH:DEFAULT:!DH:!aNULL:!eNULL:!LOW:!ADH:!RC4:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS' 7 | 8 | 9 | 10 | class CipherFactory: 11 | def __init__(self, cipher: str = None): 12 | if cipher is None: 13 | cipher = ORIGIN_CIPHERS 14 | self.cipher = cipher 15 | 16 | @classmethod 17 | def setter_cipher(cls, val: str = None): 18 | return cls(val) 19 | 20 | def __call__(self) -> str: 21 | ciphers_list = self.cipher.split(':') 22 | random.shuffle(ciphers_list) 23 | ciphers_real = ':'.join(ciphers_list) 24 | return ciphers_real 25 | 26 | 27 | generate_cipher = CipherFactory() 28 | 29 | 30 | class Ja3ScrapyClientContextFactory(ScrapyClientContextFactory): 31 | 32 | def getCertificateOptions(self): 33 | tls_ciphers = generate_cipher() 34 | self.tls_ciphers = AcceptableCiphers.fromOpenSSLCipherString( 35 | tls_ciphers) 36 | return CertificateOptions( 37 | verify=False, 38 | method=getattr(self, 'method', getattr(self, '_ssl_method', None)), 39 | fixBrokenPeers=True, 40 | acceptableCiphers=self.tls_ciphers 41 | ) 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # IDE 2 | .vscode/ 3 | .idea/ 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | *$py.class 9 | 10 | # C extensions 11 | *.so 12 | 13 | # Distribution / packaging 14 | .Python 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | .eggs/ 21 | lib/ 22 | lib64/ 23 | parts/ 24 | sdist/ 25 | var/ 26 | wheels/ 27 | pip-wheel-metadata/ 28 | share/python-wheels/ 29 | *.egg-info/ 30 | .installed.cfg 31 | *.egg 32 | MANIFEST 33 | 34 | # PyInstaller 35 | # Usually these files are written by a python script from a template 36 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 37 | *.manifest 38 | *.spec 39 | 40 | # Installer logs 41 | pip-log.txt 42 | pip-delete-this-directory.txt 43 | 44 | # Unit test / coverage reports 45 | htmlcov/ 46 | .tox/ 47 | .nox/ 48 | .coverage 49 | .coverage.* 50 | .cache 51 | nosetests.xml 52 | coverage.xml 53 | *.cover 54 | *.py,cover 55 | .hypothesis/ 56 | .pytest_cache/ 57 | 58 | # Translations 59 | *.mo 60 | *.pot 61 | 62 | # Django stuff: 63 | *.log 64 | local_settings.py 65 | db.sqlite3 66 | db.sqlite3-journal 67 | 68 | # Flask stuff: 69 | instance/ 70 | .webassets-cache 71 | 72 | # Scrapy stuff: 73 | .scrapy 74 | 75 | # Sphinx documentation 76 | docs/_build/ 77 | 78 | # PyBuilder 79 | target/ 80 | 81 | # Jupyter Notebook 82 | .ipynb_checkpoints 83 | 84 | # IPython 85 | profile_default/ 86 | ipython_config.py 87 | 88 | # pyenv 89 | .python-version 90 | 91 | # pipenv 92 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 93 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 94 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 95 | # install all needed dependencies. 96 | #Pipfile.lock 97 | 98 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 99 | __pypackages__/ 100 | 101 | # Celery stuff 102 | celerybeat-schedule 103 | celerybeat.pid 104 | 105 | # SageMath parsed files 106 | *.sage.py 107 | 108 | # Environments 109 | .env 110 | .venv 111 | env/ 112 | venv/ 113 | ENV/ 114 | env.bak/ 115 | venv.bak/ 116 | 117 | # Spyder project settings 118 | .spyderproject 119 | .spyproject 120 | 121 | # Rope project settings 122 | .ropeproject 123 | 124 | # mkdocs documentation 125 | /site 126 | 127 | # mypy 128 | .mypy_cache/ 129 | .dmypy.json 130 | dmypy.json 131 | 132 | # Pyre type checker 133 | .pyre/ 134 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | from __future__ import unicode_literals 3 | 4 | import os 5 | import sys 6 | 7 | from shutil import rmtree 8 | 9 | from setuptools import setup, Command 10 | 11 | 12 | NAME = "anti_useragent" 13 | DESCRIPTION = "fake pc or app browser useragent, anti useragent, and other awesome tools" 14 | URL = "https://github.com/ihandmine/anti-useragent" 15 | AUTHOR = "handmine" 16 | AUTHOR_EMAIL = "handmine@outlook.com" 17 | VERSION = "1.0.10" 18 | LICENSE = "MIT" 19 | REQUIRES_PYTHON = ">=3.7.0" 20 | 21 | 22 | def list_dir(dir): 23 | result = [dir] 24 | for file in os.listdir(dir): 25 | if os.path.isdir(os.path.join(dir, file)): 26 | result.extend(list_dir(os.path.join(dir, file))) 27 | return result 28 | 29 | 30 | here = os.path.abspath(os.path.dirname(__file__)) 31 | with open(f"{here}/README.md", encoding='utf-8') as f: 32 | long_description = f.read() 33 | 34 | 35 | class UploadCommand(Command): 36 | """Support setup.py upload.""" 37 | 38 | description = "Build and publish the package." 39 | user_options = [] 40 | 41 | @staticmethod 42 | def status(s): 43 | """Prints things in bold.""" 44 | print("\033[1m{0}\033[0m".format(s)) 45 | 46 | def initialize_options(self): 47 | pass 48 | 49 | def finalize_options(self): 50 | pass 51 | 52 | def run(self): 53 | try: 54 | self.status("Removing previous builds...") 55 | rmtree(os.path.join(here, "dist")) 56 | except OSError: 57 | pass 58 | 59 | self.status("Building Source and Wheel distribution...") 60 | os.system("{0} setup.py sdist bdist_wheel".format(sys.executable)) 61 | 62 | self.status("Uploading the package to PyPI via Twine...") 63 | os.system("twine upload dist/*") 64 | 65 | sys.exit() 66 | 67 | 68 | 69 | setup( 70 | name=NAME, 71 | version=VERSION, 72 | description=DESCRIPTION, 73 | long_description=long_description, 74 | long_description_content_type='text/markdown', 75 | classifiers=[ 76 | 'License :: OSI Approved :: MIT License', 77 | 'Programming Language :: Python', 78 | 'Intended Audience :: Developers', 79 | 'Operating System :: OS Independent', 80 | ], 81 | url=URL, 82 | author=AUTHOR, 83 | author_email=AUTHOR_EMAIL, 84 | license=LICENSE, 85 | packages=list_dir(NAME), 86 | package_dir={NAME: NAME}, 87 | python_requires=REQUIRES_PYTHON, 88 | include_package_data=True, 89 | zip_safe=False, 90 | install_requires=[ 91 | 'loguru', 92 | ], 93 | keywords=[ 94 | 'user', 'agent', 'user agent', 'useragent', 95 | 'fake', 'fake useragent', 'fake user agent', 96 | 'anti', 'anti useragent' 97 | ], 98 | cmdclass={"upload": UploadCommand}, 99 | ) 100 | -------------------------------------------------------------------------------- /anti_useragent/useragent/browser/chrome.py: -------------------------------------------------------------------------------- 1 | from anti_useragent.useragent import BaseUserAgent 2 | 3 | 4 | class ChromeUA(BaseUserAgent): 5 | """ 6 | Mozilla/5.0 是一个通用标记符号,用来表示与 Mozilla 兼容,这几乎是现代浏览器的标配。 7 | platform 用来说明浏览器所运行的原生系统平台(例如 Windows、Mac、Linux 或 Android),以及是否运行在手机上。搭载 Firefox OS 的手机仅简单地使用了 "Mobile" 这个字符串;因为 web 本身就是平台。注意 platform 可能会包含多个使用 "; " 隔开的标记符号。参见下文获取更多的细节信息及示例。 8 | Chrome (或 Chromium/blink-based engines)用户代理字符串与 Firefox 的格式类似。为了兼容性,它添加了诸如 "KHTML, like Gecko" 和 "Safari" 这样的字符串。 9 | 10 | example: 11 | Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36 12 | platform: 13 | windows 14 | mac 15 | linux 16 | """ 17 | 18 | def __init__(self, *args, **kwargs): 19 | self.min_version = getattr(self, 'min_version', 55) 20 | self.max_version = getattr(self, 'max_version', 100) 21 | super().__init__(*args, **kwargs) 22 | 23 | 24 | @property 25 | def ua(self): 26 | self.set_platform(self.platform) 27 | _system = self.utils.system_version(self.s_info, self.s_bit) 28 | _version = self.utils.chrome(self.min_version, self.max_version).split('.') 29 | _ua = self.settings.get('BASE_USER_AGENT_CHROME') % { 30 | 'system_info': _system[0], 31 | 'system_bit': _system[1], 32 | 'big_version': _version[0], 33 | 'mid_version': _version[1], 34 | 'small_version': _version[2], 35 | 'beta_version': _version[3], 36 | } 37 | if self.logger: 38 | self.logger.debug(_ua) 39 | return _ua 40 | 41 | def __repr__(self): 42 | return "" % (self.platform, self.max_version) 43 | 44 | 45 | class ChromeAndroidUA(BaseUserAgent): 46 | 47 | def __init__(self, *args, **kwargs): 48 | self.min_version = getattr(self, 'min_version', 55) 49 | self.max_version = getattr(self, 'max_version', 100) 50 | super().__init__(*args, **kwargs) 51 | 52 | 53 | @property 54 | def ua(self): 55 | self.set_platform(platform='android') 56 | _ua = self.settings.get('BASE_USER_AGENT_CHROME_ANDROID') % { 57 | 'android_version': self.utils.android_version(), 58 | 'android_phone': self.utils.system_version(self.s_info, self.s_bit)[0], 59 | 'chrome': self.utils.chrome(self.min_version, self.max_version), 60 | 'safari': self.utils.safari() 61 | } 62 | if self.logger: 63 | self.logger.debug(_ua) 64 | return _ua 65 | 66 | def __repr__(self): 67 | return "" % ("android", self.max_version) 68 | 69 | 70 | class ChromeIphoneUA(BaseUserAgent): 71 | 72 | def __init__(self, *args, **kwargs): 73 | self.min_version = getattr(self, 'min_version', 55) 74 | self.max_version = getattr(self, 'max_version', 100) 75 | super().__init__(*args, **kwargs) 76 | 77 | 78 | @property 79 | def ua(self): 80 | self.set_platform(platform='iphone') 81 | _ua = self.settings.get('BASE_USER_AGENT_CHROME_IPHONE') % { 82 | 'mac_version': self.utils.mac_version(), 83 | 'mobile': self.utils.key(), 84 | 'safari': self.utils.safari() 85 | } 86 | if self.logger: 87 | self.logger.debug(_ua) 88 | return _ua 89 | 90 | def __repr__(self): 91 | return "" % ("iphone", self.max_version) 92 | -------------------------------------------------------------------------------- /anti_useragent/useragent/ua.py: -------------------------------------------------------------------------------- 1 | from random import choice 2 | from typing import Set, Optional 3 | 4 | from anti_useragent.useragent import browser 5 | from anti_useragent.settings import Settings 6 | from anti_useragent.exceptions import UserAgentError, AntiUserAgentError 7 | 8 | 9 | class UserAgent(object): 10 | _shortcut = { 11 | 'chrome': browser.ChromeUA, 12 | 'firefox': browser.FirefoxUA, 13 | 'opera': browser.OperaUA, 14 | 'chrome_android': browser.ChromeAndroidUA, 15 | 'chrome_iphone': browser.ChromeIphoneUA, 16 | 'wechat_android': browser.WechatAndroidUA, 17 | 'wechat_iphone': browser.WechatIphoneUA, 18 | 'baidu_android': browser.BaiduAndroidUA, 19 | 'baidu_iphone': browser.BaiduIphoneUA, 20 | 'uc': browser.UcUA 21 | } 22 | 23 | def __init__(self, 24 | platform: Optional[str] = None, 25 | versions: Set[int]=None, 26 | logger: object=False, 27 | min_version: Optional[int] = None, 28 | max_version: Optional[int] = None 29 | ): 30 | self.logger = logger 31 | self.platform = platform 32 | if versions is not None: 33 | self.min_version = versions[0] 34 | self.max_version = versions[1] 35 | else: 36 | self.min_version = min_version 37 | self.max_version = max_version 38 | self.settings = self.from_settings 39 | self._platform_ua_map = self.settings.get('PLATFORM_UA_MAP') 40 | 41 | @property 42 | def from_settings(self) -> Settings: 43 | return Settings() 44 | 45 | def __getitem__(self, rule): 46 | return self.__getattr__(rule) 47 | 48 | def __getattr__(self, rule: str) -> Optional[str]: 49 | try: 50 | if rule != 'random': 51 | _item_rule = [item for item in list(self._shortcut.keys()) if rule in item] 52 | _item_rule = [item for item in self._platform_ua_map[self.platform] 53 | if rule in item] if self.platform else _item_rule 54 | return getattr( 55 | self._shortcut[ 56 | ''.join(choice(_item_rule) if _item_rule else []) or rule 57 | ]( 58 | platform=self.platform, 59 | min_version=self.min_version, 60 | max_version=self.max_version, 61 | logger=self.logger 62 | ), 63 | 'ua') 64 | if not self.platform: 65 | _attr = choice(list(self._shortcut.keys())) 66 | _ua = self._shortcut[_attr]( 67 | platform=self.platform, 68 | min_version=self.min_version, 69 | max_version=self.max_version, 70 | logger=self.logger 71 | ) 72 | _ua.set_platform(choice(_ua.settings.get('PLATFORM'))) 73 | return getattr(_ua, 'ua') 74 | else: 75 | _attr = choice(self._platform_ua_map[self.platform]) 76 | _ua=self._shortcut[_attr]( 77 | platform=self.platform, 78 | min_version=self.min_version, 79 | max_version=self.max_version, 80 | logger=self.logger 81 | ) 82 | return getattr(_ua, 'ua') 83 | except UserAgentError: 84 | raise AntiUserAgentError('Error occurred during getting useragent.') 85 | except KeyError: 86 | raise AntiUserAgentError('The platform unsupported browser.') 87 | 88 | AntiUserAgent = UserAgent 89 | -------------------------------------------------------------------------------- /anti_useragent/utils/log.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, unicode_literals 2 | 3 | import sys 4 | import socket 5 | 6 | from anti_useragent.utils import misc 7 | 8 | 9 | try: 10 | from loguru import logger 11 | except: 12 | misc.install("loguru") 13 | from loguru import logger 14 | 15 | 16 | def set_log_config(formatter, logfile=None): 17 | return { 18 | "default": { 19 | "handlers": [ 20 | { 21 | "sink": sys.stdout, 22 | "format": formatter, 23 | "level": "TRACE" 24 | }, 25 | { 26 | "sink": f"{__file__}.log" if not logfile else logfile, 27 | "format": formatter, 28 | "level": "INFO", 29 | "rotation": '1 week', 30 | "retention": '30 days', 31 | 'encoding': 'utf-8' 32 | }, 33 | ], 34 | "extra": { 35 | "host": socket.gethostbyname(socket.gethostname()), 36 | 'log_name': 'default', 37 | 'type': 'None' 38 | }, 39 | "levels": [ 40 | dict(name="TRACE", icon="✏️", color=""), 41 | dict(name="DEBUG", icon="❄️", color=""), 42 | dict(name="INFO", icon="♻️", color=""), 43 | dict(name="SUCCESS", icon="✔️", color=""), 44 | dict(name="WARNING", icon="⚠️", color=""), 45 | dict(name="ERROR", icon="❌️", color=""), 46 | dict(name="CRITICAL", icon="☠️", color=""), 47 | ] 48 | }, 49 | 'kafka': True 50 | } 51 | 52 | 53 | class LogFormatter(object): 54 | default_formatter = '{time:YYYY-MM-DD HH:mm:ss,SSS} | ' \ 55 | '[{extra[log_name]}] {module}:{name}:{function}:{line} | ' \ 56 | '{extra[host]} | ' \ 57 | '{level.icon}{level: <5} | ' \ 58 | '{level.no} | ' \ 59 | '{extra[type]} | ' \ 60 | '{message} ' 61 | 62 | kafka_formatter = '{time:YYYY-MM-DD HH:mm:ss,SSS}| ' \ 63 | '[{extra[log_name]}] {module}:{name}:{function}:{line} | ' \ 64 | '{extra[host]} | ' \ 65 | '{process} | ' \ 66 | '{thread} | ' \ 67 | '{level: <5} | ' \ 68 | '{level.no} | ' \ 69 | '{extra[type]}| ' \ 70 | '{message} ' 71 | 72 | def __init__(self): 73 | self.logger = logger 74 | 75 | def setter_log_handler(self, callback=None): 76 | assert callable(callback), 'callback must be a callable object' 77 | self.logger.add(callback, format=self.kafka_formatter) 78 | 79 | def get_logger(self, name=None): 80 | log_config = set_log_config(self.default_formatter) 81 | config = log_config.pop('default', {}) 82 | if name: 83 | config['extra']['log_name'] = name 84 | self.logger.configure(**config) 85 | return self.logger 86 | 87 | @staticmethod 88 | def format(spider, meta): 89 | if hasattr(spider, 'logging_keys'): 90 | logging_txt = [] 91 | for key in spider.logging_keys: 92 | if meta.get(key, None) is not None: 93 | logging_txt.append(u'{0}:{1} '.format(key, meta[key])) 94 | logging_txt.append('successfully') 95 | return ' '.join(logging_txt) 96 | 97 | -------------------------------------------------------------------------------- /doc/README_ZH.md: -------------------------------------------------------------------------------- 1 | # anti-useragent 2 | 3 | 4 | 5 | > 信息: 伪装PC端APP端浏览器的用户浏览器UA头,反用户代理, UA指纹,和其他实用的工具方法 6 | 7 | ## 特性 8 | 9 | - 更多的浏览器支持 10 | - 更多的随机规则 11 | - 更多有趣的工具集 12 | 13 | [English](../README.md) | 中文 14 | 15 | ### 安装 16 | 17 | ```shell 18 | pip install anti-useragent 19 | ``` 20 | 21 | ### 用法 22 | 23 | ```python 24 | from anti_useragent import UserAgent 25 | ua = UserAgent() 26 | 27 | ua.opera 28 | # Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11 29 | ua.chrome 30 | # Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36 31 | ua['chrome'] 32 | # Mozilla/5.0 (Windows NT 5.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.2.3576.5 Safari/537.36 33 | ua.firefox 34 | # Mozilla/5.0 (Windows NT 5.1; WOW64; rv:47.0) Gecko/20100101 Firefox/45.0 35 | ua['firefox'] 36 | # Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:49.0) Gecko/20100101 Firefox/31.0 37 | ua.android 38 | # Mozilla/5.0 (Linux; Android 7.5.2; M571C Build/LMY47D) AppleWebKit/666.7 (KHTML, like Gecko) Chrome/72.7.7953.78 Mobile Safari/666.7 39 | ua.iphone 40 | # Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/349.56 (KHTML, like Gecko) Mobile/J9UMJN baiduboxapp/0_17.7.6.6_enohpi_8957_628/2.01_4C2%258enohPi/1099a/P0SJ2RX4DXJT3RW906040KVOSH2E76RJUNHVIJUPCJQCZMEM2GL/1 41 | ua.wechat 42 | # Mozilla/5.0 (Linux; Android 10.9.8; MI 5 Build/NRD90M) AppleWebKit/536.93 (KHTML, like Gecko) Chrome/81.7.8549.56 Mobile Safari/536.93 43 | 44 | # and the best one, random via real world browser usage statistic 45 | ua.random 46 | # Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.3.8610.5 Safari/537.36 47 | ``` 48 | 49 | ### 支持平台 50 | 51 | | browser/platfom | windows | mac | linux | iphone | android | 52 | | :-------------: | :-----: | :--: | :---: | :----: | :-----: | 53 | | **chrome** | ✔ | ✔ | ✔ | ✔ | ✔ | 54 | | **firefox** | ✔ | ✔ | ✔ | ❌ | ❌ | 55 | | **opera** | ✔ | ✔ | ✔ | ❌ | ❌ | 56 | | **wechat** | ❌ | ❌ | ❌ | ✔ | ✔ | 57 | | **baidu** | ❌ | ❌ | ❌ | ✔ | ✔ | 58 | | **uc** | ❌ | ❌ | ❌ | ❌ | ✔ | 59 | 60 | 如果你想要指定平台: 61 | 62 | ```python 63 | from anti_useragent import UserAgent 64 | ua = UserAgent(platform='mac') # windows, iphone, android, linux 65 | ``` 66 | 67 | 如果你想要指定最大最小随机版本: 68 | 69 | ```python 70 | from anti_useragent import UserAgent 71 | ua = UserAgent(max_version=90) 72 | 73 | ua = UserAgent(min_version=50) 74 | ``` 75 | 76 | 如果你要使用了loguru日志开启: 77 | 78 | ```python 79 | from anti_useragent import UserAgent 80 | ua = UserAgent(logger=True) 81 | 82 | # the default install loguru 83 | try: 84 | from loguru import logger 85 | except: 86 | install("loguru") 87 | from loguru import logger 88 | ``` 89 | 90 | 91 | 92 | 确保你的包是最新版本 93 | 94 | ``` 95 | pip install -U anti-useragent 96 | ``` 97 | 98 | 检查你的版本通过python控制台: 99 | 100 | ``` 101 | import anti_useragent 102 | 103 | print(anti_useragent.VERSION) 104 | ``` 105 | 增加一些实用的工具使用方法: 106 | ```python 107 | # requests: 108 | from anti_useragent.utils.cipers import set_requests_cipers, set_tls_protocol 109 | 110 | # ja3 tls verify 111 | @set_requests_cipers 112 | def get_html(): 113 | requests.get(...) 114 | 115 | # ja3 tls version 116 | session = set_tls_protocol(version="TLSv1_2") 117 | 118 | 119 | # aiohttp: 120 | from anti_useragent.utils.cipers import sslgen 121 | async with ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session: 122 | # ja3 tls verify 123 | await session.get(..., ssl=sslgen()) 124 | 125 | # ja3 tls version 126 | await session.get(..., ssl=sslgen(_ssl="TLSv1_2")) 127 | 128 | # scrapy: 129 | # settings.py ja3 tls verify 130 | DOWNLOADER_CLIENTCONTEXTFACTORY = 'anti_useragent.utils.scrapy_contextfactory.Ja3ScrapyClientContextFactory' 131 | 132 | ``` 133 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # anti-useragent 2 | 3 | 4 | 5 | > info: fake pc or app browser useragent, anti useragent, and other awesome tools 6 | 7 | ## Features 8 | 9 | - more browser up to date 10 | - more randomize ruler 11 | - more fun awesome tools 12 | 13 | English | [中文](./doc/README_ZH.md) 14 | 15 | ### Installation 16 | 17 | ```shell 18 | pip install anti-useragent 19 | ``` 20 | 21 | ### Usage 22 | 23 | ```python 24 | from anti_useragent import UserAgent 25 | ua = UserAgent() 26 | 27 | ua.opera 28 | # Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11 29 | ua.chrome 30 | # Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36 31 | ua['chrome'] 32 | # Mozilla/5.0 (Windows NT 5.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.2.3576.5 Safari/537.36 33 | ua.firefox 34 | # Mozilla/5.0 (Windows NT 5.1; WOW64; rv:47.0) Gecko/20100101 Firefox/45.0 35 | ua['firefox'] 36 | # Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:49.0) Gecko/20100101 Firefox/31.0 37 | ua.android 38 | # Mozilla/5.0 (Linux; Android 7.5.2; M571C Build/LMY47D) AppleWebKit/666.7 (KHTML, like Gecko) Chrome/72.7.7953.78 Mobile Safari/666.7 39 | ua.iphone 40 | # Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_1 like Mac OS X) AppleWebKit/349.56 (KHTML, like Gecko) Mobile/J9UMJN baiduboxapp/0_17.7.6.6_enohpi_8957_628/2.01_4C2%258enohPi/1099a/P0SJ2RX4DXJT3RW906040KVOSH2E76RJUNHVIJUPCJQCZMEM2GL/1 41 | ua.wechat 42 | # Mozilla/5.0 (Linux; Android 10.9.8; MI 5 Build/NRD90M) AppleWebKit/536.93 (KHTML, like Gecko) Chrome/81.7.8549.56 Mobile Safari/536.93 43 | 44 | # and the best one, random via real world browser usage statistic 45 | ua.random 46 | # Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.3.8610.5 Safari/537.36 47 | ``` 48 | 49 | ### Supported platform 50 | 51 | | browser/platfom | windows | mac | linux | iphone | android | 52 | | :-------------: | :-----: | :--: | :---: | :----: | :-----: | 53 | | **chrome** | ✔ | ✔ | ✔ | ✔ | ✔ | 54 | | **firefox** | ✔ | ✔ | ✔ | ❌ | ❌ | 55 | | **opera** | ✔ | ✔ | ✔ | ❌ | ❌ | 56 | | **wechat** | ❌ | ❌ | ❌ | ✔ | ✔ | 57 | | **baidu** | ❌ | ❌ | ❌ | ✔ | ✔ | 58 | | **uc** | ❌ | ❌ | ❌ | ❌ | ✔ | 59 | 60 | If You want to specify the platform just: 61 | 62 | ```python 63 | from anti_useragent import UserAgent 64 | ua = UserAgent(platform='mac') # windows, linux, android, iphone 65 | ``` 66 | 67 | If You want to specify the browser max version or min version just: 68 | 69 | ```python 70 | from anti_useragent import UserAgent 71 | ua = UserAgent(max_version=90) 72 | 73 | ua = UserAgent(min_version=50) 74 | 75 | # 1.0.9 new supported 76 | ua = UserAgent(versions=(90, 100)) 77 | ``` 78 | 79 | If You want to specify the enable logger just: 80 | 81 | ```python 82 | from anti_useragent import UserAgent 83 | ua = UserAgent(logger=True) 84 | 85 | # the default install loguru 86 | try: 87 | from loguru import logger 88 | except: 89 | install("loguru") 90 | from loguru import logger 91 | ``` 92 | 93 | 94 | 95 | Make sure that You using latest version 96 | 97 | ``` 98 | pip install -U anti-useragent 99 | ``` 100 | 101 | Check version via python console: 102 | 103 | ``` 104 | import anti_useragent 105 | 106 | print(anti_useragent.VERSION) 107 | ``` 108 | Add awesome tools usage: 109 | ```python 110 | # requests: 111 | from anti_useragent.utils.cipers import set_requests_cipers, set_tls_protocol 112 | 113 | # ja3 tls verify 114 | @set_requests_cipers 115 | def get_html(): 116 | requests.get(...) 117 | 118 | # ja3 tls version 119 | session = set_tls_protocol(version="TLSv1_2") 120 | 121 | 122 | # aiohttp: 123 | from anti_useragent.utils.cipers import sslgen 124 | async with ClientSession(connector=aiohttp.TCPConnector(ssl=False)) as session: 125 | # ja3 tls verify 126 | await session.get(..., ssl=sslgen()) 127 | 128 | # ja3 tls version 129 | await session.get(..., ssl=sslgen(_ssl="TLSv1_2")) 130 | 131 | # scrapy: 132 | # settings.py ja3 tls verify 133 | DOWNLOADER_CLIENTCONTEXTFACTORY = 'anti_useragent.utils.scrapy_contextfactory.Ja3ScrapyClientContextFactory' 134 | 135 | ``` 136 | -------------------------------------------------------------------------------- /anti_useragent/utils/cipers.py: -------------------------------------------------------------------------------- 1 | import random 2 | import ssl 3 | import requests 4 | 5 | from loguru import logger 6 | from copy import deepcopy 7 | from requests.adapters import HTTPAdapter 8 | from requests.packages.urllib3.poolmanager import PoolManager 9 | 10 | 11 | ORIGIN_CIPHERS = 'ECDHE-ECDSA-AES256-CCM:ECDHE-ECDSA-AES128-CCM8:ECDHE-ECDSA-AES256-CCM8:DHE-RSA-AES128-CCM:DHE-RSA-AES256-CCM:AES128-CCM8:AES256-CCM8:DHE-RSA-AES128-CCM8:DHE-RSA-AES256-CCM8:ADH-AES128-SHA256:ADH-AES256-SHA256:ADH-AES128-GCM-SHA256:ADH-AES256-GCM-SHA384:AES128-CCM:AES256-CCM:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-DSS-AES128-SHA256:DHE-DSS-AES256-SHA256:DHE-DSS-AES128-GCM-SHA256:DHE-DSS-AES256-GCM-SHA384:DHE-RSA-AES128-SHA256:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:RSA+AES128:ALL:!ADH:@STRENGTH:HIGH:DEFAULT:!DH:!aNULL:!eNULL:!LOW:!ADH:!RC4:!3DES:!MD5:!EXP:!PSK:!SRP:!DSS' 12 | 13 | 14 | class _SSLMethod(object): 15 | ssl_method = { 16 | 'SSLv23': ssl.PROTOCOL_SSLv23, 17 | 'TLSv1': ssl.PROTOCOL_TLSv1, 18 | 'TLSv1_1': ssl.PROTOCOL_TLSv1_1, 19 | 'TLSv1_2': ssl.PROTOCOL_TLSv1_2, 20 | 'TLS': ssl.PROTOCOL_TLS, 21 | 'TLS_CLIENT': ssl.PROTOCOL_TLS_CLIENT, 22 | 'TLS_SERVER': ssl.PROTOCOL_TLS_SERVER, 23 | } 24 | 25 | ssl_context = { 26 | 'SSLv2': ssl.OP_NO_SSLv2, 27 | 'SSLv3': ssl.OP_NO_SSLv3, 28 | 'TLSv1': ssl.OP_NO_TLSv1, 29 | 'TLSv1_1': ssl.OP_NO_TLSv1_1, 30 | 'TLSv1_2': ssl.OP_NO_TLSv1_2, 31 | 'TLSv1_3': ssl.OP_NO_TLSv1_3, 32 | } 33 | 34 | def __init__(self, version=None): 35 | self.version: str = version or "TLSv1_2" 36 | 37 | @property 38 | def gen(self): 39 | return self.ssl_method[self.version] 40 | 41 | @property 42 | def context(self): 43 | _ssl_context = deepcopy(self.ssl_context) 44 | del _ssl_context[self.version] 45 | return _ssl_context 46 | 47 | 48 | 49 | class CipherFactory: 50 | def __init__(self, cipher: str = None): 51 | if cipher is None: 52 | cipher = ORIGIN_CIPHERS 53 | self.cipher = cipher 54 | 55 | @classmethod 56 | def setter_cipher(cls, val: str = None): 57 | return cls(val) 58 | 59 | def __call__(self) -> str: 60 | ciphers_list = self.cipher.split(':') 61 | random.shuffle(ciphers_list) 62 | ciphers_real = ':'.join(ciphers_list) 63 | return ciphers_real 64 | 65 | 66 | generate_cipher = CipherFactory() 67 | 68 | 69 | class SSLFactory: 70 | cipers = generate_cipher 71 | 72 | def __call__(self, _ssl: str = 'TLSv1_2') -> ssl.SSLContext: 73 | _verion_set = _SSLMethod(_ssl).context 74 | ciphers = self.cipers() + ":!aNULL:!eNULL:!MD5" 75 | 76 | context = ssl.create_default_context() 77 | for ssl_option in _verion_set.values(): 78 | context.options |= ssl_option 79 | context.set_ciphers(ciphers) 80 | return context 81 | 82 | 83 | sslgen = SSLFactory() 84 | 85 | 86 | def set_requests_cipers(func): 87 | """[jar3 anti spider] decorator requests package to use random cipers""" 88 | def inner(*args, **kwargs): 89 | requests.adapters.DEFAULT_RETRIES = 50 90 | requests.packages.urllib3.disable_warnings() 91 | cipers_real = generate_cipher() 92 | # logger.debug(cipers_real) 93 | requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS = cipers_real 94 | return func(*args, **kwargs) 95 | return inner 96 | 97 | 98 | def set_tls_protocol(version: str) -> requests.Session: 99 | """[jar3 anti spider] requtest package set tls version""" 100 | 101 | _version = _SSLMethod(version).gen 102 | 103 | class Ssl3HttpAdapter(HTTPAdapter): 104 | """"Transport adapter" that allows us to use SSLv3.""" 105 | 106 | def init_poolmanager(self, connections, maxsize, block=False, **kwargs): 107 | self.poolmanager = PoolManager( 108 | num_pools=connections, 109 | maxsize=maxsize, 110 | block=block, 111 | ssl_version=_version) 112 | 113 | s = requests.Session() 114 | s.mount('https://', Ssl3HttpAdapter()) 115 | return s 116 | 117 | 118 | __all__ = [ 119 | sslgen, 120 | set_tls_protocol, 121 | set_requests_cipers, 122 | generate_cipher 123 | ] 124 | -------------------------------------------------------------------------------- /anti_useragent/settings/default_settings.py: -------------------------------------------------------------------------------- 1 | 2 | BASE_USER_AGENT_FIREFOX = 'Mozilla/5.0 (%(system_info)s; %(system_bit)s; rv:%(r_version)s)' \ 3 | ' Gecko/20100101 Firefox/%(version)s' 4 | 5 | BASE_USER_AGENT_CHROME = 'Mozilla/5.0 (%(system_info)s; %(system_bit)s) ' \ 6 | 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome' \ 7 | '/%(big_version)s.%(mid_version)s.%(small_version)s.%(beta_version)s Safari/537.36' 8 | 9 | BASE_USER_AGENT_OPERA = 'Mozilla/5.0 (%(system_info)s; %(system_bit)s) ' \ 10 | 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome' \ 11 | '/%(big_version)s.%(mid_version)s.%(small_version)s.%(beta_version)s Safari/537.36 ' \ 12 | 'OPR/%(o_version)s.0.%(os_version)s.%(ob_version)s' 13 | 14 | BASE_USER_AGENT_CHROME_ANDROID = 'Mozilla/5.0 (Linux; Android %(android_version)s; '\ 15 | '%(android_phone)s) AppleWebKit/%(safari)s (KHTML, like Gecko) '\ 16 | 'Chrome/%(chrome)s Mobile Safari/%(safari)s' 17 | 18 | BASE_USER_AGENT_CHROME_IPHONE = 'Mozilla/5.0 (iPhone; CPU iPhone OS %(mac_version)s} like Mac OS X)'\ 19 | ' AppleWebKit/%(safari)s (KHTML, like Gecko) Version/9.0 Mobile/%(mobile)s Safari/%(safari)s' 20 | 21 | BASE_USER_AGENT_WECHAT_IPHONE = 'Mozilla/5.0 (iPhone; CPU iPhone OS %(mac_version)s like Mac OS X) '\ 22 | 'AppleWebKit/%(safari)s (KHTML, like Gecko) Mobile/%(mobile)s MicroMessenger/%(micro_messenger)s '\ 23 | 'NetType/%(net_type)s Language/zh_CN' 24 | 25 | BASE_USER_AGENT_WECHAT_ANDROID = 'Mozilla/5.0 (Linux; Android %(android_version)s; %(android_phone)s) '\ 26 | 'AppleWebKit/%(safari)s (KHTML, like Gecko) Version/4.0 Chrome/%(chrome)s Mobile MQQBrowser/6.2 '\ 27 | 'TBS/%(TBS)s Safari/%(safari)s MicroMessenger/%(micro_messenger)s NetType/%(net_type)s Language/zh_CN' 28 | 29 | BASE_USER_AGENT_UC = 'Mozilla/5.0 (Linux; U; Android %(android_version)s; zh-cn; %(android_phone)s AppleWebKit/%(safari)s (KHTML, like Gecko)'\ 30 | ' Version/4.0 UCBrowser/1.0.0.100 U3/0.8.0 Mobile Safari/%(safari)s AliApp(TB/6.6.4) WindVane/8.0.0 1080X1920 GCanvas/1.4.2.21' 31 | 32 | BASE_USER_AGENT_BAIDU_BOX_ANDROID = 'Mozilla/5.0 (Linux; Android %(android_version)s; %(android_phone)s) AppleWebKit/%(safari)s (KHTML, like Gecko) '\ 33 | 'Version/4.0 Chrome/%(chrome)s Mobile Safari/%(safari)s T7/7.4 baiduboxapp/%(baidu_box_app_android)s (Baidu; P1 %(android_version)s)' 34 | 35 | BASE_USER_AGENT_BAIDU_BOX_IPHONE = 'Mozilla/5.0 (iPhone; CPU iPhone OS {mac_version} like Mac OS X) AppleWebKit/{safari} '\ 36 | '(KHTML, like Gecko) Mobile/{mobile} baiduboxapp/{baidu_box_app_iphone}/2.01_4C2%258enohPi/1099a/{key}/1' 37 | 38 | WIN_SYSTEM = [ 39 | 'Windows NT 5.0', 40 | 'Windows NT 5.1', 41 | 'Windows NT 6.0', 42 | 'Windows NT 6.1', 43 | 'Windows NT 6.2', 44 | 'Windows NT 6.3', 45 | 'Windows NT 10.0', 46 | ] 47 | 48 | WIN_SYSTEM_BIT = [ 49 | 'Win64; x64', 50 | 'WOW64', 51 | ] 52 | 53 | MAC_SYSTEM = [ 54 | 'Macintosh', 55 | ] 56 | 57 | MAC_SYSTEM_BIT = [ 58 | 'PPC Mac OS X', 59 | 'Intel Mac OS X' 60 | ] 61 | 62 | LINUX_SYSTEM = [ 63 | 'X11' 64 | ] 65 | 66 | LINUX_SYSTEM_BIT = [ 67 | 'Linux ppc', 68 | 'Linux ppc64', 69 | 'Linux i686', 70 | 'Linux x86_64', 71 | ] 72 | 73 | PLATFORM = ['windows', 'mac', 'linux', 'android', 'iphone'] 74 | 75 | ANDROID_SYSTEM_LIST = [ 76 | 'Nexus 4 Build/KOT49H', 'Nexus 5 Build/MRA58N', 'Nexus 6 Build/LYZ28E', 77 | 'Nexus 7 Build/JSS15Q', 'Nexus 8 Build/MRA58N', 'Nexus 9 Build/LYZ28E', 78 | # 三星 79 | 'GT-I9152P Build/JLS36C', 'SM-E7000 Build/KTU84P', 'SM-G9200 Build/LMY47X', 80 | 'GT-I9128I Build/JDQ39', 'GT-I9500 Build/JDQ39', 'SM-N9008V Build/LRX21V', 81 | 'SM-N7506V Build/JLS36C', 'SM-G3609 Build/KTU84P', 'SCH-W2013 Build/IMM76D', 82 | # LG 83 | 'LGMS323 Build/KOT49I.MS32310c', 84 | # OPPO/VIVO 85 | 'OPPO R7 Build/KTU84P', 'OPPO R7t Build/KTU84P', 'R7007 Build/JLS36C', 'R2017 Build/JLS36C', 'R6007 Build/JLS36C', 86 | '1105 Build/KTU84P', 'N5117 Build/JLS36C', 'M571C Build/LMY47D', 'R7Plus Build/LRX21M', 'X909T Build/JDQ39', 87 | 'A31t Build/KTU84P', 'A31 Build/KTU84P', 'R8207 Build/KTU84P', 'R833T Build/JDQ39', 88 | 89 | 'vivo Y13iL Build/KTU84P', 'vivo X5Pro D Build/LRX21M', 'vivo Y22L Build/JLS36C', 'vivo Y13T Build/JDQ39', 90 | 'vivo X5Max Build/KTU84P', 'ONE A2001 Build/LMY48W', 91 | # 华为 92 | 'VIE-AL10 Build/HUAWEIVIE-AL10; wv', 'HUAWEI NXT-AL10 Build/HUAWEINXT-AL10', 'HUAWEI NXT-CL00 Build/HUAWEINXT-CL00', 93 | 'Che2-TL00M Build/HonorChe2-TL00M; wv', 'FRD-AL10 Build/HUAWEIFRD-AL10', 'HUAWEI RIO-AL00 Build/HuaweiRIO-AL00', 94 | 'HUAWEI C199 Build/HuaweiC199', 'HUAWEI RIO-TL00 Build/HUAWEIRIO-TL00; wv', 'HUAWEI TAG-TL00 Build/HUAWEITAG-TL00', 95 | 'HUAWEI MT7-CL00 Build/HuaweiMT7-CL00; wv', 'PLE-703L Build/HuaweiMediaPad; wv', 'PLK-TL01H Build/HONORPLK-TL01H', 96 | 'EVA-AL10 Build/HUAWEIEVA-AL10', 97 | # 小米 98 | 'MI MAX Build/MMB29M', 'MI 5 Build/NRD90M', 'MI NOTE LTE Build/KTU84P', 'MI 3C Build/MMB29M', 'MI 5s Build/MXB48T', 99 | 'MI NOTE LTE Build/MMB29M', 'MI 2S Build/JRO03L', 'MI 5 Build/MXB48T', 'MI NOTE Pro Build/LRX22G', 100 | # 联想ZUK 101 | 'Z2 Plus Build/N2G47O; wv', 102 | ] 103 | 104 | NET_TYPE = ['4G', 'WIFI'] 105 | 106 | PLATFORM_OVERRIDES = { 107 | 'windows': [WIN_SYSTEM, WIN_SYSTEM_BIT], 108 | 'mac': [MAC_SYSTEM, MAC_SYSTEM_BIT], 109 | 'linux': [LINUX_SYSTEM, LINUX_SYSTEM_BIT], 110 | 'android': [ANDROID_SYSTEM_LIST, NET_TYPE], 111 | 'iphone': [ANDROID_SYSTEM_LIST, NET_TYPE], 112 | } 113 | 114 | PLATFORM_UA_MAP = { 115 | 'android': [ 116 | 'chrome_android', 117 | 'wechat_android', 118 | 'baidu_android', 119 | 'uc' 120 | ], 121 | 'iphone': [ 122 | 'chrome_iphone', 123 | 'wechat_iphone', 124 | 'baidu_iphone', 125 | ], 126 | 'windows': [ 127 | 'chrome', 128 | 'firefox', 129 | 'opera', 130 | ], 131 | 'linux': [ 132 | 'chrome', 133 | 'firefox', 134 | 'opera', 135 | ], 136 | 'mac': [ 137 | 'chrome', 138 | 'firefox', 139 | 'opera', 140 | ], 141 | } 142 | -------------------------------------------------------------------------------- /anti_useragent/settings/__init__.py: -------------------------------------------------------------------------------- 1 | import json 2 | import copy 3 | from collections.abc import MutableMapping 4 | from importlib import import_module 5 | from pprint import pformat 6 | 7 | from . import default_settings 8 | 9 | 10 | SETTINGS_PRIORITIES = { 11 | 'default': 0, 12 | 'command': 10, 13 | 'project': 20, 14 | 'spider': 30, 15 | 'cmdline': 40, 16 | } 17 | 18 | 19 | def get_settings_priority(priority) -> int: 20 | """ 21 | get settings priority [SETTINGS_PRIORITIES] ~ `from settings import SETTINGS_PRIORITIES` 22 | :param priority: config | others 23 | :return: int 24 | """ 25 | if isinstance(priority, str): 26 | return SETTINGS_PRIORITIES[priority] 27 | else: 28 | return priority 29 | 30 | 31 | class SettingsAttribute: 32 | 33 | def __init__(self, value, priority): 34 | self.value = value 35 | if isinstance(self.value, BaseSettings): 36 | self.priority = max(self.value.maxpriority(), priority) 37 | else: 38 | self.priority = priority 39 | 40 | def set(self, value, priority): 41 | if priority >= self.priority: 42 | if isinstance(self.value, BaseSettings): 43 | value = BaseSettings(value, priority=priority) 44 | self.value = value 45 | self.priority = priority 46 | 47 | def __str__(self): 48 | return f"" 49 | 50 | __repr__ = __str__ 51 | 52 | 53 | class BaseSettings(MutableMapping): 54 | 55 | def __init__(self, values=None, priority='project'): 56 | self.frozen = False 57 | self.attributes = {} 58 | if values: 59 | self.update(values, priority) 60 | 61 | def __getitem__(self, opt_name): 62 | if opt_name not in self: 63 | return None 64 | return self.attributes[opt_name].value 65 | 66 | def __contains__(self, name): 67 | return name in self.attributes 68 | 69 | def get(self, name, default=None): 70 | return self[name] if self[name] is not None else default 71 | 72 | def getbool(self, name, default=False): 73 | got = self.get(name, default) 74 | try: 75 | return bool(int(got)) 76 | except ValueError: 77 | if got in ("True", "true"): 78 | return True 79 | if got in ("False", "false"): 80 | return False 81 | raise ValueError("Supported values for boolean settings " 82 | "are 0/1, True/False, '0'/'1', " 83 | "'True'/'False' and 'true'/'false'") 84 | 85 | def getint(self, name, default=0): 86 | return int(self.get(name, default)) 87 | 88 | def getfloat(self, name, default=0.0): 89 | return float(self.get(name, default)) 90 | 91 | def getlist(self, name, default=None): 92 | 93 | value = self.get(name, default or []) 94 | if isinstance(value, str): 95 | value = value.split(',') 96 | return list(value) 97 | 98 | def getdict(self, name, default=None): 99 | value = self.get(name, default or {}) 100 | if isinstance(value, str): 101 | value = json.loads(value) 102 | return dict(value) 103 | 104 | def getwithbase(self, name): 105 | compbs = BaseSettings() 106 | compbs.update(self[name + '_BASE']) 107 | compbs.update(self[name]) 108 | return compbs 109 | 110 | def getpriority(self, name): 111 | if name not in self: 112 | return None 113 | return self.attributes[name].priority 114 | 115 | def maxpriority(self): 116 | if len(self) > 0: 117 | return max(self.getpriority(name) for name in self) 118 | else: 119 | return get_settings_priority('default') 120 | 121 | def __setitem__(self, name, value): 122 | self.set(name, value) 123 | 124 | def set(self, name, value, priority='project'): 125 | self._assert_mutability() 126 | priority = get_settings_priority(priority) 127 | if name not in self: 128 | if isinstance(value, SettingsAttribute): 129 | self.attributes[name] = value 130 | else: 131 | self.attributes[name] = SettingsAttribute(value, priority) 132 | else: 133 | self.attributes[name].set(value, priority) 134 | 135 | def setdict(self, values, priority='project'): 136 | self.update(values, priority) 137 | 138 | def setmodule(self, module, priority='project'): 139 | self._assert_mutability() 140 | if isinstance(module, str): 141 | module = import_module(module) 142 | for key in dir(module): 143 | if key.isupper(): 144 | self.set(key, getattr(module, key), priority) 145 | 146 | def update(self, values, priority='project'): 147 | self._assert_mutability() 148 | if isinstance(values, str): 149 | values = json.loads(values) 150 | if values is not None: 151 | if isinstance(values, BaseSettings): 152 | for name, value in values.items(): 153 | self.set(name, value, values.getpriority(name)) 154 | else: 155 | for name, value in values.items(): 156 | self.set(name, value, priority) 157 | 158 | def delete(self, name, priority='project'): 159 | self._assert_mutability() 160 | priority = get_settings_priority(priority) 161 | if priority >= self.getpriority(name): 162 | del self.attributes[name] 163 | 164 | def __delitem__(self, name): 165 | self._assert_mutability() 166 | del self.attributes[name] 167 | 168 | def _assert_mutability(self): 169 | if self.frozen: 170 | raise TypeError("Trying to modify an immutable Settings object") 171 | 172 | def copy(self): 173 | return copy.deepcopy(self) 174 | 175 | def freeze(self): 176 | self.frozen = True 177 | 178 | def frozencopy(self): 179 | copy = self.copy() 180 | copy.freeze() 181 | return copy 182 | 183 | def __iter__(self): 184 | return iter(self.attributes) 185 | 186 | def __len__(self): 187 | return len(self.attributes) 188 | 189 | def _to_dict(self): 190 | return {k: (v._to_dict() if isinstance(v, BaseSettings) else v) 191 | for k, v in self.items()} 192 | 193 | def copy_to_dict(self): 194 | settings = self.copy() 195 | return settings._to_dict() 196 | 197 | def _repr_pretty_(self, p, cycle): 198 | if cycle: 199 | p.text(repr(self)) 200 | else: 201 | p.text(pformat(self.copy_to_dict())) 202 | 203 | 204 | class _DictProxy(MutableMapping): 205 | 206 | def __init__(self, settings, priority): 207 | self.o = {} 208 | self.settings = settings 209 | self.priority = priority 210 | 211 | def __len__(self): 212 | return len(self.o) 213 | 214 | def __getitem__(self, k): 215 | return self.o[k] 216 | 217 | def __setitem__(self, k, v): 218 | self.settings.set(k, v, priority=self.priority) 219 | self.o[k] = v 220 | 221 | def __delitem__(self, k): 222 | del self.o[k] 223 | 224 | def __iter__(self, k, v): 225 | return iter(self.o) 226 | 227 | 228 | class Settings(BaseSettings): 229 | 230 | def __init__(self, values=None, priority='project'): 231 | super().__init__() 232 | self.setmodule(default_settings, 'default') 233 | for name, val in self.items(): 234 | if isinstance(val, dict): 235 | self.set(name, BaseSettings(val, 'default'), 'default') 236 | self.update(values, priority) 237 | 238 | 239 | def iter_default_settings(): 240 | for name in dir(default_settings): 241 | if name.isupper(): 242 | yield name, getattr(default_settings, name) 243 | 244 | 245 | def overridden_settings(settings): 246 | for name, defvalue in iter_default_settings(): 247 | value = settings[name] 248 | if not isinstance(defvalue, dict) and value != defvalue: 249 | yield name, value 250 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | --------------------------------------------------------------------------------