Last active
June 2, 2021 03:49
-
-
Save TheOpponent/d0d4269d14248aaa41f487da7f9cb56f to your computer and use it in GitHub Desktop.
OBS Python script: Query live viewer count and write to text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Writes viewer_count.txt to script path every 60 seconds for use with text source. | |
| # | |
| # Install pyTwitchAPI first: | |
| # https://github.com/Teekeks/pyTwitchAPI | |
| # pip3.6 install twitchAPI | |
| import obspython | |
| import os | |
| from datetime import datetime | |
| from twitchAPI.twitch import Twitch | |
| CHANNEL = "CHANNEL_NAME" | |
| twitch = Twitch('CLIENT_ID','CLIENT_SECRET') | |
| twitch.authenticate_app([]) | |
| txt_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),"viewer_count.txt") | |
| def count_viewers(): | |
| time_now = datetime.now().strftime("%H:%M:%S") | |
| if obspython.obs_frontend_streaming_active(): | |
| try: | |
| viewer_count = twitch.get_streams(user_login=CHANNEL)["data"][0]["viewer_count"] | |
| print(f"[{time_now}] {viewer_count} viewers.") | |
| with open(txt_path,"w") as stream_info: | |
| stream_info.write(str(viewer_count)) | |
| # If no data is returned, assume stream is offline. | |
| except IndexError: | |
| print(f"[{time_now}] Stream offline.") | |
| with open(txt_path,"w") as stream_info: | |
| stream_info.write("---") | |
| else: | |
| print(f"[{time_now}] Not streaming.") | |
| with open(txt_path,"w") as stream_info: | |
| stream_info.write("---") | |
| def script_unload(): | |
| with open(txt_path,"w") as stream_info: | |
| stream_info.write("") | |
| count_viewers() | |
| obspython.timer_add(count_viewers,60000) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment