Ejemplo n.º 1
0
        public async Task SetText(string text)
        {
            Facade facade = new Facade();
            Dispatcher dispatcher = Application.Current.Dispatcher;

            LinkTask task = new LinkTask
            {
                OnStatus = (link, status) =>
                {
                    dispatcher.Handle(() =>
                    {
                        this.model.SetStatus(link, status);
                    });
                },
                OnCompleted = (link, resource) =>
                {
                    dispatcher.Handle(() =>
                    {
                        this.model.Complete(link, resource);
                    });
                },
                OnLog = entry =>
                {
                    dispatcher.Handle(() =>
                    {
                        this.OnLog.Invoke(entry);
                    });
                }
            };

            this.model.ClearLinks();
            this.model.SetLinks(await facade.ParseTextToLinks(text));

            task.Links = this.model.GetAnalyzableLinks();
            await facade.Analyze(task);
        }
Ejemplo n.º 2
0
Archivo: Facade.cs Proyecto: amacal/ine
        public async Task Analyze(LinkTask task)
        {
            if (task.Links.Length > 0)
            {
                foreach (Link link in task.Links)
                {
                    task.OnStatus(link, "pending");
                }

                await Task.Run(() =>
                {
                    Parallel.ForEach(task.Links, new ParallelOptions { MaxDegreeOfParallelism = 2 }, link =>
                    {
                        task.OnStatus.Invoke(link, "checking");
                        task.OnLog.Information("Analyzing '{0}'.", link.Url);
                        task.OnLog.Information("Starting PhantomJS");

                        ProcessStartInfo info = new ProcessStartInfo
                        {
                            FileName = GetPhantomPath(),
                            Arguments = GetScriptPath(link.Hosting) + " query " + link.Url.ToString(),
                            UseShellExecute = false,
                            RedirectStandardOutput = true,
                            CreateNoWindow = true,
                            WindowStyle = ProcessWindowStyle.Hidden,
                            WorkingDirectory = GetDataPath()
                        };

                        using (Process process = Process.Start(info))
                        {
                            Resource resource = new Resource
                            {
                                Url = link.Url,
                                Hosting = link.Hosting,
                                IsAvailable = true
                            };

                            PhantomCallback callback = new PhantomCallback
                            {
                                OnDownload = url => true,
                                OnFileName = text =>
                                {
                                    resource.Name = text;
                                    return true;
                                },
                                OnFileSize = text =>
                                {
                                    resource.Size = text;
                                    return true;
                                },
                                OnFileStatus = text =>
                                {
                                    resource.IsAvailable = false;
                                    return false;
                                },
                                OnFatal = text =>
                                {
                                    task.OnLog.Warning(text);
                                    return true;
                                },
                                OnCaptcha = text => Task.FromResult(false),
                                OnDebug = text =>
                                {
                                    task.OnLog.Debug(text);
                                    return true;
                                },
                                OnFallback = text => true,
                                OnMessage = text => true,
                                OnRaw = text => { }
                            };

                            this.Handle(callback, CancellationToken.None, process).Wait();

                            if (resource.IsAvailable == false)
                            {
                                task.OnStatus(link, "unavailable");
                            }

                            if (resource.IsAvailable == true)
                            {
                                task.OnStatus(link, "available");
                            }

                            if (resource.Name != null && resource.Size != null)
                            {
                                task.OnCompleted(link, resource);
                            }

                            process.WaitForExit();
                        }
                    });
                });
            }
        }