-
Notifications
You must be signed in to change notification settings - Fork 0
/
YouTubeSubAge_StreamlabsSystem.py
118 lines (95 loc) · 5.4 KB
/
YouTubeSubAge_StreamlabsSystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import sys
import json
import datetime
import codecs
sys.path.append(os.path.join(os.path.dirname(__file__), "lib")) #point at lib folder for classes / references
import clr
clr.AddReference("IronPython.SQLite.dll")
clr.AddReference("IronPython.Modules.dll")
#Import your Settings class
from Settings_Module import MySettings
ScriptName = "YouTube Sub Age"
Website = "https://youtube.com/prohenis"
Description = "Type !subage in YouTube chat and Streamlabs Chatbot will return the date & time the user subscribed to the channel. (only if the user has their YouTube subscriptions public)"
Creator = "ProHenis"
Version = "1.0.0.0"
settings = {}
def Init():
global settings
work_dir = os.path.dirname(__file__)
with codecs.open(os.path.join(work_dir, "settings.json"), encoding='utf-8-sig') as json_file:
settings = json.load(json_file, encoding='utf-8-sig')
return
def Execute(data):
UserPerm = Parent.HasPermission(data.User, settings["Permission"], settings["Command"])
if data.GetParam(0) != settings["Command"]:
return
if Parent.IsOnUserCooldown(ScriptName, settings["Command"], data.User):
log(data.UserName + " - FAILED: user still has cooldown")
return
if UserPerm != 1:
log(data.UserName + " - FAILED: user does not have permission")
return
chatterUsername = data.UserName
chatterChannelID = data.User
headers = {}
request = Parent.GetRequest("https://youtube.googleapis.com/youtube/v3/subscriptions?part=snippet%2CcontentDetails&forChannelId=" + settings["streamerChannelID"] + "&channelId=" + chatterChannelID + "&key=" + settings["youtubeAPIKey"], headers)
request_json = json.loads(request)
# Output the API request to a text file in AppData\Roaming\Streamlabs\Streamlabs Chatbot for debugging (remove # before lines 55 & 56 to enable)
#with open("youtubesubage_api_output.txt", "w") as file:
# file.write(request)
if "error" in request_json and request_json["error"] == "Forbidden":
Parent.SendStreamMessage("@" + chatterUsername + " subscription privacy is set to private. Change it to public here: https://www.youtube.com/account_privacy")
log(data.UserName + " - FAILED: user subscriptions set to private")
elif 'totalResults' in request_json['response'] and json.loads(request_json['response'])['pageInfo']['totalResults'] == 0:
Parent.SendStreamMessage("@" + chatterUsername + " is not subscribed to the channel")
log(data.UserName + " - FAILED: user not subscribed to the channel")
elif 'publishedAt' in request_json['response']:
items = json.loads(request_json["response"])["items"]
published_at = items[0]["snippet"]["publishedAt"]
# Handles new & old time formats
if '.' in published_at:
timestamp = datetime.datetime.strptime(published_at, "%Y-%m-%dT%H:%M:%S.%fZ")
else:
timestamp = datetime.datetime.strptime(published_at, "%Y-%m-%dT%H:%M:%SZ")
time_format = settings["timeFormat"]
if time_format == "Name of Month-Day-Year":
formatted_time = timestamp.strftime("%B %d, %Y")
if time_format == "Name of Month-Day-Year 12hr Clock":
formatted_time = timestamp.strftime("%B %d, %Y %I:%M %p UTC")
if time_format == "Name of Month-Day-Year 12hr Clock w/ Seconds":
formatted_time = timestamp.strftime("%B %d, %Y %I:%M:%S %p UTC")
if time_format == "Name of Month-Day-Year 24hr Clock":
formatted_time = timestamp.strftime("%B %d, %Y %H:%M UTC")
if time_format == "Name of Month-Day-Year 24hr Clock w/ Seconds":
formatted_time = timestamp.strftime("%B %d, %Y %H:%M:%S UTC")
if time_format == "Month-Day-Year":
formatted_time = timestamp.strftime("%m-%d-%Y")
if time_format == "Month-Day-Year 12hr Clock":
formatted_time = timestamp.strftime("%m-%d-%Y %I:%M %p UTC")
if time_format == "Month-Day-Year 12hr Clock w/ Seconds":
formatted_time = timestamp.strftime("%m-%d-%Y %I:%M:%S %p UTC")
if time_format == "Month-Day-Year 24hr Clock":
formatted_time = timestamp.strftime("%m-%d-%Y %H:%M UTC")
if time_format == "Month-Day-Year 24hr Clock w/ Seconds":
formatted_time = timestamp.strftime("%m-%d-%Y %H:%M:%S UTC")
if time_format == "Day-Month-Year":
formatted_time = timestamp.strftime("%d-%m-%Y")
if time_format == "Day-Month-Year 12hr Clock":
formatted_time = timestamp.strftime("%d-%m-%Y %I:%M %p UTC")
if time_format == "Day-Month-Year 12hr Clock w/ Seconds":
formatted_time = timestamp.strftime("%d-%m-%Y %I:%M:%S %p UTC")
if time_format == "Day-Month-Year 24hr Clock":
formatted_time = timestamp.strftime("%d-%m-%Y %H:%M UTC")
if time_format == "Day-Month-Year 24hr Clock w/ Seconds":
formatted_time = timestamp.strftime("%d-%m-%Y %H:%M:%S UTC")
Parent.SendStreamMessage("@" + chatterUsername + " subscribed on " + formatted_time)
log(data.UserName + " - SUCCESS: posted time in chat")
Parent.AddUserCooldown(ScriptName, settings["Command"], data.User, settings["userCooldown"])
return
def log(message):
Parent.Log(settings["Command"], message)
return
def Tick():
return