Beispiel #1
0
        /// <summary>
        /// Change running campaign status
        /// </summary>
        /// <param name="campaign">Campaign data</param>
        /// <param name="v">If true, campaign will be paused. Otherwise it will be resumed</param>
        /// <returns></returns>
        private RedditAdJson ToggleCampaign(Campaign campaign, bool v)
        {
            var result = new RedditAdJson();

            string postString =
                $"campaign_id36={campaign.CampaignId}&link_id36={campaign.PromotionId}&should_pause={v.ToString().ToLower()}&uh={campaign.Uh}&renderstyle=html";
            var postUrl = "https://www.reddit.com//api/toggle_pause_campaign";

            var toggleRequest = WebRequest.Create(postUrl) as HttpWebRequest;

            if (toggleRequest == null)
            {
                throw new ArgumentNullException(nameof(toggleRequest));
            }

            toggleRequest.ContentType            = "application/x-www-form-urlencoded; charset=UTF-8";
            toggleRequest.Method                 = "POST";
            toggleRequest.CookieContainer        = Cookies;
            toggleRequest.Accept                 = "application/json, text/javascript, */*; q=0.01";
            toggleRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
            toggleRequest.UserAgent              =
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36";
            toggleRequest.Referer = "https://www.reddit.com/";
            toggleRequest.Timeout = 15000;

            var customHeaders = toggleRequest.Headers;

            customHeaders.Add("accept-language", "en;q=0.4");
            customHeaders.Add("origin", "https://www.reddit.com");
            customHeaders.Add("x-requested-with", "XMLHttpRequest");

            var bytes = Encoding.ASCII.GetBytes(postString);

            toggleRequest.ContentLength = bytes.Length;

            using (var os = toggleRequest.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);
            }

            var toggleResponse = toggleRequest.GetResponse() as HttpWebResponse;


            if (toggleResponse != null && toggleResponse.StatusCode == HttpStatusCode.OK)
            {
                using (var s = toggleResponse.GetResponseStream())
                {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    using (var sr = new StreamReader(s, Encoding.GetEncoding(toggleResponse.CharacterSet)))
                    {
                        result = JsonConvert.DeserializeObject <RedditAdJson>(sr.ReadToEnd());
                    }
                }
            }

            return(result);
        }
Beispiel #2
0
        private async void ResumeAllPausedcampaignsButton_Click(object sender, RoutedEventArgs e)
        {
            GeneralProgressBar.Visibility = Visibility.Visible;

            var runningCampaigns = Campaigns.Where(x => x.State == State.Paused).ToList();

            foreach (var campaign in runningCampaigns)
            {
                var errorMsg = string.Empty;
                var error    = false;
                var result   = new RedditAdJson();

                try
                {
                    var task = Task.Factory.StartNew(() =>
                    {
                        result = ToggleCampaign(campaign, false);
                    });

                    await task;
                }
                catch (AggregateException ae)
                {
                    ae.Handle(x =>
                    {
                        errorMsg = x.Message + " | " + x.StackTrace;
                        error    = true;

                        return(error);
                    });
                }

                if (error)
                {
                    Log("ERROR", errorMsg);
                }
                else
                {
                    if (result.Success)
                    {
                        Log("INFO", $"Campaign #{campaign.CampaignId} on ad #{campaign.PromotionId} successfully resumed");
                        campaign.State = State.Running;
                    }
                }
            }

            GeneralProgressBar.Visibility = Visibility.Hidden;
            ShowCampaignsStatus();
            ResumeAllPausedcampaignsButton.IsEnabled = false;
            PauseAllActivecampaignsButton.IsEnabled  = true;
        }