예제 #1
0
        protected override bool InitalizeServiceData()
        {
            if (!base.InitalizeServiceData())
            {
                return(false);
            }

            System.Net.ServicePointManager.Expect100Continue = false;

            // Initalize FullService with his account access data.
            GetAccountAccessData();

            _accountData = new AccountData(UserAgent,
                                           GetConfigurationOptionsField("User"), Encryptor.Decrypt(GetConfigurationOptionsField("Password")),
                                           _accountEmail, GetConfigurationOptionsField("DeveloperToken"), GetConfigurationOptionsField("ApplicationToken"));

            _fullService = new FullService(_accountData);
            _fullService.Update();

            // Initalize the report parameters.
            string reportName = GetConfigurationOptionsField("ReportType");

            if (reportName == string.Empty)
            {
                Log.Write("Adwords retriever doesn't have ReportType parameter.", LogMessageType.Error);
                return(false);
            }

            TargetSubDir  = @"Google\" + reportName + @"\";
            ErrorFilePath = @"Google\" + reportName + @"\Errors";
            serviceType   = "Google.Adwords." + reportName;

            _googleJob = CreateReport("Google" + reportName + "Report", reportName != "Content" ? DailyAggergation : ContetAggergation);
            return(true);
        }
        private bool SaveReport(FullService fullService, DefinedReportJob googleJob, ref DateTime retrievedDay)
        {
            long   ID;
            string fileName = string.Empty;

            // Get Data from Google AdWords
            ID = fullService.ScheduleReportJob(googleJob, Instance.AccountID);

            // Invaild report.
            if (ID == 0)
            {
                throw new Exception("Can't get data from Google AdWords. return ID from google = 0");
            }

            string url = fullService.GetReportDownloadUrl(ID, Instance.AccountID);

            if (url == "failed")
            {
                throw new Exception("Can't get data from Google AdWords. return Url from google = 'failed'.");
            }

            fileName = WriteResultToFile(url, retrievedDay, googleJob.name, true);

            return(SaveFilePathToDB(_googleServiceType, fileName, retrievedDay, _adwordsFile));
        }
예제 #3
0
        /// <summary>
        /// New function for google adwords V13 that also use the app.config.
        /// The function get the report name and fetch the proper report type
        /// section from app.config with his info and his columns.
        /// </summary>
        /// <param name="type">The report name that exist in the app.config</param>
        /// <returns>A report with the relevent parameters</returns>
        private DefinedReportJob CreateReport(string name, string[] aggregationTypes)
        {
            Hashtable        fields = new Hashtable();
            DefinedReportJob job    = new DefinedReportJob();

            try
            {
                ReportTypesElementSection rtes = (ReportTypesElementSection)ConfigurationManager.GetSection("ReportTypes");

                // Scan all the reports types till we find the chosen report.
                foreach (ReportTypesElement rte in rtes.Fields)
                {
                    if (rte.Enabled && rte.Name == name)
                    {
                        // Load the proper report paramters by the report name.
                        FieldElementSection fes = (FieldElementSection)ConfigurationManager.GetSection(rte.Name);

                        // Initalize the hash table with the fields of the the report.
                        foreach (FieldElement fe in fes.Fields)
                        {
                            if (fe.Enabled)
                            {
                                fields.Add(fe.Key, fe.Key);
                            }
                        }

                        // Add the fields report to and string array useing IDictionaryEnumerator
                        string[] columns          = new string[fields.Count];
                        int      count            = 0;
                        IDictionaryEnumerator dic = fields.GetEnumerator();
                        while (dic.MoveNext())
                        {
                            columns[count] = dic.Value.ToString();
                            count++;
                        }

                        // Initalize the report with his info.
                        job.selectedReportType = rte.Type;
                        job.name = rte.Name;
                        //job.aggregationTypes = new String[] { rte.Aggregation };
                        job.aggregationTypes               = aggregationTypes;
                        job.includeZeroImpression          = true;
                        job.includeZeroImpressionSpecified = true;
                        job.selectedColumns = columns;

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write("Error get configuration Data for the report.", ex);
            }
            return(job);
        }
        private void RunReport(FullService fullService, DefinedReportJob googleJob, DateTime retrievedDay, bool errorRun)
        {
            Log.Write(string.Format("Run retriever for service {2}, for account {0}, for date {1}.", Instance.AccountID.ToString(), retrievedDay.ToShortDateString(), Instance.ParentInstance.Configuration.Name), LogMessageType.Information);

            string errorMsg = string.Empty;

            googleJob.startDay = retrievedDay;
            googleJob.endDay   = retrievedDay;

            // Fetch the report from AdWords,
            if (!Retrieve(fullService, googleJob, retrievedDay, ref errorMsg) && !errorRun)
            {
                _errorDates.Add(retrievedDay);
            }
            else if (errorRun)
            {
                Log.Write(string.Format("Can't retreieve date {0}, exception mesaage: {1}", retrievedDay, errorMsg), LogMessageType.Error);
            }
        }
        /// <summary>
        /// Get the data from google AdWords for goolge job.
        /// and save the data into xml file and save a link to the file in the DB.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="googleJob">The job to get his data fro AdWords.</param>
        /// <returns>The name of the xml file that contain the data from AdWords.</returns>
        private bool Retrieve(FullService fullService, DefinedReportJob googleJob, DateTime retrievedDay, ref string errorMsg)
        {
            int  numOfRetries = 0;
            bool reportSaved  = false;

            while (!reportSaved)
            {
                try
                {
                    reportSaved = SaveReport(fullService, googleJob, ref retrievedDay);
                }
                catch (Exception ex)
                {
                    if (numOfRetries >= MaxRetries)                     // too many retries, bail out
                    {
                        errorMsg = ex.Message.ToString();
                        return(false);
                    }

                    ++numOfRetries;
                }
            }
            return(true);
        }