Example #1
0
        /* indexPDF
         * indicizza le informazioni estratte dal file PDF
         */
        public bool indexPDF(page p, int pdfSize, string pdf2text)
        {
            string outStr;
            string sql;
            bool   ret;

            outStr  = "\n";
            outStr += " + Indexing PDF [ " + p.GenerateURL() + " ]\n";

            nsGlobalOutput.output.write(outStr);

            html htmp = new html();

            pdf2text = htmp.removeUnWantedChars(pdf2text);

            addContent2Index(p._hostID, p._hostname, p._page, p._title, p._anchorText, p._depthLevel, pdf2text, pdf2text);

            sql = "INSERT INTO pdf (host_id, filename, pdf_size, pdf_text) " +
                  "VALUES(" + p._hostID + ", " +
                  "'" + myMySQLEscapeString(p._page) + "', " +
                  "'" + pdfSize + "', " +
                  "'" + myMySQLEscapeString(pdf2text) + "') ";


            GlobalVars.threadsVars.mutexMySQLPageList.WaitOne();
            try
            {
                ret = GlobalVars.mysqlConn.connPageList.executeSQLQuery(sql);
            }
            catch (Exception e)
            {
                nsGlobalOutput.output.write("SQL Error: " + e.Message + "\n\nSQL: -===[\n" + sql.Substring(0, 1000) + "\n]===-\n\n");
                ret = false;
            }
            finally
            {
                GlobalVars.threadsVars.mutexMySQLPageList.ReleaseMutex();
            }

            return(ret);
        }
Example #2
0
        public bool indexThisPage(page p, html h)
        {
            // se negli argomenti è specificato di non indicizzare: esci
            if (GlobalVars.args.noIndex)
            {
                return(false);
            }

            string text2Index = string.Empty;

            try
            {
                // controlla che le regex permettano l'indicizzazione di questa pagina
                // 1. stiamo usano hostlist_extras?
                if (GlobalVars.limits.useHostlist_Extras_limits == true)
                {
                    // 2. c'è una regex valida per le inclusioni?
                    if (GlobalVars.limits.he_regex_include_pages != "")
                    {
                        Regex testIncludePageRegex = new Regex(GlobalVars.limits.he_regex_include_pages, RegexOptions.IgnoreCase);
                        // se la pagina corrente NON VERIFICA l'espressione regolare: non indicizzare
                        if (!testIncludePageRegex.IsMatch(p._page))
                        {
                            return(false);
                        }
                    }

                    // 3. c'è una regex valida per le esclusioni?
                    if (GlobalVars.limits.he_regex_exclude_pages != "")
                    {
                        Regex testExcludePageRegex = new Regex(GlobalVars.limits.he_regex_exclude_pages, RegexOptions.IgnoreCase);
                        // se la pagina corrente VERIFICA l'espressione regolare: non indicizzare
                        if (testExcludePageRegex.IsMatch(p._page))
                        {
                            return(false);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                nsGlobalOutput.output.write("\n\n + Error while parsing hostlist_extras regex: " + e.Message + "\n\n");
            }

            if (p.isValidPage && h.HTML != "")
            {
                if (h.contentType.StartsWith("text/html", StringComparison.CurrentCultureIgnoreCase))
                {
                    text2Index = h.UnHTML(h.HTML);
                }
                else if (h.contentType.StartsWith("text/", StringComparison.CurrentCultureIgnoreCase))
                {
                    // indicizza il testo (es.: *.txt; *.c; *.h)
                    text2Index = h.removeUnWantedChars(h.HTML).Trim();
                }
                else
                {
                    return(false);
                }

                return(addContent2Index(p._hostID, p._hostname, p._page, p._title, p._anchorText, p._depthLevel, text2Index, h.HTML));
            }
            return(false);
        }
Example #3
0
        // indexer vero e proprio
        public void DoWork(object threadID)
        {
            page   pageToIndex;
            string outStr;

            // funzioni per l'accesso al DB
            db __db = new db();

            while (!_shouldStop)
            {
                System.Threading.Thread.Sleep(50);

                System.Threading.Thread.Sleep(0);

                if (GlobalVars.OpenWebSpider.crawlerActPAUSE)
                {
                    // aspetta ulteriore tempo
                    System.Threading.Thread.Sleep(300);

                    continue;
                }

                outStr = string.Empty;

                // nsGlobalOutput.output.write("Thread["+ (int)threadID +"]: working.");

                // blocca l'accesso alla lista degli URL da indicizzare e ne estrae uno ( se disponibile )
                GlobalVars.threadsVars.mutexAccessURLList.WaitOne();

                // cerca una pagina da indicizzare
                // se siamo in stress test mode: usa sempre la prima pagina
                if (GlobalVars.args.stressTest == true)
                {
                    pageToIndex = GlobalVars.threadsVars.currentDomain;
                }
                else
                {
                    pageToIndex = GlobalVars.urlsLists.getPageByStatus(0);
                }

                if (pageToIndex == null)
                {
                    // rilascia il mutex
                    GlobalVars.threadsVars.mutexAccessURLList.ReleaseMutex();
                    // riparte dall'inizio
                    continue;
                }
                else
                {
                    try
                    {
                        // imposta la pagina come "indicizzazione in corso"
                        pageToIndex.isIndexed = 2;

                        // rilascia il mutex
                        GlobalVars.threadsVars.mutexAccessURLList.ReleaseMutex();

                        // indicizza questa pagina!!!
                        html htmlPage = new html();

                        outStr = outStr + "\nT[" + (int)threadID + "] \t Downloading... [ " + pageToIndex.GenerateURL() + " ] [Depth Level: " + pageToIndex._depthLevel + "]\n";

                        // siamo in presenza di un Crawl-Delay ( da robots.txt o da riga di comando)
                        if (GlobalVars.robotstxtVars.robotstxtCrawlDelay > 0 || GlobalVars.args.crawlDelay > 0)
                        {
                            // locka il mutex
                            GlobalVars.threadsVars.mutexCrawlDelay.WaitOne();

                            // aspetta il tempo del Crawl-Delay
                            // se c'è un crawl-delay da robots.txt usa quello; altrimenti quello da riga di comando
                            int crawlDelay2Use = (GlobalVars.robotstxtVars.robotstxtCrawlDelay > 0) ? GlobalVars.robotstxtVars.robotstxtCrawlDelay * 1000 : GlobalVars.args.crawlDelay * 1000;
                            System.Threading.Thread.Sleep(crawlDelay2Use);
                        }

                        long curTick = DateTime.Now.Ticks;

                        // scarica la pagina
                        htmlPage.getURL(pageToIndex, true);

                        // siamo in presenza di un Crawl-Delay ( da robots.txt o da riga di comando)
                        if (GlobalVars.robotstxtVars.robotstxtCrawlDelay > 0 || GlobalVars.args.crawlDelay > 0)
                        {
                            // un-locka il mutex
                            GlobalVars.threadsVars.mutexCrawlDelay.ReleaseMutex();
                        }

                        outStr = outStr + "T[" + (int)threadID + "] \t Downloaded ";
                        if (htmlPage.HTML.Length > 0)
                        {
                            outStr = outStr + htmlPage.HTML.Length / 1024 + " Kb (" + htmlPage.HTML.Length + " bytes)";
                        }
                        outStr = outStr + " in " + ((DateTime.Now.Ticks - curTick) / 10000) + " ms\n";

                        outStr = outStr + "T[" + (int)threadID + "] \t HTTP Status Code: " + htmlPage.statusCode + " -][- Content-Type: " + htmlPage.contentType + "\n";

                        if (htmlPage.errorString != "")
                        {
                            outStr = outStr + "T[" + (int)threadID + "] \t Error: " + htmlPage.errorString + "\n";
                        }

                        // se non stiamo in stress test mode: allora parsa la pagina e indicizza
                        if (GlobalVars.args.stressTest == false)
                        {
                            // se lo status code (HTTP) è 200 (OK) e il content-type è "text/html" :: indicizza!
                            if (htmlPage.statusCode == 200)
                            {
                                if (htmlPage.contentType.StartsWith("text/html"))
                                {
                                    // siamo in una pagina HTML: controlla i META TAG
                                    htmlPage.checkMETA();

                                    // siamo in una pagina HTML: estrai il titolo <title>...</title>
                                    string __title = htmlPage.getTitle();
                                    if (__title != string.Empty)
                                    {
                                        pageToIndex._title = __title;

                                        // controlla che il titolo rientri nel limite del campo sul DB
                                        if (pageToIndex._title.Length > GlobalVars.dbLimits.maxTitleLength)
                                        {
                                            pageToIndex._title = pageToIndex._title.Substring(0, 255);
                                        }

                                        outStr = outStr + "T[" + (int)threadID + "] \t Page Title: " + __title + "\n";
                                    }

                                    curTick = DateTime.Now.Ticks;
                                    outStr  = outStr + "T[" + (int)threadID + "] \t Getting URLs...";
                                    if (htmlPage.META_ROBOTS_FOLLOW)
                                    {
                                        int nValidURLs = htmlPage.GetURLs(htmlPage.HTML, pageToIndex);
                                        outStr = outStr + "OK [" + ((DateTime.Now.Ticks - curTick) / 10000) + " ms]  ( " + nValidURLs + " valid URLs found )\n";
                                    }
                                    else
                                    {
                                        // NOFOLLOW
                                        outStr = outStr + "NOFOLLOW (META ROBOTS)\n";
                                    }
                                }

                                curTick = DateTime.Now.Ticks;
                                outStr  = outStr + "T[" + (int)threadID + "] \t Indexing...";
                                if (htmlPage.META_ROBOTS_INDEX)
                                {
                                    if (__db.indexThisPage(pageToIndex, htmlPage) == true)
                                    {
                                        outStr = outStr + "OK [" + ((DateTime.Now.Ticks - curTick) / 10000) + " ms]\n";
                                    }
                                    else
                                    {
                                        outStr = outStr + "NOT INDEXED [" + ((DateTime.Now.Ticks - curTick) / 10000) + " ms ]\n";
                                    }
                                }
                                else
                                {
                                    // NOINDEX
                                    outStr = outStr + "NOINDEX (META ROBOTS)\n";
                                }
                            }
                            else
                            {
                                GlobalVars.limits.curErrorCodes++;              // TODO: da testare gli error code che arrivano qui
                                Console.WriteLine(" ##  Error code: " + htmlPage.statusCode + "  ## ");
                            }
                        }

                        // incrementa il numero di pagine indicizzate
                        GlobalVars.limits.curPages++;

                        // incrementa i bytes indicizzati
                        GlobalVars.limits.curBytes += htmlPage.HTML.Length;
                    }
                    catch (Exception e)
                    {
                        outStr += "Error: " + e.Message + "\n";
                    }
                    finally
                    {
                        // imposta la pagina come "indicizzata"
                        if (GlobalVars.args.stressTest == false)
                        {
                            pageToIndex.isIndexed = 1;
                        }
                        else
                        {
                            // se siamo in stress-test: la mette come da indicizzare
                            pageToIndex.isIndexed = 0;
                        }

                        nsGlobalOutput.output.write(outStr);
                    }
                }
            }

            nsGlobalOutput.output.write("T[" + (int)threadID + "] \t Worker thread: terminating gracefully.");
        }