Skip to content

groups

Group interfaces for analytix.

You should never need to create any of these yourself.

Group dataclass

Bases: _Resource

A group.

Parameters:

Name Type Description Default
kind str

The kind of resource this is. This will always be "youtube#group".

required
etag Optional[str]

The Etag of this resource.

required
id str

The ID that YouTube uses to uniquely identify the group.

required
published_at datetime

The date and time that the group was created.

required
title str

The group name.

required
item_count int

The number of items in the group.

required
item_type str

The type of resources that the group contains.

required
shard Shard

The shard instance used to fetch this group.

required
Source code in analytix/groups/groups.py
@dataclass(frozen=True)
class Group(_Resource):
    """A group.

    Parameters
    ----------
    kind
        The kind of resource this is. This will always be
        "youtube#group".
    etag
        The Etag of this resource.
    id
        The ID that YouTube uses to uniquely identify the group.
    published_at
        The date and time that the group was created.
    title
        The group name.
    item_count
        The number of items in the group.
    item_type
        The type of resources that the group contains.
    shard
        The shard instance used to fetch this group.
    """

    __slots__ = ("id", "published_at", "title", "item_count", "item_type", "shard")

    id: str
    published_at: dt.datetime
    title: str
    item_count: int
    item_type: str
    shard: "Shard"

    @classmethod
    def from_json(cls, shard: "Shard", data: Dict[str, Any]) -> "Group":
        """Create a new `Group` instance from JSON data.

        ???+ note "Changed in version 5.0"
            This now takes the shard instance used to fetch the data.

        Parameters
        ----------
        shard
            The shard instance used to fetch the data.
        data
            The raw JSON data from the API.

        Returns
        -------
        Group
            The newly created instance.
        """
        return cls(
            data["kind"],
            data["etag"],
            data["id"],
            dt.datetime.fromisoformat(
                data["snippet"]["publishedAt"].replace("Z", "+00:00"),
            ),
            data["snippet"]["title"],
            int(data["contentDetails"]["itemCount"]),
            data["contentDetails"]["itemType"],
            shard,
        )

    @property
    def data(self) -> Dict[str, Any]:
        """The raw data for this group in JSON format.

        Returns
        -------
        Dict[str, Any]
            The response data.
        """
        return {
            "kind": self.kind,
            "etag": self.etag,
            "id": self.id,
            "snippet": {
                "publishedAt": (
                    self.published_at.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
                ),
                "title": self.title,
            },
            "contentDetails": {
                "itemCount": str(self.item_count),
                "itemType": self.item_type,
            },
        }

    def fetch_items(self) -> "GroupItemList":
        """Fetch a list of all items within this group.

        !!! note "New in version 5.0"

        Returns
        -------
        GroupItemList
            An object containing the list of group items and the next
            page token.

        Raises
        ------
        BadRequest
            Your request was invalid.
        Unauthorised
            Your access token is invalid.
        Forbidden
            You tried to access data you're not allowed to access. If
            your channel is not partnered, this is raised when you try
            to access monetary data.
        """
        return self.shard.fetch_group_items(self.id)

data property

data: Dict[str, Any]

The raw data for this group in JSON format.

Returns:

Type Description
Dict[str, Any]

The response data.

fetch_items

fetch_items() -> GroupItemList

Fetch a list of all items within this group.

New in version 5.0

Returns:

Type Description
GroupItemList

An object containing the list of group items and the next page token.

Raises:

Type Description
BadRequest

Your request was invalid.

Unauthorised

Your access token is invalid.

Forbidden

You tried to access data you're not allowed to access. If your channel is not partnered, this is raised when you try to access monetary data.

Source code in analytix/groups/groups.py
def fetch_items(self) -> "GroupItemList":
    """Fetch a list of all items within this group.

    !!! note "New in version 5.0"

    Returns
    -------
    GroupItemList
        An object containing the list of group items and the next
        page token.

    Raises
    ------
    BadRequest
        Your request was invalid.
    Unauthorised
        Your access token is invalid.
    Forbidden
        You tried to access data you're not allowed to access. If
        your channel is not partnered, this is raised when you try
        to access monetary data.
    """
    return self.shard.fetch_group_items(self.id)

from_json classmethod

from_json(shard: Shard, data: Dict[str, Any]) -> Group

Create a new Group instance from JSON data.

Changed in version 5.0

This now takes the shard instance used to fetch the data.

Parameters:

Name Type Description Default
shard Shard

The shard instance used to fetch the data.

required
data Dict[str, Any]

The raw JSON data from the API.

required

Returns:

Type Description
Group

The newly created instance.

Source code in analytix/groups/groups.py
@classmethod
def from_json(cls, shard: "Shard", data: Dict[str, Any]) -> "Group":
    """Create a new `Group` instance from JSON data.

    ???+ note "Changed in version 5.0"
        This now takes the shard instance used to fetch the data.

    Parameters
    ----------
    shard
        The shard instance used to fetch the data.
    data
        The raw JSON data from the API.

    Returns
    -------
    Group
        The newly created instance.
    """
    return cls(
        data["kind"],
        data["etag"],
        data["id"],
        dt.datetime.fromisoformat(
            data["snippet"]["publishedAt"].replace("Z", "+00:00"),
        ),
        data["snippet"]["title"],
        int(data["contentDetails"]["itemCount"]),
        data["contentDetails"]["itemType"],
        shard,
    )

