Helper Functions

These are some methods which could be used for more than just one function. This reduces the bloat and makes the overall code less bloated.

ID’s

userbot.helper_funcs.ids.get_entity_from_msg(event: telethon.events.newmessage.NewMessage.Event) → Tuple[Union[None, telethon.tl.types.User], Union[None, bool, str], Union[None, bool, str]]

Get a User entity and/or a reason from the event’s regex pattern

userbot.helper_funcs.ids.get_user_from_msg(event: telethon.events.newmessage.NewMessage.Event) → Union[int, str, None]

Get a user’s ID or username from the event’s regex pattern match

Parser

class userbot.helper_funcs.parser.Parser

Bases: object

Parse UserFull, ChannelFull and ChatFull objects.

static parse_full_chat(chat_obj: Union[telethon.tl.types.ChatFull, telethon.tl.types.ChannelFull], event: telethon.events.newmessage.NewMessage.Event) → str

Human-friendly string of a Chat/Channel obj’s attributes

static parse_full_user(usr_obj: telethon.tl.types.UserFull, event: telethon.events.newmessage.NewMessage.Event) → str

Human-friendly string of an User obj’s attributes

SED

exception userbot.helper_funcs.sed.UnknownFlagError(flag)

Bases: Exception

Used to raise an Exception for an unknown flag.

userbot.helper_funcs.sed.convertCharacterCase(string: str, case: str) → str

Convert the case of a character if found. Used for u and l.

Parameters:
  • string (str) – The string containing the case.
  • case (str) – The raw string of the case.
Returns:

str | None – The replaced string on success, None otherwise.

userbot.helper_funcs.sed.convertStringCase(string: str, case: str) → str

Convert the matched string in the necessary case. Used for U and L.

Parameters:
  • string (str) – The string containing the case.
  • case (str) – The case to search for.
Returns:

str | None – The replaced string on success, None otherwise.

userbot.helper_funcs.sed.convertWordCase(string: str, case: str) → str

Convert the matched words in the necessary case. Used for F and I.

Parameters:
  • string (str) – The string containing the case.
  • case (str) – The case to search for.
Returns:

str | None – The replaced string on success, None otherwise.

userbot.helper_funcs.sed.match_splitter(match: re.Match) → Tuple[str, str, str, str]

Splits an re.Match to get the required attributes for substitution. Unescapes the slashes as well because this is Python.

Parameters:match (Match) – Match object to split.
Returns:(str, str, str, str) – A tuple of strings containing line, regexp, replacement and flags respectively.
userbot.helper_funcs.sed.resolve_flags(fl: str) → Tuple[int, Union[int, enum.Enum]]

Split all flags from the string for substituion.

Parameters:fl (str) – String containing all the flags.
Raises:UnknownFlagError – If there’s an unknown flag, then this is raised to stop any farther execution.
Returns:(int, (int | enum.Enum)) – Count and all the other re flags as an Enum type, if any.
userbot.helper_funcs.sed.sub_matches(matches: list, original: str) → Optional[str]

Iterate over all the matches whilst substituing the string.

Parameters:
  • matches (list) – A list of Match objects.
  • original (str) – The original string to use for substituion.
Returns:

str | None – The final substitued string on success, None otherwise.

userbot.helper_funcs.sed.substitute(fr: str, to: str, original: str, line: (<class 'str'>, <class 'int'>, None) = None, count: int = 1, flags: Union[enum.Enum, int] = 0) → Optional[str]

Substitute a (specific) string. Match the regular-expression against the content of the pattern space. If found, replace matched string with replacement.

Parameters:
  • fr (str) – The regexp string.
  • to (str) – The replacement string.
  • original (str) – Original string to use for substituion.
  • line (str | int | None, optional) – Line to use for substitution. Defaults to None.
  • count (int, optional) – The amount of repetitions to do. Defaults to 1.
  • flags (int | enum.Enum, optional) – Flags to use. Defaults to 0.
Returns:

str | None – The replaced string on success, None otherwise.

Time

userbot.helper_funcs.time.amount_to_secs(amount: tuple) → int

Resolves one unit to total seconds.

Parameters:amount (int, str) – Tuple where str is the unit.
Returns:int – Total seconds of the unit on success.

Example

>>> await amount_to_secs(("1", "m"))
60
userbot.helper_funcs.time.split_extra_string(string: str) → Tuple[Optional[str], Optional[int]]
userbot.helper_funcs.time.string_to_secs(string: str) → int

Converts a time string to total seconds.

Parameters:string (str) – String conatining the time.
Returns:int – Total seconds of all the units.

Example

>>> await string_to_sec("6h20m")
22800

YoutubeDL

class userbot.helper_funcs.yt_dl.ProgressHook(event, update=5)

Bases: object

Custom hook with the event stored for YTDL.

callback(task)

Cancel pending tasks else skip them if completed.

edit(*args, **kwargs)

Create a Task of the progress edit.

hook(d: dict) → None

YoutubeDL’s hook which logs progress and errors to UserBot logger.

class userbot.helper_funcs.yt_dl.YTdlLogger

Bases: object

Logger used for YoutubeDL which logs to UserBot logger.

critical(msg: str) → None

Logs critical messages with youtube-dl tag to UserBot logger.

debug(msg: str) → None

Logs debug messages with youtube-dl tag to UserBot logger.

error(msg: str) → None

Logs error messages with youtube-dl tag to UserBot logger.

warning(msg: str) → None

Logs warning messages with youtube-dl tag to UserBot logger.

userbot.helper_funcs.yt_dl.extract_info(loop, executor: concurrent.futures._base.Executor, ydl_opts: dict, url: str, download: bool = False) → str

Runs YoutubeDL’s extract_info method without blocking the event loop.

Parameters:
  • executor (concurrent.futures.Executor) – Either ThreadPoolExecutor or ProcessPoolExecutor.
  • params (dict) – Parameters/Keyword arguments to use for YoutubeDL.
  • url (str) – The url which you want to use for extracting info.
  • download (bool, optional) – If you want to download the video. Defaults to False.
Returns:

str – Successfull string or info_dict on success or an exception’s string if any occur.

userbot.helper_funcs.yt_dl.list_formats(info_dict: dict) → str

YoutubeDL’s list_formats method but without format notes.

Parameters:info_dict (dict) – Dictionary which is returned by YoutubeDL’s extract_info method.
Returns:str – All available formats in order as a string instead of stdout.