private void ReadCallback(IAsyncResult asynchronousResult) { try { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); using (StreamReader reader = new StreamReader(response.GetResponseStream())) { JToken o = JObject.Parse(reader.ReadLine()); JArray featured = JArray.Parse(o.SelectToken("streams").ToString()); Deployment.Current.Dispatcher.BeginInvoke(() => { if (featured.Count == 0) IsStreamsLoaded = true; else { foreach (var arrayValue in featured) { var preview = new Preview(); var channel = new Channel(); var small = new BitmapImage(); var medium = new BitmapImage(); var viewers = int.Parse(arrayValue.SelectToken("viewers").ToString()); var display_name = arrayValue.SelectToken("channel").SelectToken("display_name").ToString(); var name = arrayValue.SelectToken("channel").SelectToken("name").ToString(); var status = ""; var logo = arrayValue.SelectToken("channel").SelectToken("logo").ToString(); try { status = arrayValue.SelectToken("channel").SelectToken("status").ToString(); } catch (Exception ex) { Debug.WriteLine(ex); if (status == "") { status = display_name; } } try { small = new BitmapImage(new Uri(arrayValue.SelectToken("preview").SelectToken("small").ToString())); medium = new BitmapImage(new Uri(arrayValue.SelectToken("preview").SelectToken("medium").ToString())); small.ImageFailed += ImageFailed; medium.ImageFailed += ImageFailed; } catch (Exception ex) { Debug.WriteLine(ex); } preview = new Preview { small = small, medium = medium }; channel = new Channel { display_name = display_name, name = name, status = status, logoUri = logo }; StreamList.Add(new TwitchAPIHandler.Objects.Stream() { channel = channel, preview = preview, viewers = viewers }); } } IsLoading = false; }); } } catch (Exception e) { Deployment.Current.Dispatcher.BeginInvoke(() => { MessageBox.Show("Network error occured: Couldn't load Top Streams for this Game"); }); Debug.WriteLine(e); } }
public static async Task<Stream> GetStream(string streamName) { Uri path = new Uri(string.Format(PathStrings.GET_STREAM, streamName)); var request = HttpWebRequest.Create(path); request.Method = "GET"; try { var response = await HttpRequest(request); JToken o = JObject.Parse(response); JToken stream = o.SelectToken("stream"); var channel = new Channel(); var viewers = int.Parse(stream.SelectToken("viewers").ToString()); var display_name = stream.SelectToken("channel").SelectToken("display_name").ToString(); var name = stream.SelectToken("channel").SelectToken("name").ToString(); var status = ""; var logo = stream.SelectToken("channel").SelectToken("logo").ToString(); try { status = stream.SelectToken("channel").SelectToken("status").ToString(); } catch { if (status == "") { status = display_name; } } channel = new Channel { display_name = display_name, name = name, status = status, logoUri = logo }; var streamToReturn = new Stream() { channel = channel, viewers = viewers }; return streamToReturn; } catch { return new Stream(); } }