Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typing literals + UserAgent __repr__ #15

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/ua_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"""
import typing

from . import user_agent, options as _options
from . import user_agent, options as _options, data as _data


def generate(device: typing.Union[tuple, str, None] = None,
platform: typing.Union[tuple, str, None] = None,
browser: typing.Union[tuple, str, None] = None,
options: typing.Union[_options.Options, None] = None) -> user_agent.UserAgent:
def generate(
device: typing.Union[tuple[_data._DEVICES_TYPE], _data._DEVICES_TYPE, None] = None,
platform: typing.Union[tuple[_data._PLATFORMS_TYPE], _data._PLATFORMS_TYPE, None] = None,
browser: typing.Union[tuple[_data._BROWSERS_TYPE], _data._BROWSERS_TYPE, None] = None,
options: typing.Union[_options.Options, None] = None
):
return user_agent.UserAgent(device, platform, browser, options)
5 changes: 5 additions & 0 deletions src/ua_generator/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
Copyright: 2022-2024 Ekin Karadeniz (github.com/iamdual)
License: Apache License 2.0
"""
from typing import Literal

DEVICES = ('desktop', 'mobile')
_DEVICES_TYPE = Literal['desktop', 'mobile', None]

PLATFORMS = ('windows', 'macos', 'ios', 'linux', 'android')
_PLATFORMS_TYPE = Literal['windows', 'macos', 'ios', 'linux', 'android', None]
PLATFORMS_DESKTOP = ('windows', 'macos', 'linux') # Platforms on desktop devices
PLATFORMS_MOBILE = ('ios', 'android') # Platforms on mobile devices

BROWSERS = ('chrome', 'edge', 'firefox', 'safari')
_BROWSERS_TYPE = Literal['chrome', 'edge', 'firefox', 'safari', None]
BROWSERS_SUPPORT_CH = ('chrome', 'edge') # Browsers that support Client Hints
_BROWSERS_SUPPORT_CH_TYPE = Literal['chrome', 'edge', None]
19 changes: 17 additions & 2 deletions src/ua_generator/user_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,26 @@

from . import utils, exceptions
from .client_hints import ClientHints
from .data import DEVICES, BROWSERS, PLATFORMS, PLATFORMS_DESKTOP, PLATFORMS_MOBILE
from .data import (
DEVICES, _DEVICES_TYPE,
BROWSERS, _BROWSERS_TYPE,
PLATFORMS, _PLATFORMS_TYPE,
PLATFORMS_DESKTOP,
PLATFORMS_MOBILE,
)
from .data.generator import Generator
from .headers import Headers
from .options import Options


class UserAgent:
def __init__(self, device=None, platform=None, browser=None, options=None):
def __init__(
self,
device: _DEVICES_TYPE = None,
platform: _PLATFORMS_TYPE = None,
browser: _BROWSERS_TYPE = None,
options: typing.Union[Options, None] = None,
):
self.device: typing.Union[str, None] = utils.choice(device) if device else None
self.platform: typing.Union[str, None] = utils.choice(platform) if platform else None
self.browser: typing.Union[str, None] = utils.choice(browser) if browser else None
Expand Down Expand Up @@ -88,3 +100,6 @@ def __complete(self):

def __str__(self):
return self.text

def __repr__(self) -> str:
return f"UserAgent(\"{self.text}\", device={self.device}, platform={self.platform}, browser={self.browser})"