aminoacid.util

 1from base64 import b64encode
 2from datetime import datetime
 3from hashlib import sha1
 4from hmac import HMAC
 5
 6from .commands import *
 7
 8__version__ = "0.1.5"
 9
10str_to_ts = lambda _str: int(datetime.strptime(_str, "%Y-%m-%dT%H:%M:%SZ").timestamp())
11"""Convert an Amino Timestamp to a UNIX timestamp
12
13Parameters
14----------
15_str : str
16    The string to convert
17
18Returns
19----------
20int
21    The UNIX timestamp of the given Amino Timestamp
22"""
23
24
25def parse_topic(topic_str: str) -> dict:
26    """Parses a topic string (e.g. "ndtopic:x1:users-start-typing-at:00000000-0000-0000-0000-000000000000")
27
28    Parameters
29    ----------
30    topic_str : str
31        The topic string to parse, this string is split into fields separated by ":"
32
33    Returns
34    -------
35    dict
36        Dictionary containing the scope, topic and extras given in the string
37    """
38    return {
39        key: field
40        for field, key in zip(topic_str.split(":")[1:], ["scope", "topic", "extras"])
41    }
42
43
44def get_headers(
45    data: bytes = b"", device: str = "", key: bytes = b"", v: bytes = b""
46) -> dict:
47    """Generate request headers
48
49    Parameters
50    ----------
51    data : bytes, optional
52        The request payload, by default b""
53    device : str, optional
54        deviceId to be sent, by default ""
55    key : bytes, optional
56        the key to be used in the request signature, this key has to be supplied by the user, by default b""
57    v : bytes, optional
58        version of the request signature, this is used as the prefix for the signature, by default b""
59
60    Returns
61    -------
62    dict
63        Returns the Headers
64    """
65    head = {
66        "NDCDEVICEID": device,
67        "Accept-Language": "en-US",
68        "User-Agent": f"AminoAcid/{__version__} (+https://github.com/okok7711/AminoAcid)",
69        "Host": "service.narvii.com",
70        "Accept-Encoding": "gzip",
71        "Connection": "Keep-Alive",
72    }
73    if data:
74        head["NDC-MSG-SIG"] = b64encode(v + HMAC(key, data, sha1).digest()).decode(
75            "utf-8"
76        )
77
78    return head
def str_to_ts(_str):
11str_to_ts = lambda _str: int(datetime.strptime(_str, "%Y-%m-%dT%H:%M:%SZ").timestamp())

Convert an Amino Timestamp to a UNIX timestamp

Parameters
  • _str (str): The string to convert
Returns
  • int: The UNIX timestamp of the given Amino Timestamp
def parse_topic(topic_str: str) -> dict:
26def parse_topic(topic_str: str) -> dict:
27    """Parses a topic string (e.g. "ndtopic:x1:users-start-typing-at:00000000-0000-0000-0000-000000000000")
28
29    Parameters
30    ----------
31    topic_str : str
32        The topic string to parse, this string is split into fields separated by ":"
33
34    Returns
35    -------
36    dict
37        Dictionary containing the scope, topic and extras given in the string
38    """
39    return {
40        key: field
41        for field, key in zip(topic_str.split(":")[1:], ["scope", "topic", "extras"])
42    }

Parses a topic string (e.g. "ndtopic:x1:users-start-typing-at:00000000-0000-0000-0000-000000000000")

Parameters
  • topic_str (str): The topic string to parse, this string is split into fields separated by ":"
Returns
  • dict: Dictionary containing the scope, topic and extras given in the string
def get_headers( data: bytes = b'', device: str = '', key: bytes = b'', v: bytes = b'') -> dict:
45def get_headers(
46    data: bytes = b"", device: str = "", key: bytes = b"", v: bytes = b""
47) -> dict:
48    """Generate request headers
49
50    Parameters
51    ----------
52    data : bytes, optional
53        The request payload, by default b""
54    device : str, optional
55        deviceId to be sent, by default ""
56    key : bytes, optional
57        the key to be used in the request signature, this key has to be supplied by the user, by default b""
58    v : bytes, optional
59        version of the request signature, this is used as the prefix for the signature, by default b""
60
61    Returns
62    -------
63    dict
64        Returns the Headers
65    """
66    head = {
67        "NDCDEVICEID": device,
68        "Accept-Language": "en-US",
69        "User-Agent": f"AminoAcid/{__version__} (+https://github.com/okok7711/AminoAcid)",
70        "Host": "service.narvii.com",
71        "Accept-Encoding": "gzip",
72        "Connection": "Keep-Alive",
73    }
74    if data:
75        head["NDC-MSG-SIG"] = b64encode(v + HMAC(key, data, sha1).digest()).decode(
76            "utf-8"
77        )
78
79    return head

Generate request headers

Parameters
  • data (bytes, optional): The request payload, by default b""
  • device (str, optional): deviceId to be sent, by default ""
  • key (bytes, optional): the key to be used in the request signature, this key has to be supplied by the user, by default b""
  • v (bytes, optional): version of the request signature, this is used as the prefix for the signature, by default b""
Returns
  • dict: Returns the Headers