Exemplo n.º 1
0
        /* indexMP3
         * indicizza le informazioni estratte dal file MP3
         */
        public bool indexMP3(page p, mp3 MP3Info)
        {
            string outStr;
            string sql;
            bool   ret;

            outStr  = "\n";
            outStr += " + Indexing MP3 [ " + p.GenerateURL() + " ]\n";
            outStr += "   - Title   : " + MP3Info.mp3Title + "\n";
            outStr += "   - Artist  : " + MP3Info.mp3Artist + "\n";
            outStr += "   - Album   : " + MP3Info.mp3Album + "\n";
            outStr += "   - Genre   : " + MP3Info.mp3Genre + "\n";
            outStr += "   - Duration: " + MP3Info.mp3Length + " seconds\n";
            outStr += "\n";

            nsGlobalOutput.output.write(outStr);

            sql = "INSERT INTO mp3 (host_id, filename, mp3_size, mp3_artist, mp3_title, mp3_album, mp3_genre, mp3_duration) " +
                  "VALUES(" + p._hostID + ", " +
                  "'" + myMySQLEscapeString(p._page) + "', " +
                  "'" + MP3Info.mp3Size + "', " +
                  "'" + myMySQLEscapeString(MP3Info.mp3Artist) + "', " +
                  "'" + myMySQLEscapeString(MP3Info.mp3Title) + "', " +
                  "'" + myMySQLEscapeString(MP3Info.mp3Album) + "', " +
                  "'" + myMySQLEscapeString(MP3Info.mp3Genre) + "', " +
                  MP3Info.mp3Length + ")";


            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);
        }
Exemplo n.º 2
0
        public string getURL(string URL, page p, bool followRedirects)
        {
            errorString = String.Empty;
            contentType = String.Empty;
            statusCode  = 0;
            HTML        = String.Empty;

            try
            {
                HttpWebRequest request = HttpWebRequest.Create(URL) as HttpWebRequest;

                // imposta l'user-agent
                request.UserAgent = GlobalVars.OpenWebSpider.USERAGENT;

                // imposta il timeout (default: un minuto 60.000 ms)
                request.Timeout = GlobalVars.args.reqTimeout * 1000;

                // segue i redirect
                if (followRedirects)
                {
                    request.AllowAutoRedirect            = true;
                    request.MaximumAutomaticRedirections = 5;
                }
                else
                {
                    request.AllowAutoRedirect = false;
                }

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (response.CharacterSet == null)
                {
                    return(HTML);
                }

                // Support to Encodings
                Encoding responseEncoding;
                responseEncoding = Encoding.UTF8;   //default UTF-8
                if (response.CharacterSet.Trim() != "")
                {
                    responseEncoding = Encoding.GetEncoding(response.CharacterSet);
                }

                StreamReader sr           = new StreamReader(response.GetResponseStream(), responseEncoding);
                BinaryReader binaryStream = new BinaryReader(response.GetResponseStream());

                statusCode = (int)response.StatusCode;

                contentType = response.Headers["Content-Type"];

                // il content-type di questa pagina è testo?
                if (contentType.StartsWith("text", StringComparison.CurrentCultureIgnoreCase))
                {
                    HTML = sr.ReadToEnd();
                }
                else if (GlobalVars.args.indexMP3 && contentType.ToLower() == "audio/mpeg")
                {
                    // se sappiamo chi è il padre e ha un hostID valido allora indicizza l'MP3
                    if (p != null)
                    {
                        if (p.isValidPage && p._hostID > 0)
                        {
                            string fullPath = binaryStream2MD5File(p, binaryStream, response.Headers["Content-Type"], response.Headers["Content-Length"]);

                            mp3 MP3 = new mp3(fullPath);
                            deleteFile(fullPath);

                            MP3.mp3Size = int.Parse(response.Headers["Content-Length"]);

                            db __db = new db();
                            __db.indexMP3(p, MP3);
                        }
                    }
                }
                else if (GlobalVars.args.indexPDF && contentType.ToLower() == "application/pdf")
                {
                    // se sappiamo chi è il padre e ha un hostID valido allora indicizza il PDF
                    if (p != null)
                    {
                        if (p.isValidPage && p._hostID > 0)
                        {
                            string fullPath = binaryStream2MD5File(p, binaryStream, response.Headers["Content-Type"], response.Headers["Content-Length"]);

                            pdf PDF = new pdf(fullPath);
                            deleteFile(fullPath);

                            db __db = new db();
                            __db.indexPDF(p, int.Parse(response.Headers["Content-Length"]), PDF.pdfText);
                        }
                    }
                }
                else
                {
                    HTML = string.Empty;
                }

                // forza l'encoding corrente a UTF-8
                Encoding utf8 = Encoding.Unicode;

                byte[] responseEncodingBytes = responseEncoding.GetBytes(HTML);

                byte[] utf8Bytes = Encoding.Convert(responseEncoding,
                                                    utf8,
                                                    responseEncodingBytes);

                HTML = utf8.GetString(utf8Bytes);

                sr.Close();
            }
            catch (WebException e)
            {
                // TODO: in caso di 404 leggere ugualmente lo stream e ritornare l'HTML

                HttpWebResponse response = (HttpWebResponse)e.Response;
                if (response != null)
                {
                    // in caso di eccezione: prova a recuperare da qui lo status code
                    statusCode = (int)response.StatusCode;

                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        string challenge = null;
                        challenge = response.GetResponseHeader("WWW-Authenticate");
                        if (challenge != null)
                        {
                            errorString = "The following challenge was raised by the server:" + challenge;
                        }
                    }
                    else
                    {
                        errorString = "The following WebException was raised : " + e.Message;
                    }
                }
                else
                {
                    errorString = "Response Received from server was null";
                }
            }
            catch (Exception e)
            {
                errorString = "The following Exception was raised :" + e.Message;
            }

            return(HTML);
        }