Skip to content

cocorum.jsonhandles

This module provides abstract classes for wrapping JSON data blocks returned by the API in a Python object with attributes / properties. This library does not contain any classes or functions meant to be used directly.

JSON handles

Abstract classes for handling JSON data. S.D.G.

JSONObj

Abstract class for handling a JSON data block as an object

Source code in cocorum/jsonhandles.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class JSONObj():
    """Abstract class for handling a JSON data block as an object"""
    def __init__(self, jsondata):
        """Abstract class for handling a JSON data block as an object.

    Args:
        jsondata (dict): The JSON data block of an API object.
        """

        self._jsondata = jsondata

    def __getitem__(self, key):
        """Get a key from the JSON"""
        return self._jsondata[key]

    @property
    def get(self):
        """Get a key from the JSON with fallback"""
        return self._jsondata.get

get property

Get a key from the JSON with fallback

__getitem__(key)

Get a key from the JSON

Source code in cocorum/jsonhandles.py
21
22
23
def __getitem__(self, key):
    """Get a key from the JSON"""
    return self._jsondata[key]

__init__(jsondata)

Abstract class for handling a JSON data block as an object.

Parameters:

Name Type Description Default
jsondata dict

The JSON data block of an API object.

required
Source code in cocorum/jsonhandles.py
12
13
14
15
16
17
18
19
def __init__(self, jsondata):
    """Abstract class for handling a JSON data block as an object.

Args:
    jsondata (dict): The JSON data block of an API object.
    """

    self._jsondata = jsondata

JSONUserAction

Bases: JSONObj

Abstract class for Rumble JSON user actions

Source code in cocorum/jsonhandles.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class JSONUserAction(JSONObj):
    """Abstract class for Rumble JSON user actions"""
    def __init__(self, jsondata):
        """Abstract class for Rumble JSON user actions.

    Args:
        jsondata (dict): The JSON block for a single Rumble user action.
        """

        JSONObj.__init__(self, jsondata)
        self.__profile_pic = None

    def __eq__(self, other):
        """Is this user equal to another?

    Args:
        other (str, JSONUserAction): Object to compare to.

    Returns:
        Comparison (bool, None): Did it fit the criteria?
        """

        #Check if the compared string is our username, or base 36 user ID if we have one
        if isinstance(other, str):
            #We have a base 36 user ID
            if hasattr(self, "user_id_b36"):
                return other in (self.username, self.user_id_b36)

            #We only have our username
            return self.username == other

        #Check if the compared object has a username and if it matches our own
        if hasattr(other, "username"):
            return self.username == other.username

        #Check if the compared object has a user ID in base 36 and if it matches our own, if we have one
        if hasattr(self, "user_id_b36") and hasattr(other, "user_id_b36"):
            return self.user_id_b36 == other.user_id_b36

    def __str__(self):
        """Follower as a string"""
        return self.username

    @property
    def username(self):
        """The username"""
        return self["username"]

    @property
    def profile_pic_url(self):
        """The user's profile picture URL"""
        return self["profile_pic_url"]

    @property
    def profile_pic(self):
        """The user's profile picture as a bytes string"""
        if not self.profile_pic_url: #The profile picture is blank
            return b''

        if not self.__profile_pic: #We never queried the profile pic before
            response = requests.get(self.profile_pic_url, timeout = static.Delays.request_timeout)
            assert response.status_code == 200, "Status code " + str(response.status_code)

            self.__profile_pic = response.content

        return self.__profile_pic

profile_pic property

The user's profile picture as a bytes string

profile_pic_url property

The user's profile picture URL

username property

The username

__eq__(other)

Is this user equal to another?

Parameters:

Name Type Description Default
other (str, JSONUserAction)

Object to compare to.

required

Returns:

Name Type Description
Comparison (bool, None)

Did it fit the criteria?

Source code in cocorum/jsonhandles.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def __eq__(self, other):
    """Is this user equal to another?

Args:
    other (str, JSONUserAction): Object to compare to.

Returns:
    Comparison (bool, None): Did it fit the criteria?
    """

    #Check if the compared string is our username, or base 36 user ID if we have one
    if isinstance(other, str):
        #We have a base 36 user ID
        if hasattr(self, "user_id_b36"):
            return other in (self.username, self.user_id_b36)

        #We only have our username
        return self.username == other

    #Check if the compared object has a username and if it matches our own
    if hasattr(other, "username"):
        return self.username == other.username

    #Check if the compared object has a user ID in base 36 and if it matches our own, if we have one
    if hasattr(self, "user_id_b36") and hasattr(other, "user_id_b36"):
        return self.user_id_b36 == other.user_id_b36

__init__(jsondata)

Abstract class for Rumble JSON user actions.

Parameters:

Name Type Description Default
jsondata dict

The JSON block for a single Rumble user action.

required
Source code in cocorum/jsonhandles.py
32
33
34
35
36
37
38
39
40
def __init__(self, jsondata):
    """Abstract class for Rumble JSON user actions.

Args:
    jsondata (dict): The JSON block for a single Rumble user action.
    """

    JSONObj.__init__(self, jsondata)
    self.__profile_pic = None

__str__()

Follower as a string

Source code in cocorum/jsonhandles.py
69
70
71
def __str__(self):
    """Follower as a string"""
    return self.username

S.D.G.