Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit e2e5f6d

Browse files
authored
Merge pull request #2 from junah201/refactor/typehint
Refactor/typehint
2 parents f175f95 + 197724b commit e2e5f6d

8 files changed

+351
-305
lines changed

README.md

+26-14
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,36 @@
1212
pip install twip-api
1313
```
1414

15-
## ✍️ 사용 예시
15+
## ✍️ Example
1616

1717
```py
18-
import twip
18+
from twip import Twip, Donate, Follow, Subscribe, Hosting, Cheer, Sound, Slotmachine
1919

20-
Twip = twip.Twip()
20+
class MyTwip(Twip):
21+
def on_ready(self):
22+
print("Twip is ready!")
2123

22-
@Twip.event
23-
def on_donate(ctx):
24-
print(f"id : {ctx.id}")
25-
print(f"nickname : {ctx.nickname}")
26-
print(f"amount : {ctx.amount}")
27-
print(f"comment : {ctx.comment}")
24+
def on_donate(self, donate: Donate):
25+
print(donate)
2826

29-
Twip.run("your alert box id", "your twip api token"")
27+
def on_follow(self, follow: Follow):
28+
print(follow)
29+
30+
def on_subscribe(self, subscribe: Subscribe):
31+
print(subscribe)
32+
33+
def on_hosting(self, hosting: Hosting):
34+
print(hosting)
35+
36+
def on_cheer(self, cheer: Cheer):
37+
print(cheer)
38+
39+
def on_sound(self, sound: Sound):
40+
print(sound)
41+
42+
if __name__ == "__main__":
43+
myTwip = MyTwip()
44+
myTwip.run("your alert box id", "your twip api token")
3045
```
3146

3247
더 많은 예제는 Github [example.py](https://github.com/junah201/Twip/blob/main/twip/example.py) 에서 확인하세요.
@@ -49,10 +64,7 @@ Twip.run("your alert box id", "your twip api token"")
4964
- **[0.0.8.2](https://pypi.org/project/twip-api/0.0.8.2/)** : 함수 이름 관련 버그 수정
5065
- **[0.0.9](https://pypi.org/project/twip-api/0.0.9/)** : on_ready 이벤트 추가
5166
- **[0.0.9.1](https://pypi.org/project/twip-api/0.0.9.1/)** : 크롤링된 토큰이 유효기간이 지난 후에도 계속해서 사용되던 버그 수정
52-
53-
## ✔️ 업데이트 예정
54-
55-
- 비동기 설정 추가
67+
- **[1.0.0](https://pypi.org/project/twip-api/1.0.0/)** : 타입 힌트 추가 및 구조 개편
5668

5769
## 🕮 License
5870

requirments.txt requirements.txt

File renamed without changes.

setup.py

+18-19
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
from setuptools import setup, find_packages
2-
from pathlib import Path
32

4-
README = open("README.md", 'r', encoding = "utf-8").read()
3+
README = open("README.md", 'r', encoding="utf-8").read()
54

65
setup(
7-
name = 'twip-api',
8-
version = '0.0.9.1',
9-
description = 'Parses the things provided by twip such as donation, follow',
10-
long_description = README,
11-
long_description_content_type = 'text/markdown',
12-
author = 'Junah201',
13-
author_email = 'junah.dev@gmail.com',
14-
url = 'https://github.com/junah201/Twip',
15-
install_requires = ["websocket-client", "requests"],
16-
packages = find_packages(exclude = []),
17-
keywords = ['twip', 'twitch'],
18-
python_requires = '>=3',
19-
license = 'MIT',
20-
package_data = {},
21-
zip_safe = False,
22-
classifiers = [
6+
name='twip-api',
7+
version='1.0.0',
8+
description='Parses the things provided by twip such as donation, follow',
9+
long_description=README,
10+
long_description_content_type='text/markdown',
11+
author='Junah201',
12+
author_email='junah.dev@gmail.com',
13+
url='https://github.com/junah201/Twip',
14+
install_requires=["websocket-client", "requests"],
15+
packages=find_packages(exclude=[]),
16+
keywords=['twip', 'twip.kr', 'twitch'],
17+
python_requires='>=3',
18+
license='MIT',
19+
package_data={},
20+
zip_safe=False,
21+
classifiers=[
2322
'Programming Language :: Python :: 3.7',
2423
'Programming Language :: Python :: 3.8',
2524
'Programming Language :: Python :: 3.9',
2625
'Programming Language :: Python :: 3.10',
2726
],
28-
)
27+
)

twip/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
name = "twip"
1+
from .twip import *
22

3-
from .twip import *
3+
name = "twip"

twip/data_convert.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from models import Donate, Follow, Subscribe, Hosting, Cheer, Sound, Slotmachine
2+
3+
4+
def convert_donate(data: list) -> Donate:
5+
data_type = data[0]
6+
data_value = data[1]
7+
donate = Donate(
8+
type=data_type,
9+
id=data_value.get("_id"),
10+
nickname=data_value.get("nickname"),
11+
amount=data_value.get("amount"),
12+
comment=data_value.get("comment"),
13+
watcher_id=data_value.get("watcher_id"),
14+
subbed=data_value.get("subbed"),
15+
repeat=data_value.get("repeat"),
16+
tts_type=data_value.get("ttstype"),
17+
tts_url=data_value.get("ttsurl"),
18+
effect=data_value.get("effect"),
19+
variation_id=data_value.get("variation_id"),
20+
slotmachine=Slotmachine()
21+
)
22+
23+
if data_value.get("slotmachine_data"):
24+
slotmachine_data = data_value.get("slotmachine_data")
25+
donate.slotmachine = Slotmachine(
26+
items=slotmachine_data.get("items"),
27+
result=slotmachine_data.get("gotcha"),
28+
sound=slotmachine_data.get("config").get("sound"),
29+
point=slotmachine_data.get("config").get("point"),
30+
duration=slotmachine_data.get("config").get("duration")
31+
)
32+
33+
return donate
34+
35+
36+
def convert_follow(data: list) -> Follow:
37+
data_type = data[0]
38+
data_value = data[1]
39+
follow = Follow(
40+
nickname=data_value.get("nickname"),
41+
repeat=data_value.get("repeat"),
42+
variation_id=data_value.get("variation_id")
43+
)
44+
45+
return follow
46+
47+
48+
def convert_subscribe(data: list) -> Subscribe:
49+
data_type = data[0]
50+
data_value = data[1]
51+
subscribe = Subscribe(
52+
username=data_value.get("username"),
53+
months=data_value.get("months"),
54+
message=data_value.get("message"),
55+
method=data_value.get("method"),
56+
repeat=data_value.get("repeat"),
57+
variation_id=data_value.get("variation_id")
58+
)
59+
60+
return subscribe
61+
62+
63+
def convert_hosting(data: list) -> Hosting:
64+
data_value = data[1]
65+
hosting = Hosting(
66+
username=data_value.get("username"),
67+
viewers=data_value.get("viewers"),
68+
repeat=data_value.get("repeat"),
69+
variation_id=data_value.get("variation_id")
70+
)
71+
72+
return hosting
73+
74+
75+
def convert_cheer(data: list) -> Cheer:
76+
data_type = data[0]
77+
data_value = data[1]
78+
cheer = Cheer(
79+
nickname=data_value.get("nickname"),
80+
amount=data_value.get("amount"),
81+
comment=data_value.get("comment"),
82+
repeat=data_value.get("repeat"),
83+
variation_id=data_value.get("variation_id")
84+
)
85+
86+
return cheer
87+
88+
89+
def convert_sound(data: list) -> Sound:
90+
data_type = data[0]
91+
if len(data) == 2:
92+
data_value = data[1]
93+
sound = Sound(
94+
type=data_type[6:],
95+
volume=data_value.get("volume"),
96+
url=data_value.get("url")
97+
)
98+
else:
99+
sound = Sound(
100+
type=data_type[6:],
101+
volume=0,
102+
url=""
103+
)
104+
105+
return sound

twip/example.py

+23-65
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,29 @@
1-
import twip
1+
from twip import Twip, Donate, Follow, Subscribe, Hosting, Cheer, Sound, Slotmachine
22

3-
Twip = twip.Twip()
43

5-
@Twip.event
6-
def on_ready():
7-
print("Twip is ready!")
4+
class MyTwip(Twip):
5+
def on_ready(self):
6+
print("Twip is ready!")
87

9-
@Twip.event
10-
def on_donate(ctx):
11-
print("===on_donate===")
12-
print(f"id : {ctx.id}")
13-
print(f"nickname : {ctx.nickname}")
14-
print(f"amount : {ctx.amount}")
15-
print(f"comment : {ctx.comment}")
16-
print(f"watcher_id : {ctx.watcher_id}")
17-
print(f"subbed : {ctx.subbed}")
18-
print(f"repeat : {ctx.repeat}")
19-
print(f"ttstype : {ctx.tts_type}")
20-
print(f"ttsurl : {ctx.tts_url}")
21-
print(f"slotmachine.items : {ctx.slotmachine.items}")
22-
print(f"slotmachine.result : {ctx.slotmachine.result}")
23-
print(f"slotmachine.reward_id : {ctx.slotmachine.reward_id}")
24-
print(f"slotmachine.sound : {ctx.slotmachine.sound}")
25-
print(f"slotmachine.point : {ctx.slotmachine.point}")
26-
print(f"slotmachine.duration : {ctx.slotmachine.duration}")
27-
print(f"effect : {ctx.effect}")
28-
print(f"variation_id : {ctx.variation_id}")
8+
def on_donate(self, donate: Donate):
9+
print(donate)
2910

30-
@Twip.event
31-
def on_subscribe(ctx):
32-
print("===on_subscribe===")
33-
print(f"username : {ctx.username}")
34-
print(f"months : {ctx.months}")
35-
print(f"message : {ctx.message}")
36-
print(f"method : {ctx.method}")
37-
print(f"repeat : {ctx.repeat}")
38-
print(f"variation_id : {ctx.variation_id}")
39-
40-
@Twip.event
41-
def on_hosting(ctx):
42-
print("===on_hosting===")
43-
print(f"username : {ctx.username}")
44-
print(f"viewers : {ctx.viewers}")
45-
print(f"repeat : {ctx.repeat}")
46-
print(f"variation_id : {ctx.variation_id}")
47-
48-
@Twip.event
49-
def on_cheer(ctx):
50-
print("===on_cheer===")
51-
print(f"nickname : {ctx.nickname}")
52-
print(f"amount : {ctx.amount}")
53-
print(f"comment : {ctx.comment}")
54-
print(f"repeat : {ctx.repeat}")
55-
print(f"variation_id : {ctx.variation_id}")
56-
57-
@Twip.event
58-
def on_follow(ctx):
59-
print("===on_follow===")
60-
print(f"nickname : {ctx.nickname}")
61-
print(f"repeat : {ctx.repeat}")
62-
print(f"variation_id : {ctx.variation_id}")
11+
def on_follow(self, follow: Follow):
12+
print(follow)
6313

64-
@Twip.event
65-
def on_sound(ctx):
66-
print("===on_sound===")
67-
print(f"type : {ctx.type}")
68-
print(f"url : {ctx.url}")
69-
print(f"volume : {ctx.volume}")
14+
def on_subscribe(self, subscribe: Subscribe):
15+
print(subscribe)
7016

71-
Twip.run("your alert box id", "your twip api token")
17+
def on_hosting(self, hosting: Hosting):
18+
print(hosting)
19+
20+
def on_cheer(self, cheer: Cheer):
21+
print(cheer)
22+
23+
def on_sound(self, sound: Sound):
24+
print(sound)
25+
26+
27+
if __name__ == "__main__":
28+
myTwip = MyTwip()
29+
myTwip.run("your alert box id", "your twip api token")

0 commit comments

Comments
 (0)