Skip to content

Commit

Permalink
Merge pull request #3 from iamdual/v0.2
Browse files Browse the repository at this point in the history
Merging v0.2.0
  • Loading branch information
iamdual authored Mar 9, 2024
2 parents e3134c0 + 394c176 commit 77ef2c3
Show file tree
Hide file tree
Showing 25 changed files with 197 additions and 235 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = ua-generator
version = 0.1.11
version = 0.2.0
author = Ekin Karadeniz
author_email = iamdual@icloud.com
description = A random user-agent generator
Expand Down
6 changes: 3 additions & 3 deletions src/ua_generator/__init__.py
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)
78 changes: 78 additions & 0 deletions src/ua_generator/client_hints.py
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
2 changes: 1 addition & 1 deletion src/ua_generator/data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""

Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/browsers/chrome.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/browsers/edge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/browsers/firefox.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/browsers/safari.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
14 changes: 7 additions & 7 deletions src/ua_generator/data/generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
from .browsers import chrome, safari, firefox, edge
Expand All @@ -14,11 +14,11 @@ def __init__(self, device, platform, browser):
self.platform = platform
self.browser = browser

self.platform_version = self.generate_platform_version()
self.browser_version = self.generate_browser_version()
self.user_agent = self.generate_user_agent()
self.platform_version = self.__platform_version()
self.browser_version = self.__browser_version()
self.user_agent = self.__user_agent()

def generate_platform_version(self):
def __platform_version(self):
if self.platform == 'windows':
return windows.get_version()
elif self.platform == 'macos':
Expand All @@ -30,7 +30,7 @@ def generate_platform_version(self):
elif self.platform == 'android':
return android.get_version()

def generate_browser_version(self):
def __browser_version(self):
if self.browser == 'chrome':
return chrome.get_version()
elif self.browser == 'safari':
Expand All @@ -40,7 +40,7 @@ def generate_browser_version(self):
elif self.browser == 'edge':
return edge.get_version()

def generate_user_agent(self):
def __user_agent(self):
if self.platform == 'windows':
if self.browser == 'chrome':
template = utils.choice((
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/android/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/android/android_nexus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/android/android_pixel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/android/android_samsung.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/ios.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/linux.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/macos.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/data/platforms/windows.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
import random
Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""

Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/formats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2022 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""

Expand Down
2 changes: 1 addition & 1 deletion src/ua_generator/serialization.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Random User-Agent
Copyright: 2024 Ekin Karadeniz (github.com/iamdual)
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""

Expand Down
85 changes: 85 additions & 0 deletions src/ua_generator/user_agent.py
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
Loading

0 comments on commit 77ef2c3

Please sign in to comment.