Skip to content

Instantly share code, notes, and snippets.

@oscarnovasf
Last active July 14, 2025 14:46
Show Gist options
  • Select an option

  • Save oscarnovasf/6d8e45d5128b31f45e3618808b3c07b1 to your computer and use it in GitHub Desktop.

Select an option

Save oscarnovasf/6d8e45d5128b31f45e3618808b3c07b1 to your computer and use it in GitHub Desktop.
OBS. Actualizar escena título con el título del directo
# Necesario instalar la librería requests:
# pip install requests
import requests
import obspython as obs
from textwrap import wrap
# Tu información de autenticación de Twitch.
client_id = ''
client_secret = ''
# Esta variable se rellena automáticamente con el token de acceso.
access_token = ""
# Valores por defecto y configurables en OBS.
interval = 30
source_name = ""
channel_name = ""
# Este parámetro se puede obtener de: https://www.streamweasels.com/tools/convert-twitch-username-to-user-id/
broadcaster_id = ""
# Construye la URL para obtener información del canal
api_url = f"https://api.twitch.tv/helix/channels?broadcaster_id={channel_name}"
# ------------------------------------------------------------
def update_text():
global url
global interval
global source_name
# Obtengo token de acceso.
body = {
'client_id': client_id,
'client_secret': client_secret,
"grant_type": 'client_credentials'
}
r = requests.post('https://id.twitch.tv/oauth2/token', body)
keys = r.json();
source = obs.obs_get_source_by_name(source_name)
if source is not None:
settings = obs.obs_data_create()
# Encabezados de solicitud HTTP.
headers = {
'Client-ID': client_id,
'Authorization': 'Bearer ' + keys['access_token']
}
# Realiza la solicitud HTTP.
response = requests.get('https://api.twitch.tv/helix/channels?broadcaster_id=' + broadcaster_id, headers=headers)
if response.status_code == 200:
# Parsea la respuesta JSON
data = response.json()
# Obtiene el título del canal
channel_title = data["data"][0]["title"]
channel_title = "\n".join(wrap(channel_title, 40))
obs.obs_data_set_string(settings, "text", channel_title)
else:
obs.obs_data_set_string(settings, "text", "")
obs.script_log(obs.LOG_WARNING, "Error: '" + response.reason)
obs.remove_current_callback()
obs.obs_source_update(source, settings)
obs.obs_data_release(settings)
obs.obs_source_release(source)
def refresh_pressed(props, prop):
update_text()
# ------------------------------------------------------------
def script_description():
return "Actualiza un elemento de texto con el título del directo actual.\n\nPor ONovasDev"
def script_update(settings):
global url
global interval
global source_name
global broadcaster_id
interval = obs.obs_data_get_int(settings, "interval")
source_name = obs.obs_data_get_string(settings, "source")
broadcaster_id = obs.obs_data_get_string(settings, "broadcaster_id")
obs.timer_remove(update_text)
if source_name != "":
obs.timer_add(update_text, interval * 1000)
def script_defaults(settings):
obs.obs_data_set_default_string(settings, "channel_name", "ONovasDev")
obs.obs_data_set_default_string(settings, "broadcaster_id", "")
obs.obs_data_set_default_int(settings, "interval", 30)
def script_properties():
props = obs.obs_properties_create()
obs.obs_properties_add_int(props, "interval", "Update Interval (seconds)", 5, 3600, 1)
obs.obs_properties_add_text(props, "channel_name", "Nombre del Canal", obs.OBS_TEXT_DEFAULT)
obs.obs_properties_add_text(props, "broadcaster_id", "Id del Canal", obs.OBS_TEXT_DEFAULT)
p = obs.obs_properties_add_list(props, "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_button(props, "button", "Refresh", refresh_pressed)
return props
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment