示例#1
0
        /// <summary>
        /// Fügt dem Dictionary eine gesammelte Information hinzu
        /// </summary>
        /// <param name="currentContent">Webseiten Teilinhalt in dem gerade gelesen wird.</param>
        /// <param name="command">Kommando für das Lesen von Websiteabschnitten</param>
        /// <param name="querytarget">Ziel in dem der ermittelte Informationsteil abgelegt werden soll</param>
        /// <param name="isTargetInformationItem">Handelt es sich bei dem Ziel für den Informationsteil um ein Informationsitem</param>
        /// <param name="querycontent">Neu ausgelesener Teilinhalt der Website</param>
        private void AddInformationToken(
            string currentContent,
            WebcrawlerCommand command,
            string querytarget,
            bool isTargetInformationItem,
            IEnumerable <string> querycontent
            )
        {
            var isFirstStoreCommand = ContextCommandset.First(storecommand => storecommand.Target == querytarget) == command;

            //  Die Information wird hinzugefügt, wenn ...
            if (
                //  kein Informationsitem ergänzt werden soll, oder ...
                !isTargetInformationItem ||
                //  der aktuell gefundene Inhalt nicht leer ist. (Sonst werden statisch hinzugefügte Informationen in einem Informationsitem abgelegt, obwohl es gar kein Informationsitem gibt.)
                !string.IsNullOrEmpty(currentContent)
                )
            {
                //  Erstes Speicherkommando >> Neuanlage einer Informationswiederholung
                if (isFirstStoreCommand || !isTargetInformationItem)
                {
                    var storecontent = ContextDictionary.ContainsKey(querytarget) ? new List <string>(ContextDictionary[querytarget]) : new List <string>();
                    storecontent.AddRange(querycontent);
                    ContextDictionary[querytarget] = storecontent;
                }
                //  Ergänzen der letzten Speicherinformation
                else
                {
                    var storeitems    = ContextDictionary.ContainsKey(querytarget) ? ContextDictionary[querytarget] : default(IEnumerable <string>);
                    var lastStoreitem = storeitems != null && storeitems.Any() ? storeitems.Last() : default(string);
                    if (lastStoreitem != null)
                    {
                        lastStoreitem += string.Format(" {0}", string.Join(" ", querycontent));
                        var storecontent = ContextDictionary[querytarget].ToList();
                        storecontent[storecontent.Count - 1] = lastStoreitem;
                        ContextDictionary[querytarget]       = storecontent;
                    }
                }
            }
        }
