-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from iamdual/v0.2
Merging v0.2.0
- Loading branch information
Showing
25 changed files
with
197 additions
and
235 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
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
""" | ||
Random User-Agent | ||
Copyright: 2022 Ekin Karadeniz (github.com/iamdual) | ||
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual) | ||
License: Apache License 2.0 | ||
""" | ||
from . import useragent | ||
from . import user_agent | ||
|
||
|
||
def generate(**kwargs): | ||
return useragent.UserAgent().generate(**kwargs) | ||
return user_agent.UserAgent(**kwargs) |
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,78 @@ | ||
""" | ||
Random User-Agent | ||
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual) | ||
License: Apache License 2.0 | ||
""" | ||
from . import formats, serialization | ||
from .data import platforms_mobile | ||
from .data import generator | ||
|
||
|
||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA | ||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-UA-Full-Version-List | ||
class ClientHints: | ||
# Type hinting only | ||
mobile: str | ||
platform: str | ||
platform_version: str | ||
brands: str | ||
brands_full_version_list: str | ||
|
||
def __init__(self, gen: generator.Generator): | ||
self.__generator = gen | ||
self.__cache = {} | ||
|
||
def __mobile(self): | ||
return self.__generator.platform in platforms_mobile | ||
|
||
def __platform(self): | ||
platform = self.__generator.platform | ||
|
||
if platform == 'ios': | ||
platform = 'iOS' | ||
elif platform == 'macos': | ||
platform = 'macOS' | ||
else: | ||
platform = platform.title() | ||
|
||
return platform | ||
|
||
def __platform_version(self): | ||
return formats.version(self.__generator.platform_version) | ||
|
||
def __brands(self, full_version_list: bool = False): | ||
brand_list = [{'brand': 'Not A(Brand', 'version': '99'}] | ||
|
||
if full_version_list: | ||
browser_version = formats.version(self.__generator.browser_version) | ||
else: | ||
browser_version = formats.major_version(self.__generator.browser_version) | ||
|
||
if self.__generator.browser == 'chrome': | ||
brand_list.append({'brand': 'Chromium', 'version': browser_version}) | ||
brand_list.append({'brand': 'Google Chrome', 'version': browser_version}) | ||
elif self.__generator.browser == 'edge': | ||
brand_list.append({'brand': 'Chromium', 'version': browser_version}) | ||
brand_list.append({'brand': 'Microsoft Edge', 'version': browser_version}) | ||
|
||
return brand_list | ||
|
||
def __getattr__(self, name): | ||
if name in self.__cache: | ||
return self.__cache[name] | ||
|
||
if name == 'mobile': | ||
self.__cache[name] = serialization.ch_bool(self.__mobile()) | ||
elif name == 'platform': | ||
self.__cache[name] = serialization.ch_string(self.__platform()) | ||
elif name == 'platform_version': | ||
self.__cache[name] = serialization.ch_bool(self.__platform_version()) | ||
elif name == 'brands': | ||
self.__cache[name] = serialization.ch_brand_list(self.__brands()) | ||
elif name == 'brands_full_version_list': | ||
self.__cache[name] = serialization.ch_brand_list(self.__brands(full_version_list=True)) | ||
|
||
return self.__cache[name] | ||
|
||
def __str__(self): | ||
return self.brands |
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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,85 @@ | ||
""" | ||
Random User-Agent | ||
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual) | ||
License: Apache License 2.0 | ||
""" | ||
from . import utils, exceptions | ||
from .data import devices, platforms, platforms_desktop, platforms_mobile, browsers | ||
from .data import generator | ||
from .client_hints import ClientHints | ||
|
||
|
||
class UserAgent: | ||
def __init__(self, device=None, platform=None, browser=None): | ||
self.device: str = device | ||
self.platform: str = platform | ||
self.browser: str = browser | ||
self.__complete() | ||
|
||
# Type hinting only | ||
self.text: str | ||
self.platform_version: dict | ||
self.browser_version: dict | ||
self.ch: ClientHints | ||
|
||
def __find_device(self): | ||
if self.device is not None: | ||
if not utils.contains_multiple(self.device, devices): | ||
raise exceptions.InvalidArgumentError('No device type found: {}'.format(self.device)) | ||
else: | ||
self.device = utils.choice(self.device) | ||
return self.device | ||
|
||
if self.platform is not None and utils.contains_multiple(self.platform, platforms_desktop): | ||
self.device = 'desktop' | ||
elif self.platform is not None and utils.contains_multiple(self.platform, platforms_mobile): | ||
self.device = 'mobile' | ||
elif self.device is None: | ||
self.device = utils.choice(devices) | ||
|
||
return self.device | ||
|
||
def __find_platform(self): | ||
if self.platform is not None: | ||
if not utils.contains_multiple(self.platform, platforms): | ||
raise exceptions.InvalidArgumentError('No platform found: {}'.format(self.platform)) | ||
else: | ||
self.platform = utils.choice(self.platform) | ||
return self.platform | ||
|
||
if self.device is not None and 'desktop' in self.device: | ||
self.platform = utils.choice(platforms_desktop) | ||
elif self.device is not None and 'mobile' in self.device: | ||
self.platform = utils.choice(platforms_mobile) | ||
elif self.platform is None: | ||
self.platform = utils.choice(platforms) | ||
|
||
return self.platform | ||
|
||
def __find_browser(self): | ||
if self.browser is not None: | ||
if not utils.contains_multiple(self.browser, browsers): | ||
raise exceptions.InvalidArgumentError('No browser found: {}'.format(self.browser)) | ||
else: | ||
self.browser = utils.choice(self.browser) | ||
|
||
if self.browser is None: | ||
self.browser = utils.choice(browsers) | ||
|
||
# Safari only support for macOS and iOS | ||
if self.platform != 'macos' and self.platform != 'ios' and self.browser == 'safari': | ||
self.browser = 'chrome' | ||
|
||
return self.browser | ||
|
||
def __complete(self): | ||
self.device = self.__find_device() | ||
self.platform = self.__find_platform() | ||
self.browser = self.__find_browser() | ||
|
||
ua = generator.Generator(device=self.device, platform=self.platform, browser=self.browser) | ||
self.text = ua.user_agent | ||
self.ch = ClientHints(ua) | ||
|
||
def __str__(self): | ||
return self.text |
Oops, something went wrong.