Example #1
0
        public override void Download()
        {
            AffiliateNetwork Network = base.Network;


            try
            {
                DataTable dt = new DataTable();
                using (var bridge = ParserPool.Acquire(Database.Client))
                {
                    dt = bridge.Instance.ExecuteRawCommandToDataTable("SELECT * FROM CPAOffersOnHold WHERE NetworkName = '" + Network.Name + "' ORDER BY NetworkOfferIdInt ASC");
                }

                XmlElement element    = GetElement(Network.Name, Network.PerformaUsername, Network.PerformaPassword, Network.PerformaWebsiteId, ThrowExceptions);
                var        OnHoldList = TableHelper.GetListFromDataTable <CPAOfferOnHold>(dt, 100, true);

                //Lets process it in 1-1000 batches
                int TotalCount   = element.ChildNodes.Count;
                int BatchesCount = Convert.ToInt32(Math.Ceiling((double)TotalCount / (double)1000.0));
                int MainIterator = 0;

                for (int i = 0; i < BatchesCount; ++i)
                {
                    var  Query = ConstructInsertQueryStart();
                    bool AreSomeRecordsInQuery = false;

                    for (int j = 0; j < 1000 && MainIterator < TotalCount; j++)
                    {
                        XmlNode offer = element.ChildNodes[MainIterator];

                        string id           = offer["id"].InnerText;
                        string name         = offer["name"].InnerText;
                        string type         = offer["type"].InnerText;
                        string url          = offer["url"].InnerText;
                        string description  = offer["description"].InnerText;
                        string requirements = offer["requirements"].InnerText;
                        string rate         = offer["rate"].InnerText; // e.g. 0.24
                        string categoy      = offer["categoy"].InnerText;
                        string countries    = offer["countries"].InnerText;
                        string incent       = offer["incent"].InnerText; //'Yes' 'No'
                        string banner       = offer["banner"].InnerText;

                        CPAOfferOnHold ThisOffer = SyncedNetwork.HasThisOffer(OnHoldList, id, name);

                        if (ThisOffer != null)
                        {
                            //We have it, just change the status
                            ThisOffer.Status = Advertising.AdvertStatus.WaitingForAcceptance;
                        }
                        else
                        {
                            AreSomeRecordsInQuery = true;

                            //Preparte the variables to fit database

                            StringBuilder sb = new StringBuilder();
                            if (!string.IsNullOrEmpty(description))
                            {
                                sb.Append(description);
                                sb.Append("; ");
                            }
                            sb.Append(requirements);

                            Money UserRate;
                            if (rate.Contains("."))
                            {
                                UserRate = Money.Parse(rate);
                            }
                            else
                            {
                                UserRate = Money.Parse(rate + ".0");
                            }
                            UserRate = Money.MultiplyPercent(UserRate, Network.DefaultPercentForMembers);

                            if (name.Length > 50)
                            {
                                name = name.Substring(0, 49);
                            }
                            if (!string.IsNullOrEmpty(banner) && banner.Length > 150)
                            {
                                banner = banner.Substring(0, 149);
                            }

                            string ParsedDescription = sb.ToString();
                            if (ParsedDescription.Length >= 4000)
                            {
                                ParsedDescription = ParsedDescription.Substring(0, 3998);
                            }

                            //We need to add it
                            CPAOfferOnHold NewOffer = new CPAOfferOnHold();
                            NewOffer.IsFromAutomaticNetwork = true;
                            NewOffer.NetworkOfferId         = id;
                            NewOffer.Title              = name;
                            NewOffer.ImageURL           = banner;
                            NewOffer.TargetURL          = url;
                            NewOffer.DateAdded          = DateTime.Now;
                            NewOffer.Status             = Advertising.AdvertStatus.Active;
                            NewOffer.AdvertiserUsername = Network.Name;
                            NewOffer.LoginBoxRequired   = false;
                            NewOffer.EmailBoxRequired   = false;
                            NewOffer.LastCredited       = OffersManager.DateTimeZero;
                            NewOffer.Description        = ParsedDescription;
                            NewOffer.Category           = new Titan.CPACategory(Titan.CPACategory.DEFAULT_CATEGORY_ID);
                            NewOffer.BaseValue          = UserRate;
                            NewOffer.CreditsBought      = 1000000; //Infinity
                            NewOffer.NetworkName        = Network.Name;
                            NewOffer.NetworkRate        = rate;
                            NewOffer.IsSyncWithNetwork  = false; // by default
                            NewOffer.IsDaily            = false;
                            NewOffer.MaxDailyCredits    = 1;
                            //NewOffer.GeolocatedCountries = countries;

                            if (incent.Trim() == "No")
                            {
                                NewOffer.CreditOfferAs = CreditOfferAs.NonCash;
                            }
                            else
                            {
                                NewOffer.CreditOfferAs = CreditOfferAs.NetworkDefault;
                            }

                            SyncedNetwork.ConstructInsertQuery(Query, NewOffer);
                        }

                        //Increase the iterator
                        MainIterator++;
                    }
                    //Now execute the batch;
                    if (AreSomeRecordsInQuery)
                    {
                        using (var bridge = ParserPool.Acquire(Database.Client))
                        {
                            if (Query[Query.Length - 1] == ',')
                            {
                                Query[Query.Length - 1] = ';';
                            }

                            bridge.Instance.ExecuteRawCommandNonQuery(Query.ToString());
                        }
                    }
                }

                //Now, remove the offers that shouldn't be on the list
                foreach (var elem in OnHoldList)
                {
                    if (elem.Status != AdvertStatus.WaitingForAcceptance)
                    {
                        elem.Delete();
                    }
                }

                Network.LastSyncDate = DateTime.Now;
                Network.Save();
            }
            catch (Exception ex)
            {
                ErrorLogger.Log(ex);
                if (base.ThrowExceptions)
                {
                    throw new MsgException("There was a problem with offers export/download " + Network.Name + ": "
                                           + ex.Message + ". Check your username/password and if the website is available");
                }
            }
        }
