Example #1
0
        public void GeneratePdf(string inputPathFile, string outputPathFile)
        {
            if (!CheckFiles())
            {
                InitFiles();
            }

            if (!File.Exists(inputPathFile))
            {
                throw new FileNotFoundException(inputPathFile);
            }

            try
            {
                LogOutput($"Generating pdf from html file {inputPathFile} to {outputPathFile}");

                ExcuteCommand($"python.exe weasyprint.exe {inputPathFile} {outputPathFile} -e utf8");

                LogOutput("Pdf generated successfully");
            }
            catch (Exception ex)
            {
                OnDataError?.Invoke(new OutputEventArgs(ex.ToString()));
            }
        }
        private void LogError(string data)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return;
            }

            OnDataError?.Invoke(new OutputEventArgs(data));

            _trace?.Info($"Error: {data}");
        }
Example #3
0
 private void RaiseOnData(string feed, string topic, byte[] data, bool isImage)
 {
     try
     {
         OnDataReceived?.Invoke(this, new DataReceivedEventArgs(feed, topic, Decode(data), isImage));
     }
     catch (Exception error)
     {
         OnDataError?.Invoke(this, new DataErrorEventArgs(false, feed, topic, isImage, data, error));
     }
 }
Example #4
0
 private void RaiseOnData(string user, IPAddress address, string feed, string topic, BinaryDataPacket[] data, bool isImage)
 {
     try
     {
         OnDataReceived?.Invoke(this, new DataReceivedEventArgs(user, address, feed, topic, Decode(data), isImage));
     }
     catch (Exception error)
     {
         OnDataError?.Invoke(this, new DataErrorEventArgs(false, feed, topic, isImage, data, error));
     }
 }
Example #5
0
        public byte[] GeneratePdf(string htmlText)
        {
            if (!CheckFiles())
            {
                InitFiles();
            }

            byte[] result = null;

            try
            {
                LogOutput("Generating pdf from html text ...");

                var fileName     = $"{Guid.NewGuid().ToString().ToLower()}";
                var dirSeparator = Path.DirectorySeparatorChar;

                var inputFileName  = $"{fileName}.html";
                var outputFileName = $"{fileName}.pdf";

                var inputFullName  = Path.Combine(_libDir, inputFileName);
                var outputFullName = Path.Combine(_libDir, outputFileName);

                File.WriteAllText(Path.Combine(_libDir, inputFileName), htmlText);

                ExcuteCommand($"python.exe weasyprint.exe {inputFileName} {outputFileName} -e utf8");

                result = File.ReadAllBytes(outputFullName);

                if (File.Exists(inputFullName))
                {
                    File.Delete(inputFullName);
                }

                if (File.Exists(outputFullName))
                {
                    File.Delete(outputFullName);
                }

                LogOutput("Pdf generated successfully");
            }
            catch (Exception ex)
            {
                OnDataError?.Invoke(new OutputEventArgs(ex.ToString()));
            }

            return(result);
        }
        public byte[] GeneratePdf(string htmlText)
        {
            byte[] result;

            try
            {
                result = GeneratePdfInternal(htmlText).GetAwaiter().GetResult();

                return(result);
            }
            catch (Exception ex)
            {
                OnDataError?.Invoke(new OutputEventArgs(ex.ToString()));

                throw new WeasyPrintException(ex.Message, ex);
            }
        }