GroupItem dataclass

Bases: _Resource

A group item.

Parameters:

Name Type Description Default
kind str

The kind of resource this is. This will always be "youtube#groupItem".

required
etag Optional[str]

The Etag of this resource.

required
id str

The ID that YouTube uses to uniquely identify the channel, video, playlist, or asset resource that is included in the group.

required
resource GroupItemResource

The resource object contains information that identifies the item being added to the group.

required
Notes

The id parameter does NOT refer to the actual ID of the channel, video, playlist, or asset, but instead an ID related to its inclusion within the group.

To get the actual ID of the resource, use resource.id.

Source code in analytix/groups/groups.py
@dataclass(frozen=True)
class GroupItem(_Resource):
    """A group item.

    Parameters
    ----------
    kind
        The kind of resource this is. This will always be
        "youtube#groupItem".
    etag
        The Etag of this resource.
    id
        The ID that YouTube uses to uniquely identify the channel,
        video, playlist, or asset resource that is included in the
        group.
    resource
        The resource object contains information that identifies the
        item being added to the group.

    Notes
    -----
    The `id` parameter does NOT refer to the actual ID of the channel,
    video, playlist, or asset, but instead an ID related to its
    inclusion within the group.

    To get the actual ID of the resource, use `resource.id`.
    """

    __slots__ = ("id", "group_id", "resource")

    id: str
    group_id: str
    resource: "GroupItemResource"

    @classmethod
    def from_json(cls, data: Dict[str, Any]) -> "GroupItem":
        """Create a new `GroupItem` instance from JSON data.

        Parameters
        ----------
        data
            The raw JSON data from the API.

        Returns
        -------
        GroupItem
            The newly created instance.
        """
        return cls(
            data["kind"],
            data["etag"],
            data["id"],
            data["groupId"],
            GroupItemResource(
                data["resource"]["kind"],
                data["resource"]["id"],
            ),
        )

    @property
    def data(self) -> Dict[str, Any]:
        """The raw data for this group in JSON format.

        Returns
        -------
        Dict[str, Any]
            The response data.
        """
        return {
            "kind": self.kind,
            "etag": self.etag,
            "id": self.id,
            "groupId": self.group_id,
            "resource": {
                "kind": self.resource.kind,
                "id": self.resource.id,
            },
        }

data property

data: Dict[str, Any]

The raw data for this group in JSON format.

Returns:

Type Description
Dict[str, Any]

The response data.

from_json classmethod

from_json(data: Dict[str, Any]) -> GroupItem

Create a new GroupItem instance from JSON data.

Parameters:

Name Type Description Default
data Dict[str, Any]

The raw JSON data from the API.

required

Returns:

Type Description
GroupItem

The newly created instance.

Source code in analytix/groups/groups.py
@classmethod
def from_json(cls, data: Dict[str, Any]) -> "GroupItem":
    """Create a new `GroupItem` instance from JSON data.

    Parameters
    ----------
    data
        The raw JSON data from the API.

    Returns
    -------
    GroupItem
        The newly created instance.
    """
    return cls(
        data["kind"],
        data["etag"],
        data["id"],
        data["groupId"],
        GroupItemResource(
            data["resource"]["kind"],
            data["resource"]["id"],
        ),
    )

GroupItemList dataclass

Bases: _Resource

A list of group items.

Parameters:

Name Type Description Default
kind str

The kind of resource this is. This will always be "youtube#groupListResponse".

required
etag Optional[str]

The Etag of this resource.

required
items List[GroupItem]

A list of items that the group contains. Each item in the list represents a groupItem resource.

required
Source code in analytix/groups/groups.py
@dataclass(frozen=True)
class GroupItemList(_Resource):
    """A list of group items.

    Parameters
    ----------
    kind
        The kind of resource this is. This will always be
        "youtube#groupListResponse".
    etag
        The Etag of this resource.
    items
        A list of items that the group contains. Each item in the list
        represents a groupItem resource.
    """

    __slots__ = "items"

    items: List["GroupItem"]

    def __getitem__(self, key: int) -> "GroupItem":
        return self.items[key]

    def __iter__(self) -> Iterator["GroupItem"]:
        return iter(self.items)

    @classmethod
    def from_json(cls, data: Dict[str, Any]) -> "GroupItemList":
        """Create a new `GroupItemList` instance from JSON data.

        Parameters
        ----------
        data
            The raw JSON data from the API.

        Returns
        -------
        GroupItemList
            The newly created instance.
        """
        return cls(
            data["kind"],
            data["etag"],
            [GroupItem.from_json(item) for item in data["items"]],
        )

    @property
    def data(self) -> Dict[str, Any]:
        """The raw data for this group in JSON format.

        Returns
        -------
        Dict[str, Any]
            The response data.
        """
        return {
            "kind": self.kind,
            "etag": self.etag,
            "items": [g_item.data for g_item in self.items],
        }

