How do i Make Python to Docks

Darkero

New Member
Hello, I've adapted a Python code from Youtube chat and channel updater. I made some modifications because the interval for refreshing in seconds was causing "Encoding Overload" in my OBS when streaming or recording.

To address this issue, I removed the timer update interval and implemented a hotkeys version or refresh button.

I decided to remake this code because, while streaming on YouTube, I needed a way to update my YouTube Studio live chat automatically instead of doing it manually once each time before I start streaming.

Now, I'm wondering if it's possible to dock this code in OBS. If so, how can I do it? What I mean is, can I integrate this functionality directly into OBS for easier access and control?

this is my code what i remake

Python:
import obspython as obs
import urllib.request
import urllib.error
import lxml

url         = ""
source_name_text = ""
source_name_browser = ""

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

def update_text():
    global url
    global source_name_text
    global source_name_browser

    source = obs.obs_get_source_by_name(source_name_text)
    if source is not None:
        try:
            with urllib.request.urlopen(url) as response:
                data = response.read()
                txt = str(data)
                x = txt.find("\"videoId\"")
                y = x + 23
                y = txt[x:y]
                z = y.find(":") + 1
                extractvid = y[z:]
                newvid = extractvid.replace("\"","")
                newUrl = "https://www.youtube.com/watch?v="+newvid
                newchatUrl = "https://www.youtube.com/live_chat?dark_theme=1&is_popout=1&v="+newvid
                with urllib.request.urlopen(newUrl) as response2:
                    data2 = response2.read()
                    from lxml import etree
                    youtube = etree.HTML(data2)
                    video_url = youtube.xpath("//title")
                    video_text = video_url[0].text
                    findu2b = video_text.find(" - YouTube")
                    if findu2b >= 0:
                        video_text = video_text[:findu2b]
                    string_length=len(video_text)+2    # will be adding 2 extra spaces
                    text=video_text.center(string_length)

                settings = obs.obs_data_create()
                obs.obs_data_set_string(settings, "text", text.encode('latin_1').decode('utf8'))
                obs.obs_source_update(source, settings)
                obs.obs_data_release(settings)

        except urllib.error.URLError as err:
            obs.script_log(obs.LOG_WARNING, "Error opening URL '" + url + "': " + err.reason)
            obs.remove_current_callback()

        obs.obs_source_release(source)

    sourceb = obs.obs_get_source_by_name(source_name_browser)
    if source is not None:
        try:
            #test
            settingsb = obs.obs_data_create()
            obs.obs_data_set_string(settingsb, "url", newchatUrl)
            obs.obs_source_update(sourceb, settingsb)
            obs.obs_data_release(settingsb)

        except urllib.error.URLError as err:
            obs.script_log(obs.LOG_WARNING, "Error opening URL '" + url + "': " + err.reason)
            obs.remove_current_callback()

        obs.obs_source_release(sourceb)       

def refresh_pressed(props, prop):
    print("Key Pressed")
    update_text()

class Hotkey:
    def __init__(props, callback, obs_settings, _id):
        props.obs_data = obs_settings
        props.hotkey_id = obs.OBS_INVALID_HOTKEY_ID
        props.hotkey_saved_key = None
        props.callback = callback
        props._id = _id

        props.load_hotkey()
        props.register_hotkey()
        props.save_hotkey()

    def register_hotkey(props):
        description = str(props._id)
        props.hotkey_id = obs.obs_hotkey_register_frontend(
        str(props._id), description, props.callback
        )
        obs.obs_hotkey_load(props.hotkey_id, props.hotkey_saved_key)

    def load_hotkey(props):
        props.hotkey_saved_key = obs.obs_data_get_array(
            props.obs_data,str(props._id)
        )
        obs.obs_data_array_release(props.hotkey_saved_key)

    def save_hotkey(props):
        props.hotkey_saved_key = obs.obs_hotkey_save(props.hotkey_id)
        obs.obs_data_set_array(
            props.obs_data,str(props._id), props.hotkey_saved_key
        )
        obs.obs_data_array_release(props.hotkey_saved_key)

class HotkeyDataHolder:
    htk_copy = None  # this attribute will hold instance of Hotkey
h01 = HotkeyDataHolder()

def script_save(settings):
    for h in [h01]:
        h.htk_copy.save_hotkey()
# ------------------------------------------------------------

def script_description():
    return "Update Livechat for Using CSS on livechat OBS.\n\nChannel URL = Your Channel URL\nText Source = Streaming Title\nBrowser Source = Browser Source Live Chat\n\n Modified by Erol"

def script_update(settings):
    global url
    global source_name_text
    global source_name_browser

    url         = obs.obs_data_get_string(settings, "url")
    source_name_text = obs.obs_data_get_string(settings, "source")
    source_name_browser = obs.obs_data_get_string(settings, "sourceb")

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

    obs.obs_properties_add_text(props, "url", "Channel URL", obs.OBS_TEXT_DEFAULT)

    p1 = 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(p1, name, name)

        obs.source_list_release(sources)

    q = obs.obs_properties_add_list(props, "sourceb", "Browser Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
    sourcesb = obs.obs_enum_sources()
    if sourcesb is not None:
        for sourceb in sourcesb:
            source_id = obs.obs_source_get_id(sourceb)
            if source_id == "browser_source":
                nameb = obs.obs_source_get_name(sourceb)
                obs.obs_property_list_add_string(q, nameb, nameb)

        obs.source_list_release(sourcesb)

    obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)

    
    return props

def refresh_presseder(props):
    print("Shortcut Pressed")
    update_text()

def script_load(settings):
    h01.htk_copy = Hotkey(refresh_presseder, settings, "Triggers Refresh")
 

Attachments

  • Screenshot_186.png
    Screenshot_186.png
    3.3 KB · Views: 9

Skoony

New Member
To make Python work with Docker, you need to follow these steps:
  1. Install Docker on your system if you haven't already.
  2. Create a Dockerfile in your Python project directory. This file contains instructions for building a Docker image.
  3. In the Dockerfile, specify the base image, which would be Python. You can use an official Python image, such as "python:3.9" for Python 3.9.
  4. Install the required Python packages by adding a "RUN pip install" command followed by the list of packages.
  5. Add a command to be executed when the container starts, like "CMD ["your_python_script.py"]".
  6. Build the Docker image by running the command "docker build -t your_image_name ." in your project directory.
  7. Run the Docker container using the command "docker run -it --name your_container_name your_image_name".
  8. Python, along with your project files and dependencies, is running inside the Docker container.
 

Darkero

New Member
To make Python work with Docker, you need to follow these steps:
  1. Install Docker on your system if you haven't already.
  2. Create a Dockerfile in your Python project directory. This file contains instructions for building a Docker image.
  3. In the Dockerfile, specify the base image, which would be Python. You can use an official Python image, such as "python:3.9" for Python 3.9.
  4. Install the required Python packages by adding a "RUN pip install" command followed by the list of packages.
  5. Add a command to be executed when the container starts, like "CMD ["your_python_script.py"]".
  6. Build the Docker image by running the command "docker build -t your_image_name ." in your project directory.
  7. Run the Docker container using the command "docker run -it --name your_container_name your_image_name".
  8. Python, along with your project files and dependencies, is running inside the Docker container.
i mean not that docker, but docker OBS, that you can see in your overlay obs UI
 
Top