Skip to content

Commit c6ff8e9

Browse files
committed
finalize ad actoins
1 parent c66f004 commit c6ff8e9

9 files changed

Lines changed: 143 additions & 25 deletions

File tree

actions/ad_schedule.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import threading
2+
import time
3+
import os
4+
5+
from loguru import logger as log
6+
7+
from plugins.com_imdevinc_StreamControllerTwitchPlugin.TwitchActionBase import TwitchActionBase
8+
9+
# Currently an issue with TwitchPy that gets the wrong time format, can't use this yet
10+
11+
12+
class NextAd(TwitchActionBase):
13+
def __init__(self, *args, **kwargs):
14+
super().__init__(*args, **kwargs)
15+
self.__ad_thread: threading.Thread = None
16+
17+
def on_ready(self):
18+
self.set_media(media_path=os.path.join(
19+
self.plugin_base.PATH, "assets", "view.png"), size=0.85)
20+
if not self.__ad_thread or not self.__ad_thread.is_alive():
21+
self.__ad_thread = threading.Thread(
22+
target=self.ad_thread, daemon=True, name="ad_thread")
23+
self.__ad_thread.start()
24+
25+
def ad_thread(self):
26+
while True:
27+
self.get_next_ad()
28+
time.sleep(1)
29+
30+
def get_next_ad(self):
31+
try:
32+
next_ad = self.plugin_base.backend.get_next_ad()
33+
if not next_ad:
34+
next_ad = "-"
35+
self.set_bottom_label(str(next_ad))
36+
self.hide_error()
37+
except Exception as ex:
38+
log.error(ex)
39+
self.show_error()

actions/play_ad.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import gi
3+
from gi.repository import Adw, Gtk
4+
5+
from loguru import logger as log
6+
7+
from plugins.com_imdevinc_StreamControllerTwitchPlugin.TwitchActionBase import TwitchActionBase
8+
9+
gi.require_version("Gtk", "4.0")
10+
gi.require_version("Adw", "1")
11+
12+
options = [30, 60, 90, 120]
13+
14+
15+
class PlayAd(TwitchActionBase):
16+
_time: int = 30
17+
18+
def on_ready(self):
19+
self.set_media(media_path=os.path.join(
20+
self.plugin_base.PATH, "assets", "money.png"), size=0.85)
21+
22+
def _load_config(self):
23+
settings = self.get_settings()
24+
time = settings.get("time")
25+
for value in options:
26+
if value == time:
27+
self._time = value
28+
return
29+
self._time = 30
30+
31+
def _on_change_time(self, *_):
32+
settings = self.get_settings()
33+
selected_index = self.time_row.get_selected()
34+
time = self.action_model[selected_index].get_string()
35+
settings["time"] = int(time)
36+
self.set_settings(settings)
37+
38+
def get_config_rows(self):
39+
super_rows = super().get_config_rows()
40+
self.action_model = Gtk.StringList()
41+
self.time_row = Adw.ComboRow(
42+
model=self.action_model, title=self.plugin_base.lm.get("actions.play_ad.time.label"))
43+
self._load_config()
44+
45+
index = 0
46+
found = -1
47+
for value in options:
48+
self.action_model.append(str(value))
49+
if value == self._time:
50+
found = index
51+
index += 1
52+
if found < 0:
53+
self.time_row.set_selected(0)
54+
55+
self.time_row.connect("notify::selected", self._on_change_time)
56+
self.time_row.set_selected(found)
57+
super_rows.append(self.time_row)
58+
return super_rows
59+
60+
def on_key_down(self):
61+
try:
62+
self.plugin_base.backend.play_ad(self._time)
63+
except Exception as ex:
64+
log.error(ex)
65+
self.show_error(3)