Example #2
0
        public override void Download()
        {
            AffiliateNetwork Network = base.Network;

            try
            {
                DataTable dt = new DataTable();
                using (var bridge = ParserPool.Acquire(Database.Client))
                {
                    dt = bridge.Instance.ExecuteRawCommandToDataTable("SELECT * FROM CPAOffersOnHold WHERE NetworkName = '" + Network.Name + "' ORDER BY NetworkOfferIdInt ASC");
                }

                JArray element    = GetElement(Network.PublisherPassword, Network.PublisherUsername, ThrowExceptions);
                var    OnHoldList = TableHelper.GetListFromDataTable <CPAOfferOnHold>(dt, 100, true);

                //Lets process it in 1-1000 batches
                int TotalCount   = element.Count;
                int BatchesCount = Convert.ToInt32(Math.Ceiling((double)TotalCount / (double)1000.0));
                int MainIterator = 0;

                for (int i = 0; i < BatchesCount; ++i)
                {
                    var  Query = ConstructInsertQueryStart();
                    bool AreSomeRecordsInQuery = false;

                    for (int j = 0; j < 1000 && MainIterator < TotalCount; j++)
                    {
                        var offer = element[MainIterator];

                        string id          = offer["id"].ToString();
                        string name        = offer["name"].ToString();
                        string description = offer["description"].ToString();
                        string rate        = offer["default_payout"].ToString();

                        CPAOfferOnHold ThisOffer = SyncedNetwork.HasThisOffer(OnHoldList, id, name);

                        if (ThisOffer != null)
                        {
                            //We have it, just change the status
                            ThisOffer.Status = Advertising.AdvertStatus.WaitingForAcceptance;
                        }
                        else
                        {
                            AreSomeRecordsInQuery = true;

                            //Preparte the variables to fit database
                            Money UserRate;
                            if (rate.Contains("."))
                            {
                                UserRate = Money.Parse(rate);
                            }
                            else
                            {
                                UserRate = Money.Parse(rate + ".0");
                            }
                            UserRate = Money.MultiplyPercent(UserRate, Network.DefaultPercentForMembers);

                            if (name.Length > 50)
                            {
                                name = name.Substring(0, 49);
                            }

                            string url = GetTrackingUrl(Network.PublisherPassword, Network.PublisherUsername, id);
                            //if (string.IsNullOrEmpty(url))
                            //{
                            //    System.Diagnostics.Debug.WriteLine(id);
                            //    MainIterator++;
                            //    continue;
                            //}
                            string countries = GetCountries(id, Network.PublisherPassword, Network.PublisherUsername);

                            //We need to add it
                            CPAOfferOnHold NewOffer = new CPAOfferOnHold();
                            NewOffer.IsFromAutomaticNetwork = true;
                            NewOffer.NetworkOfferId         = id;
                            NewOffer.Title              = name;
                            NewOffer.TargetURL          = url;
                            NewOffer.DateAdded          = DateTime.Now;
                            NewOffer.Status             = Advertising.AdvertStatus.Active;
                            NewOffer.AdvertiserUsername = Network.Name;
                            NewOffer.LoginBoxRequired   = false;
                            NewOffer.EmailBoxRequired   = false;
                            NewOffer.LastCredited       = OffersManager.DateTimeZero;
                            NewOffer.Description        = description;
                            NewOffer.Category           = new Titan.CPACategory(Titan.CPACategory.DEFAULT_CATEGORY_ID);
                            NewOffer.BaseValue          = UserRate;
                            NewOffer.CreditsBought      = 1000000; //Infinity
                            NewOffer.NetworkName        = Network.Name;
                            NewOffer.NetworkRate        = rate;
                            NewOffer.IsSyncWithNetwork  = false; // by default
                            NewOffer.IsDaily            = false;
                            NewOffer.MaxDailyCredits    = 1;
                            NewOffer.AddGeolocation(new GeolocationUnit(countries));
                            NewOffer.CreditOfferAs = CreditOfferAs.NetworkDefault;

                            SyncedNetwork.ConstructInsertQuery(Query, NewOffer);
                        }

                        //Increase the iterator
                        MainIterator++;
                    }
                    //Now execute the batch;
                    if (AreSomeRecordsInQuery)
                    {
                        using (var bridge = ParserPool.Acquire(Database.Client))
                        {
                            if (Query[Query.Length - 1] == ',')
                            {
                                Query[Query.Length - 1] = ';';
                            }

                            bridge.Instance.ExecuteRawCommandNonQuery(Query.ToString());
                        }
                    }
                }

                //Now, remove the offers that shouldn't be on the list
                foreach (var elem in OnHoldList)
                {
                    if (elem.Status != AdvertStatus.WaitingForAcceptance)
                    {
                        elem.Delete();
                    }
                }

                Network.LastSyncDate = DateTime.Now;
                Network.Save();
            }
            catch (Exception ex)
            {
                ErrorLogger.Log(ex);
                if (base.ThrowExceptions)
                {
                    throw new MsgException("There was a problem with offers export/download " + Network.Name + ": "
                                           + ex.Message + ". Check your username/password and if the website is available");
                }
            }
        }