Exemplo n.º 1
0
        /// <summary>
        /// Recreates viewers if they are below our desired viewer count
        /// </summary>
        /// <param name="sender"></param>
        private static void RecreateViewers(object sender)
        {
            _recreateViewersTimer.Change(Timeout.Infinite, Timeout.Infinite);
            int currentViewCount = 0;

            lock (Viewer.ViewerCountLock)
            {
                currentViewCount = Viewer.AliveViewerCount;
            }

            System.Threading.Tasks.Parallel.For(currentViewCount, Settings.Viewers, x => Viewer.CreateViewer());
            _recreateViewersTimer.Change(TimeSpan.FromSeconds(5), Timeout.InfiniteTimeSpan);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a viewer
        /// </summary>
        public static void CreateViewer()
        {
            try
            {
                ViewBot.Proxy proxy = ViewBot.Proxy.GetProxy();
                if (proxy == null)
                {
                    return;
                }


                //Console.WriteLine("Got proxy");

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.twitch.tv/api/channels/" + Settings.Channel + "/access_token");
                WebProxy       myproxy = new WebProxy(proxy.Ip, proxy.Port);
                myproxy.BypassProxyOnLocal = false;
                request.Proxy   = myproxy;
                request.Method  = "GET";
                request.Timeout = 10000;

                request.BeginGetResponse(ar =>
                {
                    try
                    {
                        string responseText = string.Empty;
                        string sig          = string.Empty, token = string.Empty;
                        using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar))
                        {
                            if (response.StatusCode != HttpStatusCode.OK)
                            {
                                Console.WriteLine(DateTime.Now + ": Couldn't Get Token/Sig");
                                return;
                            }

                            //Console.WriteLine("Status OK");
                            using (var reader = new System.IO.StreamReader(response.GetResponseStream(), ASCIIEncoding.ASCII))
                            {
                                responseText = reader.ReadToEnd();
                            }
                        }
                        JObject json = JObject.Parse(responseText);

                        JToken temp;

                        json.TryGetValue("token", out temp);
                        token = (string)temp;
                        json.TryGetValue("sig", out temp);
                        sig = (string)temp;

                        if (string.IsNullOrWhiteSpace(sig) || string.IsNullOrWhiteSpace(token))
                        {
                            Console.WriteLine(DateTime.Now + ": Sig and/or token returned an empty string.");
                            return;
                        }

                        request = (HttpWebRequest)WebRequest.Create("http://usher.twitch.tv/select/" + Settings.Channel + ".json?nauthsig=" + sig + "&nauth=" + token + "&type=any");
                        myproxy = new WebProxy(proxy.Ip, proxy.Port);
                        myproxy.BypassProxyOnLocal = false;
                        request.Proxy   = myproxy;
                        request.Method  = "GET";
                        request.Timeout = 10000;
                        request.BeginGetResponse(ar2 =>
                        {
                            try
                            {
                                string url   = string.Empty;
                                responseText = string.Empty;
                                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar2))
                                {
                                    if (response.StatusCode != HttpStatusCode.OK)
                                    {
                                        Console.WriteLine(DateTime.Now + ": Couldn't Get URL");
                                        return;
                                    }

                                    using (var reader = new System.IO.StreamReader(response.GetResponseStream(), ASCIIEncoding.ASCII))
                                    {
                                        responseText = reader.ReadToEnd();
                                    }
                                }

                                url = responseText.Split('\n').LastOrDefault(x => !string.IsNullOrWhiteSpace(x));

                                if (string.IsNullOrWhiteSpace(url) || url.Equals("[]"))
                                {
                                    Console.WriteLine(DateTime.Now + ": Error Getting URL, Stream Offline?");
                                    return;
                                }

                                Viewer v = new Viewer(proxy, url);
                            }
                            catch (Exception)
                            {
                                Console.WriteLine(DateTime.Now + ": Exception when creating viewer.");
                                return;
                            }
                        }, null);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine(DateTime.Now + ": Exception when creating viewer.");
                        return;
                    }
                }, null);
            }
            catch (Exception)
            {
                Console.WriteLine(DateTime.Now + ": Exception when creating viewer.");
                return;
            }
            //Console.WriteLine("Viewer Created successfully.");
            return;
        }