-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
795 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# anti-useragent | ||
|
||
|
||
|
||
> info: fake chrome, firefox, opera browser header anti useragent | ||
## Features | ||
|
||
- more browser up to date | ||
- more randomize ruler | ||
- grabs up to date `useragent` from [useragentstring.com](http://useragentstring.com/) | ||
|
||
### Installation | ||
|
||
```shell | ||
pip install anti-useragent | ||
``` | ||
|
||
### Usage | ||
|
||
```python | ||
from anti_useragent import UserAgent | ||
ua = UserAgent() | ||
|
||
ua.opera | ||
# Opera/9.80 (X11; Linux i686; U; ru) Presto/2.8.131 Version/11.11 | ||
ua.chrome | ||
# Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36 | ||
ua['chrome'] | ||
# Mozilla/5.0 (Windows NT 5.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.2.3576.5 Safari/537.36 | ||
ua.firefox | ||
# Mozilla/5.0 (Windows NT 5.1; WOW64; rv:47.0) Gecko/20100101 Firefox/45.0 | ||
ua['firefox'] | ||
# Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:49.0) Gecko/20100101 Firefox/31.0 | ||
|
||
# and the best one, random via real world browser usage statistic | ||
ua.random | ||
# Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.3.8610.5 Safari/537.36 | ||
``` | ||
|
||
|
||
|
||
If You want to specify the platform just: | ||
|
||
```python | ||
from anti_useragent import UserAgent | ||
ua = UserAgent(platform='mac') | ||
``` | ||
|
||
If You want to specify the browser max version just: | ||
|
||
```python | ||
from anti_useragent import UserAgent | ||
ua = UserAgent(version=90) | ||
``` | ||
|
||
If You want to specify the enable logger just: | ||
|
||
```python | ||
from anti_useragent import UserAgent | ||
ua = UserAgent(logger=True) | ||
|
||
# the default install loguru | ||
try: | ||
from loguru import logger | ||
except: | ||
install("loguru") | ||
from loguru import logger | ||
``` | ||
|
||
|
||
|
||
Make sure that You using latest version | ||
|
||
``` | ||
pip install -U anti-useragent | ||
``` | ||
|
||
Check version via python console: | ||
|
||
``` | ||
import anti_useragent | ||
print(anti_useragent.VERSION) | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
from anti_useragent.useragent.ua import AntiUserAgent, UserAgent | ||
|
||
__version__ = '0.0.1' | ||
|
||
VERSION = __version__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from __future__ import absolute_import, unicode_literals | ||
|
||
|
||
class AntiUserAgentError(Exception): | ||
pass | ||
|
||
|
||
UserAgentError = AntiUserAgentError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,249 @@ | ||
import json | ||
import copy | ||
from collections.abc import MutableMapping | ||
from importlib import import_module | ||
from pprint import pformat | ||
|
||
from . import default_settings | ||
|
||
|
||
SETTINGS_PRIORITIES = { | ||
'default': 0, | ||
'command': 10, | ||
'project': 20, | ||
'spider': 30, | ||
'cmdline': 40, | ||
} | ||
|
||
|
||
def get_settings_priority(priority) -> int: | ||
""" | ||
get settings priority [SETTINGS_PRIORITIES] ~ `from settings import SETTINGS_PRIORITIES` | ||
:param priority: config | others | ||
:return: int | ||
""" | ||
if isinstance(priority, str): | ||
return SETTINGS_PRIORITIES[priority] | ||
else: | ||
return priority | ||
|
||
|
||
class SettingsAttribute: | ||
|
||
def __init__(self, value, priority): | ||
self.value = value | ||
if isinstance(self.value, BaseSettings): | ||
self.priority = max(self.value.maxpriority(), priority) | ||
else: | ||
self.priority = priority | ||
|
||
def set(self, value, priority): | ||
if priority >= self.priority: | ||
if isinstance(self.value, BaseSettings): | ||
value = BaseSettings(value, priority=priority) | ||
self.value = value | ||
self.priority = priority | ||
|
||
def __str__(self): | ||
return f"<SettingsAttribute value={self.value!r} priority={self.priority}>" | ||
|
||
__repr__ = __str__ | ||
|
||
|
||
class BaseSettings(MutableMapping): | ||
|
||
def __init__(self, values=None, priority='project'): | ||
self.frozen = False | ||
self.attributes = {} | ||
if values: | ||
self.update(values, priority) | ||
|
||
def __getitem__(self, opt_name): | ||
if opt_name not in self: | ||
return None | ||
return self.attributes[opt_name].value | ||
|
||
def __contains__(self, name): | ||
return name in self.attributes | ||
|
||
def get(self, name, default=None): | ||
return self[name] if self[name] is not None else default | ||
|
||
def getbool(self, name, default=False): | ||
got = self.get(name, default) | ||
try: | ||
return bool(int(got)) | ||
except ValueError: | ||
if got in ("True", "true"): | ||
return True | ||
if got in ("False", "false"): | ||
return False | ||
raise ValueError("Supported values for boolean settings " | ||
"are 0/1, True/False, '0'/'1', " | ||
"'True'/'False' and 'true'/'false'") | ||
|
||
def getint(self, name, default=0): | ||
return int(self.get(name, default)) | ||
|
||
def getfloat(self, name, default=0.0): | ||
return float(self.get(name, default)) | ||
|
||
def getlist(self, name, default=None): | ||
|
||
value = self.get(name, default or []) | ||
if isinstance(value, str): | ||
value = value.split(',') | ||
return list(value) | ||
|
||
def getdict(self, name, default=None): | ||
value = self.get(name, default or {}) | ||
if isinstance(value, str): | ||
value = json.loads(value) | ||
return dict(value) | ||
|
||
def getwithbase(self, name): | ||
compbs = BaseSettings() | ||
compbs.update(self[name + '_BASE']) | ||
compbs.update(self[name]) | ||
return compbs | ||
|
||
def getpriority(self, name): | ||
if name not in self: | ||
return None | ||
return self.attributes[name].priority | ||
|
||
def maxpriority(self): | ||
if len(self) > 0: | ||
return max(self.getpriority(name) for name in self) | ||
else: | ||
return get_settings_priority('default') | ||
|
||
def __setitem__(self, name, value): | ||
self.set(name, value) | ||
|
||
def set(self, name, value, priority='project'): | ||
self._assert_mutability() | ||
priority = get_settings_priority(priority) | ||
if name not in self: | ||
if isinstance(value, SettingsAttribute): | ||
self.attributes[name] = value | ||
else: | ||
self.attributes[name] = SettingsAttribute(value, priority) | ||
else: | ||
self.attributes[name].set(value, priority) | ||
|
||
def setdict(self, values, priority='project'): | ||
self.update(values, priority) | ||
|
||
def setmodule(self, module, priority='project'): | ||
self._assert_mutability() | ||
if isinstance(module, str): | ||
module = import_module(module) | ||
for key in dir(module): | ||
if key.isupper(): | ||
self.set(key, getattr(module, key), priority) | ||
|
||
def update(self, values, priority='project'): | ||
self._assert_mutability() | ||
if isinstance(values, str): | ||
values = json.loads(values) | ||
if values is not None: | ||
if isinstance(values, BaseSettings): | ||
for name, value in values.items(): | ||
self.set(name, value, values.getpriority(name)) | ||
else: | ||
for name, value in values.items(): | ||
self.set(name, value, priority) | ||
|
||
def delete(self, name, priority='project'): | ||
self._assert_mutability() | ||
priority = get_settings_priority(priority) | ||
if priority >= self.getpriority(name): | ||
del self.attributes[name] | ||
|
||
def __delitem__(self, name): | ||
self._assert_mutability() | ||
del self.attributes[name] | ||
|
||
def _assert_mutability(self): | ||
if self.frozen: | ||
raise TypeError("Trying to modify an immutable Settings object") | ||
|
||
def copy(self): | ||
return copy.deepcopy(self) | ||
|
||
def freeze(self): | ||
self.frozen = True | ||
|
||
def frozencopy(self): | ||
copy = self.copy() | ||
copy.freeze() | ||
return copy | ||
|
||
def __iter__(self): | ||
return iter(self.attributes) | ||
|
||
def __len__(self): | ||
return len(self.attributes) | ||
|
||
def _to_dict(self): | ||
return {k: (v._to_dict() if isinstance(v, BaseSettings) else v) | ||
for k, v in self.items()} | ||
|
||
def copy_to_dict(self): | ||
settings = self.copy() | ||
return settings._to_dict() | ||
|
||
def _repr_pretty_(self, p, cycle): | ||
if cycle: | ||
p.text(repr(self)) | ||
else: | ||
p.text(pformat(self.copy_to_dict())) | ||
|
||
|
||
class _DictProxy(MutableMapping): | ||
|
||
def __init__(self, settings, priority): | ||
self.o = {} | ||
self.settings = settings | ||
self.priority = priority | ||
|
||
def __len__(self): | ||
return len(self.o) | ||
|
||
def __getitem__(self, k): | ||
return self.o[k] | ||
|
||
def __setitem__(self, k, v): | ||
self.settings.set(k, v, priority=self.priority) | ||
self.o[k] = v | ||
|
||
def __delitem__(self, k): | ||
del self.o[k] | ||
|
||
def __iter__(self, k, v): | ||
return iter(self.o) | ||
|
||
|
||
class Settings(BaseSettings): | ||
|
||
def __init__(self, values=None, priority='project'): | ||
super().__init__() | ||
self.setmodule(default_settings, 'default') | ||
for name, val in self.items(): | ||
if isinstance(val, dict): | ||
self.set(name, BaseSettings(val, 'default'), 'default') | ||
self.update(values, priority) | ||
|
||
|
||
def iter_default_settings(): | ||
for name in dir(default_settings): | ||
if name.isupper(): | ||
yield name, getattr(default_settings, name) | ||
|
||
|
||
def overridden_settings(settings): | ||
for name, defvalue in iter_default_settings(): | ||
value = settings[name] | ||
if not isinstance(defvalue, dict) and value != defvalue: | ||
yield name, value |
Oops, something went wrong.