Skip to content

Commit

Permalink
Fixes for latest Twitch API, Adds Anonymous Login
Browse files Browse the repository at this point in the history
- Can now login Anonymously, just don't enter a username or oauth,
closes #19
- Adds Client-ID capability, closes #20
  • Loading branch information
Hotrian committed Sep 21, 2016
1 parent 672f15a commit 6d9df98
Show file tree
Hide file tree
Showing 585 changed files with 1,207 additions and 112 deletions.
Binary file modified Assets/HOTK/Twitch/TwitchChatBGMat.mat
Binary file not shown.
196 changes: 125 additions & 71 deletions Assets/HOTK/Twitch/TwitchChatTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,49 +81,72 @@ public void Start()
StartCoroutine("SyncWithSteamVR");
}

private string _username;

private void GenRandomJustinFan()
{
if (!string.IsNullOrEmpty(_username)) return;
var r = new System.Random();
var n = r.Next().ToString();
if (n.Length > 5) n = n.Substring(0, 5);
_username = "JustinFan" + n;
}

public void ToggleConnect()
{
if (!Connected)
{
if (UsernameBox != null && UsernameBox.text != "")
var anonymousLogin = false;
if (!string.IsNullOrEmpty(UsernameBox.text))
{
if (OAuthBox != null && OAuthBox.text != "")
if (string.IsNullOrEmpty(OAuthBox.text))
{
if (ChannelBox != null && ChannelBox.text != "")
{
if (ChannelBox.text.Contains(" "))
{
AddSystemNotice("Channel name invalid!", TwitchIRC.NoticeColor.Red);
return;
}
UsernameBox.interactable = false;
OAuthBox.interactable = false;
ChannelBox.interactable = false;
ConnectButtonText.text = "Press to Disconnect";

Connected = true;
OnChatMsg(new TwitchIRC.TwitchMessage(TwitchIRC.ToTwitchNotice(string.Format("Logging into #{0} as {1}!", ChannelFirstLetterToUpper(ChannelBox.text), FirstLetterToUpper(UsernameBox.text)))));
IRC.NickName = UsernameBox.text;
IRC.Oauth = OAuthBox.text;
IRC.ChannelName = ChannelBox.text.Trim().ToLower();

IRC.enabled = true;
IRC.MessageRecievedEvent.AddListener(OnChatMsg);
IRC.StartIRC();
knownFollowers.Clear();
StopCoroutine("UpdateViews");
StopCoroutine("UpdateFollowers");
StopCoroutine("SyncWithSteamVR");
gettingInitialFollowers = true;
StartCoroutine("UpdateViews");
StartCoroutine("UpdateFollowers");
StartCoroutine("SyncWithSteamVR");
}
else AddSystemNotice("Unable to Connect: Enter a Valid Channel Name!", TwitchIRC.NoticeColor.Red);
AddSystemNotice("OAuth not found. Connecting Anonymously.", TwitchIRC.NoticeColor.Yellow);
GenRandomJustinFan();
UsernameBox.text = _username;
OAuthBox.text = "";
anonymousLogin = true;
}
else AddSystemNotice("Unable to Connect: Enter a Valid OAuth Key! http://www.twitchapps.com/tmi/", TwitchIRC.NoticeColor.Red);
}
else AddSystemNotice("Unable to Connect: Enter a Valid Username!", TwitchIRC.NoticeColor.Red);
else
{
AddSystemNotice("Username not found. Connecting Anonymously.", TwitchIRC.NoticeColor.Yellow);
GenRandomJustinFan();
UsernameBox.text = _username;
OAuthBox.text = "";
anonymousLogin = true;
}
if (ChannelBox != null && ChannelBox.text != "")
{
if (ChannelBox.text.Contains(" "))
{
AddSystemNotice("Channel name invalid!", TwitchIRC.NoticeColor.Red);
return;
}
UsernameBox.interactable = false;
OAuthBox.interactable = false;
ChannelBox.interactable = false;
ConnectButtonText.text = "Press to Disconnect";

Connected = true;
OnChatMsg(new TwitchIRC.TwitchMessage(TwitchIRC.ToTwitchNotice(string.Format("Logging into #{0} as {1}!", ChannelFirstLetterToUpper(ChannelBox.text), FirstLetterToUpper(UsernameBox.text)))));
IRC.NickName = anonymousLogin ? _username : UsernameBox.text;
IRC.Oauth = anonymousLogin ? "a" : OAuthBox.text;
IRC.ChannelName = ChannelBox.text.Trim().ToLower();

IRC.enabled = true;
IRC.MessageRecievedEvent.AddListener(OnChatMsg);
IRC.StartIRC();
_knownFollowers.Clear();
StopCoroutine("UpdateViews");
StopCoroutine("UpdateFollowers");
StopCoroutine("SyncWithSteamVR");
_gettingInitialFollowers = true;
StartCoroutine("UpdateViews");
StartCoroutine("UpdateFollowers");
StartCoroutine("SyncWithSteamVR");
}
else AddSystemNotice("Unable to Connect: Enter a Valid Channel Name!", TwitchIRC.NoticeColor.Red);
}
else
{
Expand All @@ -136,14 +159,14 @@ public void ToggleConnect()
IRC.MessageRecievedEvent.RemoveListener(OnChatMsg);
IRC.enabled = false;
OnChatMsg(new TwitchIRC.TwitchMessage(TwitchIRC.ToTwitchNotice("Disconnected!", TwitchIRC.NoticeColor.Red)));
knownFollowers.Clear();
_knownFollowers.Clear();
StopCoroutine("UpdateViews");
StopCoroutine("UpdateFollowers");
ClearViewerCountAndChannelName("Disconnected");
}
}

