Skip to content

Commit 5eea751

Browse files
subsCount v1.0
1 parent e676075 commit 5eea751

File tree

3 files changed

+132
-2
lines changed

3 files changed

+132
-2
lines changed

scripts/streamInfo/streamInfo.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ https://github.com/psf/requests/releases/tag/v2.27.1
1111
- Your channel's name, ex: lotharie
1212
- Client ID
1313
- Oauth token
14-
- Title Source, which source you want for the title of the stream channel
15-
- Category Source, which source you want for the category of the stream channel
14+
- Title Source, which source you want for the title of the stream channel (Text GDI)
15+
- Category Source, which source you want for the category of the stream channel (Text GDI)
1616
- Update Interval (optionnal, by default 20s)
1717

1818
If you have problem running this Python script, please make sure you carefully follow the instructions from the <a href="https://github.com/LotharieSlayer/OBS-scripts">README</a> file at the root of this repository.

scripts/subsCount/subsCount.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
### SubsCount manual
2+
3+
`subsCount.py` allows you to get the total number of subs of your Twitch channel.
4+
5+
**Externals Python libraries needed :**
6+
- requests (2.27.1, last version supported by Python 3.6)
7+
`pip install requests==2.27.1`
8+
https://github.com/psf/requests/releases/tag/v2.27.1
9+
10+
**Settings to fill :**
11+
- Your channel's ID, ex: your_channel = 2154354 (https://www.streamweasels.com/tools/convert-twitch-username-to-user-id/)
12+
- Client ID
13+
- Oauth token
14+
- Text Source, which source you want where the number will be displayed (Text GDI)
15+
- Update Interval (optionnal, by default 20s)
16+
17+
If you have problem running this Python script, please make sure you carefully follow the instructions from the <a href="https://github.com/LotharieSlayer/OBS-scripts">README</a> file at the root of this repository.
18+
19+
### Why is it useful for me ?
20+
To make a subs counter.
21+
22+
### Corrections :
23+
v1.0 : /

scripts/subsCount/subsCount.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# @author LotharieSlayer (2022)
2+
# @version 1.0
3+
4+
import requests as rq
5+
import obspython as obs
6+
from threading import Timer
7+
8+
text_source_name = ""
9+
interval = 1
10+
refresh_button = False
11+
12+
def update_text():
13+
text_source = obs.obs_get_source_by_name(text_source_name)
14+
15+
header = {"Client-ID": client_id, "Authorization": f"Bearer {oauth}"}
16+
response = rq.get(f"https://api.twitch.tv/helix/subscriptions?broadcaster_id={channel_id}", headers = header)
17+
try:
18+
# Refresh
19+
text = str(response.json()['total'])
20+
except:
21+
# Refresh but offline stream
22+
text = "X"
23+
24+
settings = obs.obs_data_create()
25+
obs.obs_data_set_string(settings, "text", text)
26+
obs.obs_source_update(text_source, settings)
27+
obs.obs_data_release(settings)
28+
29+
global refresh_button
30+
if refresh_button:
31+
# If refresh button pressed, update and return to not start a new thread
32+
refresh_button = False
33+
return
34+
35+
# Start the thread (again)
36+
Timer(interval, update_text).start()
37+
38+
39+
def refresh_pressed(props, prop):
40+
global refresh_button
41+
refresh_button = True
42+
update_text()
43+
44+
45+
# ------------------------------------------------------------
46+
47+
# OBS Script Functions
48+
49+
def script_update(settings):
50+
51+
global channel_id
52+
global client_id
53+
global oauth
54+
global text_source_name
55+
global interval
56+
global refresh_button
57+
58+
interval = obs.obs_data_get_int(settings, "interval")
59+
channel_id = obs.obs_data_get_string(settings, "channel_id")
60+
61+
client_id = obs.obs_data_get_string(settings, "client_id")
62+
oauth = obs.obs_data_get_string(settings, "oauth")
63+
text_source_name = obs.obs_data_get_string(settings, "text_source")
64+
65+
#print("Settings JSON", obs.obs_data_get_json(settings))
66+
67+
if client_id != "" and oauth != "" and text_source_name != "":
68+
Timer(interval, update_text).start()
69+
70+
71+
72+
def script_description():
73+
return "<b>SubsCount</b>" + \
74+
"<hr>" + \
75+
"Python script to get the total number of subs of a Twitch channel." + \
76+
"<br/><br/>" + \
77+
"Made by LotharieSlayer" + \
78+
"<br/>" + \
79+
"github.com/LotharieSlayer/OBS-scripts"
80+
81+
82+
83+
def script_properties():
84+
props = obs.obs_properties_create()
85+
86+
obs.obs_properties_add_text(props, "channel_id", "Channel ID", obs.OBS_TEXT_DEFAULT)
87+
obs.obs_properties_add_text(props, "client_id", "Client ID", obs.OBS_TEXT_PASSWORD)
88+
obs.obs_properties_add_text(props, "oauth", "Oauth", obs.OBS_TEXT_PASSWORD)
89+
90+
p = obs.obs_properties_add_list(props, "text_source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
91+
sources = obs.obs_enum_sources()
92+
if sources is not None:
93+
for source in sources:
94+
source_id = obs.obs_source_get_unversioned_id(source)
95+
if source_id == "text_gdiplus" or source_id == "text_ft2_source":
96+
name = obs.obs_source_get_name(source)
97+
obs.obs_property_list_add_string(p, name, name)
98+
99+
obs.source_list_release(sources)
100+
101+
obs.obs_properties_add_int(props, "interval", "Update Interval (seconds)", 1, 3600, 1)
102+
obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
103+
104+
return props
105+
106+
def script_defaults(settings):
107+
obs.obs_data_set_default_int(settings, "interval", 5)

0 commit comments

Comments
 (0)