Skip to content

Commit 3a65217

Browse files
committed
Initial commit
1 parent b3d4ed1 commit 3a65217

File tree

7 files changed

+618
-58
lines changed

7 files changed

+618
-58
lines changed

src/cpython/kai/__init__.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .exceptions import *
2-
from .types import *
2+
from ._types import *
33

44
__all__ = [
55
"KaiError",
@@ -32,6 +32,7 @@
3232
"M3U8KeyFormatVersions",
3333
"M3U8Key",
3434
"M3U8Segment",
35+
"M3U8Segments",
3536
"M3U8Map",
3637
"M3U8AllowCache",
3738
"M3U8DateRange",
@@ -41,7 +42,9 @@
4142
"M3U8SessionData",
4243
"M3U8Start",
4344
"M3U8Media",
44-
"M3U8VariantStream"
45+
"M3U8VariantStream",
46+
"M3U8Resolution",
47+
"M3U8ByteRange"
4548
]
4649

4750
__locals = locals()

src/cpython/kai/__kai.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
sys.path.append('/storage/emulated/0/Kai/src/cpython')
13
from _kai import (
24
m3u8stream_init,
35
m3u8stream_load,
@@ -59,4 +61,9 @@ def load_file(self, filename, base_url = None):
5961

6062
stream = M3U8Stream()
6163
a=stream.load("/storage/emulated/0/cq3l8ci23aks73akgsug/master.m3u8")
62-
print(a.__dict__)
64+
import sys
65+
for item in a.stream.iter():
66+
pass
67+
#print(repr(item))
68+
print(sys.getrefcount(a.stream))
69+
print(a)

src/cpython/kai/_types/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
M3U8KeyFormatVersions,
1212
M3U8Key,
1313
M3U8Segment,
14+
M3U8Segments,
1415
M3U8Map,
1516
M3U8AllowCache,
1617
M3U8DateRange,
@@ -24,7 +25,8 @@
2425
)
2526

2627
from .m3u8 import (
27-
M3U8Resolution
28+
M3U8Resolution,
29+
M3U8ByteRange
2830
)
2931

3032

@@ -41,6 +43,7 @@
4143
"M3U8KeyFormatVersions",
4244
"M3U8Key",
4345
"M3U8Segment",
46+
"M3U8Segments",
4447
"M3U8Map",
4548
"M3U8AllowCache",
4649
"M3U8DateRange",
@@ -51,5 +54,6 @@
5154
"M3U8Start",
5255
"M3U8Media",
5356
"M3U8VariantStream",
54-
"M3U8Resolution"
57+
"M3U8Resolution",
58+
"M3U8ByteRange"
5559
]

src/cpython/kai/_types/base.py

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
import typing
2+
import inspect
3+
import json
4+
import enum
5+
6+
7+
# https://github.com/pyrogram/pyrogram/blob/v1.1.0/pyrogram/types/object.py#L26
8+
class MetaClass(type, metaclass=type("", (type,), {"__str__": lambda _: "~hi"})):
9+
10+
def __str__(self):
11+
return f"<class 'kai.{self.__name__}'>"
12+
13+
14+
# https://github.com/pyrogram/pyrogram/blob/v1.1.0/pyrogram/types/object.py#L31
15+
class BaseClass(metaclass = MetaClass):
16+
17+
def __str__(self):
18+
return json.dumps(
19+
self,
20+
indent = 4,
21+
default = self._serializer_callback,
22+
ensure_ascii = False
23+
)
24+
25+
26+
class Dict(BaseClass):
27+
28+
def __iter__(self):
29+
iterables = {}
30+
31+
code = self.__init__.__code__
32+
arguments = inspect.getargs(code).args
33+
34+
arguments.remove("self")
35+
36+
for argument in arguments:
37+
attribute = getattr(self, argument)
38+
39+
if isinstance(attribute, List):
40+
iterables.update(
41+
{
42+
argument: list(attribute)
43+
}
44+
)
45+
elif isinstance(attribute, Dict):
46+
iterables.update(
47+
{
48+
argument: dict(attribute)
49+
}
50+
)
51+
elif isinstance(attribute, enum.Enum):
52+
iterables.update(
53+
{
54+
argument: str(attribute)
55+
}
56+
)
57+
else:
58+
iterables.update(
59+
{
60+
argument: attribute
61+
}
62+
)
63+
64+
#print(iterables)
65+
66+
yield from iterables.items()
67+
68+
def __getitem__(self, item):
69+
return getattr(self, item)
70+
71+
def __setitem__(self, key, value):
72+
setattr(self, key, value)
73+
74+
def __delitem__(self, item):
75+
delattr(self, item)
76+
77+
def __repr__(self):
78+
code = self.__init__.__code__
79+
arguments = inspect.getargs(code).args
80+
81+
arguments.remove("self")
82+
83+
data = []
84+
85+
for argument in arguments:
86+
attribute = getattr(self, argument)
87+
88+
if isinstance(attribute, List):
89+
data.append(f"{argument} = {attribute.__class__.__name__}({repr(attribute.base_list)})")
90+
else:
91+
data.append(f"{argument} = {repr(attribute)}")
92+
93+
class_name = self.__class__.__name__
94+
representation = ", ".join(data)
95+
96+
return f"kai.{class_name}({representation})"
97+
98+
@staticmethod
99+
def _serializer_callback(obj):
100+
serialized_object = {}
101+
102+
code = obj.__init__.__code__
103+
arguments = inspect.getargs(code).args
104+
105+
arguments.remove("self")
106+
107+
for argument in arguments:
108+
attribute = getattr(obj, argument)
109+
110+
if isinstance(attribute, List):
111+
serialized_object.update(
112+
{
113+
argument: list(attribute)
114+
}
115+
)
116+
elif isinstance(attribute, enum.Enum):
117+
serialized_object.update(
118+
{
119+
argument: str(attribute)
120+
}
121+
)
122+
else:
123+
serialized_object.update(
124+
{
125+
argument: attribute
126+
}
127+
)
128+
129+
return serialized_object
130+
131+
class List(BaseClass):
132+
133+
def __init__(self, base_list = None):
134+
self.base_list = [] if base_list is None else base_list
135+
136+
def __iter__(self):
137+
iterable = []
138+
139+
for item in self.base_list:
140+
if isinstance(item, Dict):
141+
iterable.append(dict(item))
142+
elif isinstance(item, (str, int, float, bool, type(None))):
143+
iterable.append(item)
144+
else:
145+
raise TypeError
146+
147+
return iter(iterable)
148+
149+
def __getitem__(self, item):
150+
return self.base_list[item]
151+
152+
def __len__(self):
153+
return len(self.base_list)
154+
155+
def append(self, item):
156+
self.base_list.append(item)
157+
158+
def iter(self):
159+
return iter(self.base_list)
160+
161+
def list(self):
162+
return list(self.base_list)
163+
164+
def __repr__(self):
165+
class_name = self.__class__.__name__
166+
representation = repr(self.base_list)
167+
168+
return f"kai.{class_name}({representation})"
169+
170+
@staticmethod
171+
def _serializer_callback(obj):
172+
if isinstance(obj, List):
173+
return getattr(obj, "base_list")
174+
175+
if isinstance(obj, Dict):
176+
return dict(obj)
177+
178+
raise TypeError

src/cpython/kai/_types/m3u8.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ class M3U8Resolution:
33
def __init__(self, width, height):
44
self.width = width
55
self.height = height
6+
7+
class M3U8ByteRange:
8+
9+
def __init__(self, length, offset):
10+
self.length = length
11+
self.offset = offset

0 commit comments

Comments
 (0)