Example #7
0
        public void Publish(string feed, string topic, bool isImage, object data)
        {
            if (feed == null)
            {
                throw new ArgumentNullException(nameof(feed));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            try
            {
                _writeQueue.Add(new MulticastData(feed, topic, isImage, Encode(data)));
            }
            catch (Exception error)
            {
                OnDataError?.Invoke(this, new DataErrorEventArgs(true, feed, topic, isImage, data, error));
            }
        }
Example #8
0
        public void Send(Guid clientId, string feed, string topic, bool isImage, IReadOnlyList <DataPacket> data)
        {
            if (feed == null)
            {
                throw new ArgumentNullException(nameof(feed));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }

            try
            {
                _writeQueue.Add(new UnicastData(clientId, feed, topic, isImage, Encode(data)));
            }
            catch (Exception error)
            {
                OnDataError?.Invoke(this, new DataErrorEventArgs(true, feed, topic, isImage, data, error));
            }
        }
Example #9
0
        public void GeneratePdfFromUrl(string url, string outputFilePath)
        {
            if (!CheckFiles())
            {
                InitFiles();
            }

            try
            {
                LogOutput($"Generating pdf from url {url} ...");

                ExcuteCommand($"python.exe weasyprint.exe {url} {outputFilePath} ");

                LogOutput("Pdf generated successfully");
            }
            catch (Exception ex)
            {
                OnDataError?.Invoke(new OutputEventArgs(ex.ToString()));
            }
        }
Example #10
0
        public override void Run()
        {
            while (!stopping)
            {
                if (inputStream == null)
                {
                    break;
                }

                byte[] data = new byte[Buffer];

                try
                {
                    if (!inputStream.IsDataAvailable())
                    {
                        System.Threading.Thread.Sleep(1);
                    }
                    else if (inputStream.Read(data, 0, data.Length) > 0)
                    {
                        OnDataReceived?.Invoke(data.Decode());
                    }
                    else
                    {
                        break;
                    }
                }
                catch (Exception ex)
                {
                    OnDataError?.Invoke(ex);

                    CloseSocket();

                    break;
                }
            }
        }
Example #11
0
        static string GetFromTmdb(string address, int delayRequest = 0)
        {
            if (delayRequest > 0)
            {
                Thread.Sleep(1000 + delayRequest);
            }

            OnDataSend?.Invoke(address, null);

            var headerCollection = new WebHeaderCollection();

            var request = WebRequest.Create(address) as HttpWebRequest;

            request.KeepAlive     = true;
            request.Method        = "GET";
            request.ContentLength = 0;
            request.Timeout       = 120000;
            request.ContentType   = "application/json";
            request.UserAgent     = UserAgent;

            string strResponse = null;

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response == null)
                {
                    return(null);
                }

                Stream stream = response.GetResponseStream();

                StreamReader reader = new StreamReader(stream);
                strResponse = reader.ReadToEnd();

                headerCollection = response.Headers;

                OnDataReceived?.Invoke(strResponse, response);

                stream.Close();
                reader.Close();
                response.Close();
            }
            catch (WebException wex)
            {
                string errorMessage = wex.Message;
                if (wex.Status == WebExceptionStatus.ProtocolError)
                {
                    var response = wex.Response as HttpWebResponse;

                    string headers = string.Empty;
                    foreach (string key in response.Headers.AllKeys)
                    {
                        headers += string.Format("{0}: {1}, ", key, response.Headers[key]);
                    }
                    errorMessage = string.Format("Protocol Error, Code = '{0}', Description = '{1}', Url = '{2}', Headers = '{3}'", (int)response.StatusCode, response.StatusDescription, address, headers.TrimEnd(new char[] { ',', ' ' }));

                    // check if we got a 429 error code
                    // https://developers.themoviedb.org/3/getting-started/request-rate-limiting

                    if ((int)response.StatusCode == 429)
                    {
                        int retry = 0;
                        int.TryParse(response.Headers["Retry-After"], out retry);

                        errorMessage = string.Format("Request Rate Limiting is in effect, retrying request in {0} seconds. Url = '{1}'", retry, address);

                        OnDataError?.Invoke(errorMessage);

                        return(GetFromTmdb(address, retry * 1000));
                    }
                }

                OnDataError?.Invoke(errorMessage);

                strResponse = null;
            }
            catch (IOException ioe)
            {
                string errorMessage = string.Format("Request failed due to an IO error, Description = '{0}', Url = '{1}', Method = 'GET'", ioe.Message, address);

                OnDataError?.Invoke(ioe.Message);

                strResponse = null;
            }

            return(strResponse);
        }
 private void DataError(object sender, DataErrorEventArgs args)
 {
     OnDataError?.Invoke(this, args);
 }
Example #13
0
        static string GetFromFanartTv(string address)
        {
            OnDataSend?.Invoke(address, null);

            var headerCollection = new WebHeaderCollection();

            var request = WebRequest.Create(address) as HttpWebRequest;

            request.KeepAlive     = true;
            request.Method        = "GET";
            request.ContentLength = 0;
            request.Timeout       = 120000;
            request.ContentType   = "application/json";
            request.UserAgent     = UserAgent;

            // add required headers for authorisation
            request.Headers.Add("api-key", ApiKey);

            if (!string.IsNullOrEmpty(ClientKey))
            {
                request.Headers.Add("client_key", ClientKey);
            }

            string strResponse = null;

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response == null)
                {
                    return(null);
                }

                Stream stream = response.GetResponseStream();

                StreamReader reader = new StreamReader(stream);
                strResponse = reader.ReadToEnd();

                headerCollection = response.Headers;

                OnDataReceived?.Invoke(strResponse, response);

                stream.Close();
                reader.Close();
                response.Close();
            }
            catch (WebException wex)
            {
                string errorMessage = wex.Message;
                if (wex.Status == WebExceptionStatus.ProtocolError)
                {
                    var response = wex.Response as HttpWebResponse;

                    string headers = string.Empty;
                    foreach (string key in response.Headers.AllKeys)
                    {
                        headers += string.Format("{0}: {1}, ", key, response.Headers[key]);
                    }
                    errorMessage = string.Format("Protocol Error, Code = '{0}', Description = '{1}', Url = '{2}', Headers = '{3}'", (int)response.StatusCode, response.StatusDescription, address, headers.TrimEnd(new char[] { ',', ' ' }));
                }

                OnDataError?.Invoke(errorMessage);

                strResponse = null;
            }
            catch (IOException ioe)
            {
                string errorMessage = string.Format("Request failed due to an IO error, Description = '{0}', Url = '{1}', Method = 'GET'", ioe.Message, address);

                OnDataError?.Invoke(ioe.Message);

                strResponse = null;
            }

            return(strResponse);
        }
Example #14
0
 private void LogError(string data)
 {
     OnDataError?.Invoke(new OutputEventArgs(data));
 }