url-text.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import obspython as obs
  2. import urllib.request
  3. import urllib.error
  4. url = ""
  5. interval = 30
  6. source_name = ""
  7. # ------------------------------------------------------------
  8. def update_text():
  9. global url
  10. global interval
  11. global source_name
  12. source = obs.obs_get_source_by_name(source_name)
  13. if source is not None:
  14. try:
  15. with urllib.request.urlopen(url) as response:
  16. data = response.read()
  17. text = data.decode('utf-8')
  18. settings = obs.obs_data_create()
  19. obs.obs_data_set_string(settings, "text", text)
  20. obs.obs_source_update(source, settings)
  21. obs.obs_data_release(settings)
  22. except urllib.error.URLError as err:
  23. obs.script_log(obs.LOG_WARNING, "Error opening URL '" + url + "': " + err.reason)
  24. obs.remove_current_callback()
  25. obs.obs_source_release(source)
  26. def refresh_pressed(props, prop):
  27. update_text()
  28. # ------------------------------------------------------------
  29. def script_description():
  30. return "Updates a text source to the text retrieved from a URL at every specified interval.\n\nBy Lain"
  31. def script_update(settings):
  32. global url
  33. global interval
  34. global source_name
  35. url = obs.obs_data_get_string(settings, "url")
  36. interval = obs.obs_data_get_int(settings, "interval")
  37. source_name = obs.obs_data_get_string(settings, "source")
  38. obs.timer_remove(update_text)
  39. if url != "" and source_name != "":
  40. obs.timer_add(update_text, interval * 1000)
  41. def script_defaults(settings):
  42. obs.obs_data_set_default_int(settings, "interval", 30)
  43. def script_properties():
  44. props = obs.obs_properties_create()
  45. obs.obs_properties_add_text(props, "url", "URL", obs.OBS_TEXT_DEFAULT)
  46. obs.obs_properties_add_int(props, "interval", "Update Interval (seconds)", 5, 3600, 1)
  47. p = obs.obs_properties_add_list(props, "source", "Text Source", obs.OBS_COMBO_TYPE_EDITABLE, obs.OBS_COMBO_FORMAT_STRING)
  48. sources = obs.obs_enum_sources()
  49. if sources is not None:
  50. for source in sources:
  51. source_id = obs.obs_source_get_unversioned_id(source)
  52. if source_id == "text_gdiplus" or source_id == "text_ft2_source":
  53. name = obs.obs_source_get_name(source)
  54. obs.obs_property_list_add_string(p, name, name)
  55. obs.source_list_release(sources)
  56. obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
  57. return props