Skip to content

Commit

Permalink
subsCount v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LotharieSlayer committed Aug 1, 2022
1 parent e676075 commit 5eea751
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 2 deletions.
4 changes: 2 additions & 2 deletions scripts/streamInfo/streamInfo.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ https://github.com/psf/requests/releases/tag/v2.27.1
- Your channel's name, ex: lotharie
- Client ID
- Oauth token
- Title Source, which source you want for the title of the stream channel
- Category Source, which source you want for the category of the stream channel
- Title Source, which source you want for the title of the stream channel (Text GDI)
- Category Source, which source you want for the category of the stream channel (Text GDI)
- Update Interval (optionnal, by default 20s)

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.
Expand Down
23 changes: 23 additions & 0 deletions scripts/subsCount/subsCount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### SubsCount manual

`subsCount.py` allows you to get the total number of subs of your Twitch channel.

**Externals Python libraries needed :**
- requests (2.27.1, last version supported by Python 3.6)
`pip install requests==2.27.1`
https://github.com/psf/requests/releases/tag/v2.27.1

**Settings to fill :**
- Your channel's ID, ex: your_channel = 2154354 (https://www.streamweasels.com/tools/convert-twitch-username-to-user-id/)
- Client ID
- Oauth token
- Text Source, which source you want where the number will be displayed (Text GDI)
- Update Interval (optionnal, by default 20s)

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.

### Why is it useful for me ?
To make a subs counter.

### Corrections :
v1.0 : /
107 changes: 107 additions & 0 deletions scripts/subsCount/subsCount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# @author LotharieSlayer (2022)
# @version 1.0

import requests as rq
import obspython as obs
from threading import Timer

text_source_name = ""
interval = 1
refresh_button = False

def update_text():
text_source = obs.obs_get_source_by_name(text_source_name)

header = {"Client-ID": client_id, "Authorization": f"Bearer {oauth}"}
response = rq.get(f"https://api.twitch.tv/helix/subscriptions?broadcaster_id={channel_id}", headers = header)
try:
# Refresh
text = str(response.json()['total'])
except:
# Refresh but offline stream
text = "X"

settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", text)
obs.obs_source_update(text_source, settings)
obs.obs_data_release(settings)

global refresh_button
if refresh_button:
# If refresh button pressed, update and return to not start a new thread
refresh_button = False
return

# Start the thread (again)
Timer(interval, update_text).start()


def refresh_pressed(props, prop):
global refresh_button
refresh_button = True
update_text()


# ------------------------------------------------------------

# OBS Script Functions

def script_update(settings):

global channel_id
global client_id
global oauth
global text_source_name
global interval
global refresh_button

interval = obs.obs_data_get_int(settings, "interval")
channel_id = obs.obs_data_get_string(settings, "channel_id")

client_id = obs.obs_data_get_string(settings, "client_id")
oauth = obs.obs_data_get_string(settings, "oauth")
text_source_name = obs.obs_data_get_string(settings, "text_source")

#print("Settings JSON", obs.obs_data_get_json(settings))

if client_id != "" and oauth != "" and text_source_name != "":
Timer(interval, update_text).start()



def script_description():
return "<b>SubsCount</b>" + \
"<hr>" + \
"Python script to get the total number of subs of a Twitch channel." + \
"<br/><br/>" + \
"Made by LotharieSlayer" + \
"<br/>" + \
"github.com/LotharieSlayer/OBS-scripts"



def script_properties():
props = obs.obs_properties_create()

obs.obs_properties_add_text(props, "channel_id", "Channel ID", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(props, "client_id", "Client ID", obs.OBS_TEXT_PASSWORD)
obs.obs_properties_add_text(props, "oauth", "Oauth", obs.OBS_TEXT_PASSWORD)

p = obs.obs_properties_add_list(props, "text_source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
sources = obs.obs_enum_sources()
if sources is not None:
for source in sources:
source_id = obs.obs_source_get_unversioned_id(source)
if source_id == "text_gdiplus" or source_id == "text_ft2_source":
name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)

obs.source_list_release(sources)

obs.obs_properties_add_int(props, "interval", "Update Interval (seconds)", 1, 3600, 1)
obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)

return props

def script_defaults(settings):
obs.obs_data_set_default_int(settings, "interval", 5)

0 comments on commit 5eea751

Please sign in to comment.