/// <summary> /// Attemt connection to PubSub server, after fetching channelIDs from Twitch API. /// </summary> /// <param name="credentials">Caster credentials, to requests private PubSub events.</param> public async Task <bool> Connect(TwitchCredentials credentials) { if (credentials.IsCaster) { // Set caster credentials _casterCredentials = credentials; try { v5User authUser = await TwitchAPIv5.GetUser(credentials.OAuth); if (authUser != null) { _userId = authUser.Id; _wsclient.ConnectAsync(); return(true); } else { return(false); } } catch { return(false); } } else { return(false); } }
/* https://dev.twitch.tv/docs/v5/reference/streams */ /// <summary> /// WARNING: HACKY METHOD 1000ms between API calls, Future will only accept userids /// Gets stream information for a specified user. /// https://dev.twitch.tv/docs/v5/reference/streams#get-stream-by-user /// </summary> /// <param name="channelName">username of the user to fetch the stream from.</param> /// <returns>Returns <see cref="TwitchStream"/> when a stream is live, null if not or on error.</returns> public static async Task <TwitchStream> GetStreamByUser(string channelName) { try { // Make the request to get user id from username v5User user = await GetUsers(channelName); if (user == null) { return(null); } await Task.Delay(500); // Make the request var blob = await TwitchAPIv5Requests.GetRequest($"https://api.twitch.tv/kraken/streams/{user.Id}"); // Deserialize into a JObject, not using reflection here yet. JToken blobObj = JObject.Parse(blob); // Check if 'stream' value is given or not if (blobObj["stream"] != null) { // Stream is online, return TwitchStream object return(blobObj.SelectToken("stream").ToObject <TwitchStream>()); } else { // Stream is offline, return null return(null); } } catch { return(null); } }