示例#2
0
        public void Crawling(IEnumerable <WebcrawlerCommand> commandset, IEnumerable <string> contents = null)
        {
            if (commandset != null)
            {
                contents = contents ?? new[] { string.Empty };
                foreach (var content in contents)
                {
                    var storetarget = string.Empty;

                    foreach (var command in commandset)
                    {
                        var doRepeat = true;
                        while (doRepeat)
                        {
                            var subcommandContents = default(List <string>);

                            //  Anwendung eines Kommandos
                            var querytext      = command.Command;
                            var queryattribute = command.AttributID;

                            var wasCommandApplied = default(bool);
                            //  Ausführen des Querys
                            if (!string.IsNullOrEmpty(querytext) || !string.IsNullOrEmpty(queryattribute))
                            {
                                var querytarget   = command.Target;
                                var commandtokens = default(string[]);
                                commandtokens = (querytarget ?? string.Empty).Split('.');
                                var isTargetInformationItem = IsInformationItem(querytarget);

                                //  Ermittlung des Queryergebnis
                                var querycontent = FindContent(
                                    content,
                                    command
                                    );

                                //  Bei einem Informationsobjekt als Zieltyp ...
                                commandtokens = (querytarget ?? string.Empty).Split('.');
                                if (isTargetInformationItem)
                                {
                                    //  ... wird bei erster Verwendung/einer Wiederholung der Index des Zielobjektes hochgezählt
                                    var targetbase        = commandtokens.First();
                                    var lastObjectCommand = ContextCommandset.Last(c => c.Target.StartsWith(targetbase + "."));

                                    if (command == lastObjectCommand)
                                    {
                                        storetarget = $"{targetbase}.";
                                    }
                                }

                                var isCrawlingCommand = WebcrawlingUtilityConstants.Commands.Any(commandid => querytext.ToLower().Contains(commandid.ToLower()));
                                //  Die Abfrage ergab mindestens einen Treffer => Inhaltsverarbeitung
                                if (!isCrawlingCommand)
                                {
                                    foreach (var c in querycontent)
                                    {
                                        subcommandContents = subcommandContents ?? new List <string>();
                                        subcommandContents.Add(c);
                                    }

                                    //  Inhalt im Ziel sichern
                                    if (!string.IsNullOrEmpty(querytarget) && querycontent.Any())
                                    {
                                        AddInformationToken(
                                            content,
                                            command,
                                            querytarget,
                                            isTargetInformationItem,
                                            querycontent
                                            );
                                    }
                                }
                                //  Kein Treffer => Sonstiger Befehl
                                else
                                {
                                    if (!string.IsNullOrEmpty(querytarget))
                                    {
                                        ContextDictionary[querytarget] = new[] { string.Empty };
                                    }

                                    commandtokens = querytext.Split(':');
                                    var commandobject     = commandtokens.FirstOrDefault() ?? string.Empty;
                                    var commandexpression = commandtokens.Count() > 1 ? commandtokens.Skip(1).FirstOrDefault() ?? string.Empty : string.Empty;

                                    //  Befehle
                                    //  Browse()
                                    switch (commandexpression.ToLower())
                                    {
                                    case WebcrawlingUtilityConstants.CommandBrowse:
                                    {
                                        var uris = ContextDictionary.ContainsKey(commandobject) ? ContextDictionary[commandobject] ?? default(string[]) : default(string[]);
                                        if (uris != null)
                                        {
                                            foreach (var uri in uris)
                                            {
                                                var browseuri = uri.Trim();
                                                if (!string.IsNullOrEmpty(browseuri))
                                                {
                                                    subcommandContents = subcommandContents ?? new List <string>();
                                                    subcommandContents.Add(WebUtility.GetWebsiteContent(browseuri));
                                                    wasCommandApplied = true;
                                                }
                                            }
                                        }

                                        //  Bei einer regulären Variable wird der Inhalt der Variablen nach dem Browsen zurückgesetzt
                                        if (!IsInformationItem(commandobject))
                                        {
                                            ContextDictionary[commandobject] = Enumerable.Empty <string>();
                                        }

                                        break;
                                    }
                                    }
                                }
                            }

                            //  Ausführen der Subkommandos
                            if (command.Subcommands != null)
                            {
                                Crawling(command.Subcommands, subcommandContents);
                            }

                            doRepeat =
                                command.IsLoop && wasCommandApplied
                            ;
                        }
                    }

                    //  Wenn das storetarget gesetzt wurde (letzter Zielverweis auf Informationsobjekt), wird das Informationsobjekt über einen Provider gesichert.
                    if (!string.IsNullOrEmpty(storetarget) && this.DataWarehouseProvider != null)
                    {
                        var storedictionary = new Dictionary <string, IEnumerable <object> >();
                        foreach (var entry in this.ContextDictionary.Where(e => e.Key.StartsWith(storetarget)))
                        {
                            storedictionary[entry.Key] = entry.Value;
                        }

                        if (storedictionary != null && storedictionary.Any())
                        {
                            this.DataWarehouseProvider.StoreInformation(storedictionary);
                        }

                        storedictionary.ToList().ForEach(storeitem => { ContextDictionary.Remove(storeitem.Key); });
                    }
                }
            }
        }
        public void Crawling(IEnumerable <WebcrawlerCommand> commandset, IEnumerable <string> contents = null)
        {
            if (commandset != null)
            {
                contents = contents ?? new[] { string.Empty };
                foreach (var content in contents)
                {
                    var storetarget = string.Empty;

                    foreach (var command in commandset)
                    {
                        var doRepeat = true;
                        while (doRepeat)
                        {
                            var subcommandContents = default(List <string>);

                            //  Anwendung eines Kommandos
                            var querytext      = command.Command;
                            var queryattribute = command.AttributID;

                            var wasCommandApplied = default(bool);
                            //  Ausführen des Querys
                            if (!string.IsNullOrEmpty(querytext) || !string.IsNullOrEmpty(queryattribute))
                            {
                                var querytarget   = command.Target;
                                var commandtokens = default(string[]);
                                commandtokens = (querytarget ?? string.Empty).Split('.');
                                var isTargetInformationItem =
                                    !WebcrawlingUtilityConstants.BasicVariables.Contains(querytarget) &&
                                    commandtokens?.Length > 1
                                ;

                                //  Ermittlung des Queryergebnis
                                var querycontent = FindContent(
                                    content,
                                    command
                                    );

                                //  Bei einem Informationsobjekt als Zieltyp ...
                                commandtokens = (querytarget ?? string.Empty).Split('.');
                                if (isTargetInformationItem)
                                {
                                    //  ... wird bei erster Verwendung/einer Wiederholung der Index des Zielobjektes hochgezählt
                                    var targetbase        = commandtokens.First();
                                    var lastObjectCommand = ContextCommandset.Last(c => c.Target.StartsWith(targetbase + "."));

                                    if (command == lastObjectCommand)
                                    {
                                        storetarget = $"{targetbase}.";
                                    }
                                }

                                var isCrawlingCommand = WebcrawlingUtilityConstants.Commands.Any(commandid => querytext.ToLower().Contains(commandid.ToLower()));
                                //  Die Abfrage ergab mindestens einen Treffer => Inhaltsverarbeitung
                                if (!isCrawlingCommand)
                                {
                                    foreach (var c in querycontent)
                                    {
                                        subcommandContents = subcommandContents ?? new List <string>();
                                        subcommandContents.Add(c);
                                    }

                                    //  Inhalt im Ziel sichern
                                    if (!string.IsNullOrEmpty(querytarget) && querycontent.Any())
                                    {
                                        var isFirstStoreCommand = ContextCommandset.First(storecommand => storecommand.Target == querytarget) == command;

                                        //  Erstes Speicherkommando >> Neuanlage einer Informationswiederholung
                                        if (isFirstStoreCommand || !isTargetInformationItem)
                                        {
                                            var storecontent = ContextDictionary.ContainsKey(querytarget) ? new List <string>(ContextDictionary[querytarget]) : new List <string>();
                                            storecontent.AddRange(querycontent);
                                            ContextDictionary[querytarget] = storecontent;
                                        }
                                        //  Ergänzen der letzten Speicherinformation
                                        else
                                        {
                                            var storeitems    = ContextDictionary.ContainsKey(querytarget) ? ContextDictionary[querytarget] : default(IEnumerable <string>);
                                            var lastStoreitem = storeitems != null && storeitems.Any() ? storeitems.Last() : default(string);
                                            if (lastStoreitem != null)
                                            {
                                                lastStoreitem += string.Format(" {0}", string.Join(" ", querycontent));
                                                var storecontent = ContextDictionary[querytarget].ToList();
                                                storecontent[storecontent.Count - 1] = lastStoreitem;
                                                ContextDictionary[querytarget]       = storecontent;
                                            }
                                        }
                                    }
                                }
                                //  Kein Treffer => Sonstiger Befehl
                                else
                                {
                                    if (!string.IsNullOrEmpty(querytarget))
                                    {
                                        ContextDictionary[querytarget] = new[] { string.Empty };
                                    }

                                    commandtokens = querytext.Split(':');
                                    var commandobject     = commandtokens.FirstOrDefault() ?? string.Empty;
                                    var commandexpression = commandtokens.Count() > 1 ? commandtokens.Skip(1).FirstOrDefault() ?? string.Empty : string.Empty;

                                    //  Befehle
                                    //  Browse()
                                    switch (commandexpression.ToLower())
                                    {
                                    case WebcrawlingUtilityConstants.CommandBrowse:
                                    {
                                        var uris = ContextDictionary.ContainsKey(commandobject) ? ContextDictionary[commandobject] ?? default(string[]) : default(string[]);
                                        if (uris != null)
                                        {
                                            foreach (var uri in uris)
                                            {
                                                var browseuri = uri.Trim();
                                                if (!string.IsNullOrEmpty(browseuri))
                                                {
                                                    if (!new[] { "http:", "https:" }.Any(prefix => browseuri.StartsWith(prefix)))
                                                    {
                                                        var baseuri = ContextDictionary.ContainsKey(WebcrawlingUtilityConstants.BaseUri) ? ContextDictionary[WebcrawlingUtilityConstants.BaseUri].FirstOrDefault() ?? string.Empty : string.Empty;
                                                        browseuri = baseuri + browseuri;
                                                    }

                                                    subcommandContents = subcommandContents ?? new List <string>();
                                                    subcommandContents.Add(WebUtility.GetWebsiteContent(browseuri));
                                                    wasCommandApplied = true;
                                                }
                                            }
                                        }

                                        break;
                                    }
                                    }
                                }
                            }

                            //  Ausführen der Subkommandos
                            if (command.Subcommands != null)
                            {
                                Crawling(command.Subcommands, subcommandContents);
                            }

                            doRepeat =
                                command.IsLoop && wasCommandApplied
                            ;
                        }
                    }

                    //  Wenn das storetarget gesetzt wurde (letzter Zielverweis auf Informationsobjekt), wird das Informationsobjekt über einen Provider gesichert.
                    if (!string.IsNullOrEmpty(storetarget) && this.Storageprovider != null)
                    {
                        var storedictionary = new Dictionary <string, IEnumerable <string> >();
                        foreach (var entry in this.ContextDictionary.Where(e => e.Key.StartsWith(storetarget)))
                        {
                            storedictionary[entry.Key] = entry.Value;
                        }

                        //  Überarbeiten der ID-Inhalte
                        var keys = storedictionary.Where(item => item.Key.ToLower().EndsWith(".id")).Select(item => item.Key).ToList();
                        if (keys != null)
                        {
                            var baseuri = this.ContextDictionary.ContainsKey(WebcrawlingUtilityConstants.BaseUri) ? this.ContextDictionary[WebcrawlingUtilityConstants.BaseUri].First() : string.Empty;
                            baseuri = Regex.Split(baseuri, @"://")?.Skip(1).FirstOrDefault() ?? string.Empty;
                            if (!string.IsNullOrEmpty(baseuri))
                            {
                                foreach (var key in keys)
                                {
                                    storedictionary[key] = storedictionary[key].Select(dictionarycontent => $"{baseuri}.{dictionarycontent}");
                                }
                            }
                        }

                        this.Storageprovider.StoreInformation(storedictionary);
                        storedictionary.ToList().ForEach(storeitem => { ContextDictionary.Remove(storeitem.Key); });
                    }
                }
            }
        }