snooze_ad.py renamed to actions/snooze_ad.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class SnoozeAd(TwitchActionBase):
1010
def on_ready(self):
1111
self.set_media(media_path=os.path.join(
12-
self.plugin_base.PATH, "assets", "camera.png"), size=0.85)
12+
self.plugin_base.PATH, "assets", "delay.png"), size=0.85)
1313

1414
def on_key_down(self):
1515
try:

assets/delay.png

2.7 KB
Loading

assets/money.png

4.34 KB
Loading

locales/en_US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"actions.message.channel": "Channel",
1212
"actions.chat_mode.mode_row.label": "Chat Mode",
1313
"actions.info.link.label": "Checkout how to configure this plugin on",
14-
"actions.info.link.text": "GitHub"
14+
"actions.info.link.text": "GitHub",
15+
"actions.play_ad.time.label": "Ad Length"
1516
}

main.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from .actions.clip import Clip
1212
from .actions.marker import Marker
1313
from .actions.viewers import Viewers
14+
from .actions.play_ad import PlayAd
15+
from .actions.snooze_ad import SnoozeAd
16+
from .actions.ad_schedule import NextAd
1417

1518

1619
class PluginTemplate(PluginBase):
@@ -82,6 +85,30 @@ def __init__(self):
8285
)
8386
self.add_action_holder(self.clip_action_holder)
8487

88+
self.snooze_ad_action_holder = ActionHolder(
89+
plugin_base=self,
90+
action_base=SnoozeAd,
91+
action_id="com_imdevinc_StreamControllerTwitchPlugin::SnoozeAd",
92+
action_name="Snooze Ad"
93+
)
94+
self.add_action_holder(self.snooze_ad_action_holder)
95+
96+
self.play_ad_action_holder = ActionHolder(
97+
plugin_base=self,
98+
action_base=PlayAd,
99+
action_id="com_imdevinc_StreamControllerTwitchPlugin::PlayAd",
100+
action_name="Play Ad"
101+
)
102+
self.add_action_holder(self.play_ad_action_holder)
103+
104+
# self.next_ad_action_holder = ActionHolder(
105+
# plugin_base=self,
106+
# action_base=NextAd,
107+
# action_id="com_imdevinc_StreamControllerTwitchPlugin::NextAd",
108+
# action_name="Next Ad"
109+
# )
110+
# self.add_action_holder(self.next_ad_action_holder)
111+
85112
# Register plugin
86113
self.register(
87114
plugin_name="Twitch Integration",

play_ad.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

twitch_backend.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from urllib.parse import urlparse, parse_qs, urlencode
44
import threading
55
import requests
6+
from datetime import datetime
67

78
from loguru import logger as log
89
from twitchpy.client import Client
@@ -141,6 +142,13 @@ def play_ad(self, length: int) -> None:
141142
self.validate_auth()
142143
self.twitch.start_commercial(self.user_id, length)
143144

145+
def get_next_ad(self) -> datetime:
146+
if not self.twitch:
147+
return "Not Live"
148+
self.validate_auth()
149+
schedule = self.twitch.get_ad_schedule(self.user_id)
150+
return schedule.next_ad_at
151+
144152
def update_client_credentials(self, client_id: str, client_secret: str) -> None:
145153
if None in (client_id, client_secret) or "" in (client_id, client_secret):
146154
return
@@ -150,7 +158,7 @@ def update_client_credentials(self, client_id: str, client_secret: str) -> None:
150158
'client_id': client_id,
151159
'redirect_uri': 'http://localhost:3000/auth',
152160
'response_type': 'code',
153-
'scope': 'user:write:chat channel:manage:broadcast moderator:manage:chat_settings clips:edit channel:read:subscriptions'
161+
'scope': 'user:write:chat channel:manage:broadcast moderator:manage:chat_settings clips:edit channel:read:subscriptions channel:edit:commercial channel:manage:ads channel:read:ads'
154162
}
155163
encoded_params = urlencode(params)
156164
if not self.httpd:

0 commit comments

Comments
 (0)