data property

data: Dict[str, Any]

The raw data for this group in JSON format.

Returns:

Type Description
Dict[str, Any]

The response data.

from_json classmethod

from_json(data: Dict[str, Any]) -> GroupItemList

Create a new GroupItemList instance from JSON data.

Parameters:

Name Type Description Default
data Dict[str, Any]

The raw JSON data from the API.

required

Returns:

Type Description
GroupItemList

The newly created instance.

Source code in analytix/groups/groups.py
@classmethod
def from_json(cls, data: Dict[str, Any]) -> "GroupItemList":
    """Create a new `GroupItemList` instance from JSON data.

    Parameters
    ----------
    data
        The raw JSON data from the API.

    Returns
    -------
    GroupItemList
        The newly created instance.
    """
    return cls(
        data["kind"],
        data["etag"],
        [GroupItem.from_json(item) for item in data["items"]],
    )

GroupItemResource dataclass

A group item resource.

Parameters:

Name Type Description Default
kind str

Identifies the type of resource being added to the group.

required
id str

The channel, video, playlist, or asset ID that YouTube uses to uniquely identify the item that is being added to the group.

required
Source code in analytix/groups/groups.py
@dataclass(frozen=True)
class GroupItemResource:
    """A group item resource.

    Parameters
    ----------
    kind
        Identifies the type of resource being added to the group.
    id
        The channel, video, playlist, or asset ID that YouTube uses to
        uniquely identify the item that is being added to the group.
    """

    __slots__ = ("kind", "id")

    kind: str
    id: str

GroupList dataclass

Bases: _Resource

A list of groups.

Parameters:

Name Type Description Default
kind str

The kind of resource this is. This will always be "youtube#groupListResponse".

required
etag Optional[str]

The Etag of this resource.

required
items List[Group]

A list of groups that match the API request parameters. Each item in the list represents a group resource.

required
next_page_token Optional[str]

The token that can be used as the value of the pageToken parameter to retrieve the next page in the result set.

required
Source code in analytix/groups/groups.py
@dataclass(frozen=True)
class GroupList(_Resource):
    """A list of groups.

    Parameters
    ----------
    kind
        The kind of resource this is. This will always be
        "youtube#groupListResponse".
    etag
        The Etag of this resource.
    items
        A list of groups that match the API request parameters. Each
        item in the list represents a group resource.
    next_page_token
        The token that can be used as the value of the `pageToken`
        parameter to retrieve the next page in the result set.
    """

    __slots__ = ("items", "next_page_token")

    items: List["Group"]
    next_page_token: Optional[str]

    def __getitem__(self, key: int) -> "Group":
        return self.items[key]

    def __iter__(self) -> Iterator["Group"]:
        return iter(self.items)

    @classmethod
    def from_json(cls, shard: "Shard", data: Dict[str, Any]) -> "GroupList":
        """Create a new `GroupList` instance from JSON data.

        ???+ note "Changed in version 5.0"
            * This now takes the shard instance used to fetch the data
            * This will no longer raise an error if a channel has no
              groups

        Parameters
        ----------
        shard
            The shard instance used to fetch the data.
        data
            The raw JSON data from the API.

        Returns
        -------
        GroupList
            The newly created instance.
        """
        return cls(
            data["kind"],
            data.get("etag"),
            [Group.from_json(shard, item) for item in data["items"]],
            data.get("nextPageToken"),
        )

    @property
    def data(self) -> Dict[str, Any]:
        """The raw data for this group in JSON format.

        Returns
        -------
        Dict[str, Any]
            The response data.
        """
        return {
            "kind": self.kind,
            "etag": self.etag,
            "items": [group.data for group in self.items],
            "nextPageToken": self.next_page_token,
        }

data property

data: Dict[str, Any]

The raw data for this group in JSON format.

Returns:

Type Description
Dict[str, Any]

The response data.

from_json classmethod

from_json(shard: Shard, data: Dict[str, Any]) -> GroupList

Create a new GroupList instance from JSON data.

Changed in version 5.0
  • This now takes the shard instance used to fetch the data
  • This will no longer raise an error if a channel has no groups

Parameters:

Name Type Description Default
shard Shard

The shard instance used to fetch the data.

required
data Dict[str, Any]

The raw JSON data from the API.

required

Returns:

Type Description
GroupList

The newly created instance.

Source code in analytix/groups/groups.py
@classmethod
def from_json(cls, shard: "Shard", data: Dict[str, Any]) -> "GroupList":
    """Create a new `GroupList` instance from JSON data.

    ???+ note "Changed in version 5.0"
        * This now takes the shard instance used to fetch the data
        * This will no longer raise an error if a channel has no
          groups

    Parameters
    ----------
    shard
        The shard instance used to fetch the data.
    data
        The raw JSON data from the API.

    Returns
    -------
    GroupList
        The newly created instance.
    """
    return cls(
        data["kind"],
        data.get("etag"),
        [Group.from_json(shard, item) for item in data["items"]],
        data.get("nextPageToken"),
    )