IEnumerator SyncWithSteamVR()
private IEnumerator SyncWithSteamVR()
{
while (Application.isPlaying)
{
Expand All @@ -157,38 +180,51 @@ IEnumerator SyncWithSteamVR()
}
}

private Dictionary<uint, FollowsData> knownFollowers = new Dictionary<uint, FollowsData>();
private bool gettingInitialFollowers;
private readonly Dictionary<uint, FollowsData> _knownFollowers = new Dictionary<uint, FollowsData>();
private bool _gettingInitialFollowers;

IEnumerator UpdateFollowers()
private IEnumerator UpdateFollowers()
{
while (Connected && IRC.ChannelName.Length > 0)
{
WWW www = new WWW(URLAntiCacheRandomizer("https://api.twitch.tv/kraken/channels/" + IRC.ChannelName + "/follows"));
var form = new WWWForm();
form.AddField("name", "value");
var headers = form.headers;
var url = URLAntiCacheRandomizer("https://api.twitch.tv/kraken/channels/" + IRC.ChannelName + "/follows?limit=100");

headers["Client-ID"] = "REMOVED FOR GITHUB"; //#TODO Replace with your Client-ID
var www = new WWW(url, null, headers);
yield return www;
FollowsDataFull obj = JsonUtility.FromJson<FollowsDataFull>(www.text);
if (obj != null)
if (string.IsNullOrEmpty(www.error))
{
if (obj.follows != null)
var obj = JsonUtility.FromJson<FollowsDataFull>(www.text);
if (obj != null)
{
if (obj.follows.Length > 0)
if (obj.follows != null)
{
foreach (var follower in obj.follows)
if (obj.follows.Length > 0)
{
if (!knownFollowers.ContainsKey(follower.user._id))
Debug.Log("Found " + obj._total + " followers, retrieved top " + obj.follows.Length);
foreach (var follower in obj.follows.Where(follower => !_knownFollowers.ContainsKey(follower.user._id)))
{
knownFollowers.Add(follower.user._id, follower);
if (!gettingInitialFollowers)
{
OnChatMsg(new TwitchIRC.TwitchMessage(TwitchIRC.ToTwitchNotice(follower.user.display_name + " is now following!", TwitchIRC.NoticeColor.Purple)));
PlayNewFollowerSound();
}
_knownFollowers.Add(follower.user._id, follower);
if (_gettingInitialFollowers) continue;
OnChatMsg(
new TwitchIRC.TwitchMessage(
TwitchIRC.ToTwitchNotice(
follower.user.display_name + " is now following!",
TwitchIRC.NoticeColor.Purple)));
PlayNewFollowerSound();
}
_gettingInitialFollowers = false;
}
gettingInitialFollowers = false;
}
}
}
else
{
Debug.LogError("Error on page (" + url + "): " + www.error);
}
yield return new WaitForSeconds(30f);
}
}
Expand All @@ -203,38 +239,56 @@ public static string URLAntiCacheRandomizer(string url)
}

