Utility Functions

Object

class moddb.utils.Object(**kwargs)[source]

A dud objects that will transform every kwarg given into an attribute

get

moddb.utils.get(iterable, **attrs)[source]

A helper that returns the first element in the iterable that meets all the traits passed in attrs. This is an alternative for moddb.utils.find().

When multiple attributes are specified, they are checked using logical AND, not logical OR. Meaning they have to meet every attribute passed in and not one of them.

To have a nested attribute search (i.e. search by x.y) then pass in x__y as the keyword argument.

If nothing is found that matches the attributes passed, then None is returned.

Examples

Basic usage:

article = moddb.utils.get(mod.get_articles(), name='Version 3.5 Released')

Multiple attribute matching:

comment = moddb.utils.get(mod.get_comments(2), content='Test', karma=3)

Nested attribute matching:

comment = moddb.utils.get(article.get_comments(), author__name='SilverElf', content='Best article ever')
Parameters:
  • iterable (Sequence[TypeVar(D)]) – An iterable to search through.

  • **attrs – Keyword arguments that denote attributes to search with.

Return type:

Optional[TypeVar(D)]

find

moddb.utils.find(predicate, seq)[source]

A helper to return the first element found in the sequence that meets the predicate. For example:

comment = find(lambda comment: comment.author.name == 'SilverElf', mod.comments.flatten())

would find the first Comment whose author’s name is ‘SilverElf’ and return it. If no entry is found, then None is returned.

This is different from filter due to the fact it stops the moment it finds a valid entry.

Parameters:
  • predicate – A function that returns a boolean-like result.

  • seq (iterable) – The iterable to search through.

Return type:

Optional[TypeVar(D)]

get_date

moddb.utils.get_date(d)[source]

A helper function that takes a ModDB string representation of time and returns an equivalent datetime.datetime object. This can range from a datetime with the full year to second to just a year and a month.

Parameters:

d (str) – String representation of a datetime

Returns:

The datetime object for the given string

Return type:

datetime.datetime

soup

moddb.utils.soup(html)[source]

Simple helper function that takes a string representation of an html page and returns a beautiful soup object

Parameters:

html (str) – The string representationg of the html to parse

Returns:

The parsed html

Return type:

bs4.BeautifulSoup

get_page

moddb.utils.get_page(url, *, params={}, json=False)[source]

A helper function that takes a url and returns a beautiful soup objects. This is used to center the request making section of the library. Can also be passed a set of paramaters, used for sorting and filtering in the search function.

Parameters:
  • url (str) – The url to get

  • params (dict) – A dictionnary of filters and sorting key-value pairs.

  • json (Optional[bool]) – Whether the expected response is json, in which case it will not be soup’d

Returns:

The parsed html

Return type:

bs4.BeautifulSoup

get_views

moddb.utils.get_views(string)[source]

A helper function that takes a string representation of total something and daily amount of that same thing and returns both as a tuple of ints.

Parameters:

string (str) – The string containing the numbers both total and daily

Returns:

Tuple contains the total views (first element) and the daily views (second element)

Return type:

Tuple[int, int]

join

moddb.utils.join(path)[source]

Joins a partial moddb url with the base url and returns the combined url

Parameters:

path (str) – the url to join

Returns:

The full url.

Return type:

str

normalize

moddb.utils.normalize(string)[source]

Removes all extra fluff from a stat to get the barebone content.

Stats usually have extra words like “members” or “visitors” and have command separated integers.

Parameters:

string (str) – The string to clean up

Returns:

The cleaned up stat

Return type:

str

get_media_type

moddb.utils.get_media_type(img)[source]

Determines whether a media is an image, a video or an audio.

This is somewhat of a brittle method, don’t rely on it too much.

Parameters:

img (bsa.Tag) – The image to check

Returns:

The category of the media

Return type:

MediaCategory

get_page_type

moddb.utils.get_page_type(url)[source]

Get the page type based on a url.

Parameters:

url (str) – The url to get

Returns:

The type of the page

Return type:

ThumbnailType

get_list_stats

moddb.utils.get_list_stats(result_box, per_page=30)[source]

Get the current page, total pages and total results from a result list

Parameters:
  • result_box (bs4.BeautifulSoup) – The HTML of the result box from a list of results page

  • per_page (Optional[int]) – The number of results per page, important for calculations. Defaults to 30, doesn’t usually need to be touched

Returns:

The stats in order of: number of current page (starting from 1), total number of pages (between 1 and X) and the total results.

Return type:

Tuple[int, int, int]