Пример #1
0
        public void QuerySubByVideoPathOrHash(string filePath, string fileHash, string vHash, string langId)
        {
            UpdateProgress(0);

            FormData formData = new FormData();

            formData.Boundary = _boundary;
            formData.AddData("pathinfo", filePath);
            formData.AddData("filehash", fileHash);

            string formDataString = formData.ToString();

            byte[]         formDataUtf8 = Encoding.UTF8.GetBytes(formDataString);
            HttpWebRequest request      = WebRequest.Create(_queryUrl) as HttpWebRequest;

            request.Method        = "POST";
            request.UserAgent     = _userAgent;
            request.ContentType   = _contentType;
            request.ContentLength = formDataUtf8.Length;
            request.Timeout       = _httpTimeout;
            Stream requestStream = null;
            bool   bReqOk        = false;

            try
            {
                requestStream = request.GetRequestStream();
                requestStream.Write(formDataUtf8, 0, formDataUtf8.Length);
                requestStream.Flush();
                requestStream.Close();
                bReqOk = true;
            }
            catch (Exception ex)
            {
                UpdateProgress(ShooterConst.Error);
                LogMan.Instance.Log(Resources.ErrHttpRequest, _videoFileName, ex.Message);
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Close();
                }
            }

            string tempFilePath = null;
            bool   bRespOk      = false;

            if (bReqOk)
            {
                UpdateProgress(50);

                HttpWebResponse response       = null;
                Stream          responseStream = null;
                FileStream      outputStream   = null;

                tempFilePath = Path.GetTempFileName();

                try
                {
                    response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        responseStream = response.GetResponseStream();
                        byte[] buffer = new byte[1024];
                        int    len    = 0;
                        outputStream = new FileStream(tempFilePath, FileMode.OpenOrCreate);
                        while ((len = responseStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            outputStream.Write(buffer, 0, len);
                        }
                        bRespOk = true;
                    }
                    else
                    {
                        LogMan.Instance.Log(Resources.ErrHttpResponse, _videoFileName, response.StatusCode.ToString());
                    }
                }
                catch (Exception ex)
                {
                    UpdateProgress(ShooterConst.Error);
                    LogMan.Instance.Log(Resources.ErrHttpResponse, _videoFileName, ex.Message);
                }
                finally
                {
                    if (outputStream != null)
                    {
                        outputStream.Close();
                    }
                    if (response != null)
                    {
                        response.Close();
                    }
                    if (responseStream != null)
                    {
                        responseStream.Close();
                    }
                }
            }

            if (bRespOk)
            {
                //Update progress
                UpdateProgress(90);

                //Extract subtitle
                ShooterSubExtractor extractor = new ShooterSubExtractor();
                extractor.VideoFilePath = filePath;
                extractor.DumpFilePath  = tempFilePath;
                ShooterSubExtractor.SubExtractResult result = extractor.ExtractSubtitles();

                if (result == ShooterSubExtractor.SubExtractResult.OK)
                {
                    UpdateProgress(100);
                }
                else if (result == ShooterSubExtractor.SubExtractResult.NoSubFound)
                {
                    UpdateProgress(ShooterConst.NoSubFound);
                }
                else
                {
                    //error
                    UpdateProgress(ShooterConst.Error);
                }

                File.Delete(tempFilePath);
                LogMan.Instance.Log(Resources.InfoSubDownloadOk, _videoFileName);
            }
        }
        public void QuerySubByVideoPathOrHash(string filePath, string fileHash, string vHash, string langId)
        {
            UpdateProgress(0);

            FormData formData = new FormData();
            formData.Boundary = _boundary;
            formData.AddData("pathinfo", filePath);
            formData.AddData("filehash", fileHash);

            string formDataString = formData.ToString();

            byte[] formDataUtf8 = Encoding.UTF8.GetBytes(formDataString);
            HttpWebRequest request = WebRequest.Create(_queryUrl) as HttpWebRequest;
            request.Method = "POST";
            request.UserAgent = _userAgent;
            request.ContentType = _contentType;
            request.ContentLength = formDataUtf8.Length;
            request.Timeout = _httpTimeout;
            Stream requestStream = null;
            bool bReqOk = false;
            try
            {
                requestStream = request.GetRequestStream();
                requestStream.Write(formDataUtf8, 0, formDataUtf8.Length);
                requestStream.Flush();
                requestStream.Close();
                bReqOk = true;
            }
            catch (Exception ex)
            {
                UpdateProgress(ShooterConst.Error);
                LogMan.Instance.Log(Resources.ErrHttpRequest, _videoFileName, ex.Message);
            }
            finally
            {
                if (requestStream != null)
                    requestStream.Close();
            }

            string tempFilePath = null;
            bool bRespOk = false;

            if (bReqOk)
            {
                UpdateProgress(50);

                HttpWebResponse response = null;
                Stream responseStream = null;
                FileStream outputStream = null;
                
                tempFilePath = Path.GetTempFileName();

                try
                {
                    response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        responseStream = response.GetResponseStream();
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        outputStream = new FileStream(tempFilePath, FileMode.OpenOrCreate);
                        while ((len = responseStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            outputStream.Write(buffer, 0, len);
                        }
                        bRespOk = true;
                    }
                    else
                    {
                        LogMan.Instance.Log(Resources.ErrHttpResponse, _videoFileName, response.StatusCode.ToString());
                    }
                }
                catch (Exception ex)
                {
                    UpdateProgress(ShooterConst.Error);
                    LogMan.Instance.Log(Resources.ErrHttpResponse, _videoFileName, ex.Message);
                }
                finally
                {
                    if(outputStream != null)
                        outputStream.Close();
                    if(response != null)
                        response.Close();
                    if(responseStream != null)
                        responseStream.Close();
                }
            }

            if (bRespOk)
            {
                //Update progress
                UpdateProgress(90);

                //Extract subtitle
                ShooterSubExtractor extractor = new ShooterSubExtractor();
                extractor.VideoFilePath = filePath;
                extractor.DumpFilePath = tempFilePath;
                ShooterSubExtractor.SubExtractResult result = extractor.ExtractSubtitles();

                if (result == ShooterSubExtractor.SubExtractResult.OK)
                {
                    UpdateProgress(100);
                }
                else if (result == ShooterSubExtractor.SubExtractResult.NoSubFound)
                {
                    UpdateProgress(ShooterConst.NoSubFound);
                }
                else
                {
                    //error
                    UpdateProgress(ShooterConst.Error);
                }

                File.Delete(tempFilePath);
                LogMan.Instance.Log(Resources.InfoSubDownloadOk, _videoFileName);
            }
        }