コード例 #1
0
ファイル: JobProcessor.cs プロジェクト: nojan1/WebTagger
        public async Task ProcessJob(Job job)
        {
            if (string.IsNullOrEmpty(job.Url))
            {
                logger.Debug($"Job '{job.Name}' has no URL, ignoring");
                return;
            }

            try
            {
                var html = await httpWrapper.GetPageContent(job.Url);

                var pageParser = new Pageparser(html);

                if (job.Replace)
                {
                    tagRepository.Clear(job.Url);
                }

                if (!string.IsNullOrEmpty(job.Category))
                {
                    tagRepository.AddTag(job.Url, "category", job.Category, job.AccessLevel);
                }

                foreach (var selection in job.Selections)
                {
                    List <string> values;

                    if (selection.Hardcoded)
                    {
                        values = selection.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Trim()).ToList();
                    }
                    else
                    {
                        values = pageParser.GetSelectionValues(selection.SearchPath);
                    }

                    if (!values.Any())
                    {
                        logger.Warn($"Job '{job.Name}' returned no values");
                        continue;
                    }

                    foreach (var value in values)
                    {
                        if (selection.Output == OutputType.Tag)
                        {
                            tagRepository.AddTag(job.Url, selection.TagName, value, job.AccessLevel);
                        }
                        else
                        {
                            string url = value;

                            Uri temp;

                            if (!Uri.TryCreate(url, UriKind.Absolute, out temp))
                            {
                                url = new Uri(new Uri(job.Url), url).ToString();
                            }

                            jobRepository.RegisterAdhocJob(url, selection.JobName);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error($"Job '{job.Name}' failed", ex);
            }
        }