// Update the view count as often as possible
IEnumerator UpdateViews()
private IEnumerator UpdateViews()
{
while (Connected && IRC.ChannelName.Length > 0)
{
WWW www = new WWW("https://api.twitch.tv/kraken/streams/" + IRC.ChannelName);
var form = new WWWForm();
form.AddField("name", "value");
var headers = form.headers;
var url = "https://api.twitch.tv/kraken/streams/" + IRC.ChannelName;

headers["Client-ID"] = "REMOVED FOR GITHUB"; //#TODO Replace with your Client-ID
var www = new WWW(url, null, headers);

yield return www;
ChannelDataFull obj = JsonUtility.FromJson<ChannelDataFull>(www.text);
if (obj != null)

if (string.IsNullOrEmpty(www.error))
{
if (obj.stream != null)
var obj = JsonUtility.FromJson<ChannelDataFull>(www.text);
if (obj != null)
{
if (obj.stream.channel != null)
if (obj.stream != null)
{
if (ChannelNameTextMesh != null)
if (obj.stream.channel != null)
{
var text = "";
if (!string.IsNullOrEmpty(obj.stream.channel.display_name)) text = string.Format("#{0}", obj.stream.channel.display_name);
else if (!string.IsNullOrEmpty(obj.stream.channel.name)) text = string.Format("#{0}", obj.stream.channel.name);
else text = "Not Streaming";
ChannelNameTextMesh.text = text;
if (ChannelNameTextMesh != null)
{
var text = "";
if (!string.IsNullOrEmpty(obj.stream.channel.display_name))
text = string.Format("#{0}", obj.stream.channel.display_name);
else if (!string.IsNullOrEmpty(obj.stream.channel.name))
text = string.Format("#{0}", obj.stream.channel.name);
else text = "Not Streaming";
ChannelNameTextMesh.text = text;
}
if (ViewerCountTextMesh != null)
ViewerCountTextMesh.text = string.Format("Viewers: {0}", obj.stream.viewers);
}
else
{
ClearViewerCountAndChannelName();
}
if (ViewerCountTextMesh != null) ViewerCountTextMesh.text = string.Format("Viewers: {0}", obj.stream.viewers);
}
else
{
ClearViewerCountAndChannelName();
}
}
else
{
ClearViewerCountAndChannelName();
}
}
else
{
Debug.LogError("Error on page (" + url + "): " + www.error);
}
yield return new WaitForSeconds(10f);
}
Expand Down
Binary file modified Library/AssetServerCacheV3
Binary file not shown.
Binary file modified Library/CurrentLayout.dwlt
Binary file not shown.
Binary file modified Library/EditorUserBuildSettings.asset
Binary file not shown.
Binary file modified Library/ProjectSettings.asset
Binary file not shown.
Binary file modified Library/ScriptAssemblies/Assembly-CSharp-Editor.dll
Binary file not shown.
Binary file modified Library/ScriptAssemblies/Assembly-CSharp-Editor.dll.mdb
Binary file not shown.
Binary file modified Library/ScriptAssemblies/Assembly-CSharp-firstpass.dll
Binary file not shown.
Binary file modified Library/ScriptAssemblies/Assembly-CSharp-firstpass.dll.mdb
Binary file not shown.
Binary file modified Library/ScriptAssemblies/Assembly-CSharp.dll
Binary file not shown.
Binary file modified Library/ScriptAssemblies/Assembly-CSharp.dll.mdb
Binary file not shown.
4 changes: 2 additions & 2 deletions Library/ScriptAssemblies/BuiltinAssemblies.stamp
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
0000.572f869a.0000
0000.572f86ae.0000
0000.5787632e.0000
0000.57876340.0000
Binary file modified Library/ScriptMapper
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 6d9df98

Please sign in to comment.