public void Scan(string email, string apiKey)
        {
            Facmng mng    = new Facmng();
            Accmng accmng = new Accmng();
            string path   = @"E:\cscolor\result.txt";
            var    file   = File.CreateText(path);

            file.Close();
            StreamWriter sw = new StreamWriter(path);

            CopyleaksCloud   copyleaks = new CopyleaksCloud(eProduct.Businesses);
            CopyleaksProcess createdProcess;
            ProcessOptions   scanOptions = new ProcessOptions();

            scanOptions.SandboxMode = true; // Sandbox mode --> Read more https://api.copyleaks.com/documentation/headers/sandbox
            ResultRecord[] results;
            try
            {
                #region Login to Copyleaks cloud

                //Console.Write("Login to Copyleaks cloud...");
                copyleaks.Login(email, apiKey);
                //Console.WriteLine("Done!");

                #endregion

                #region Checking account balance

                //Console.Write("Checking account balance...");
                uint creditsBalance = copyleaks.Credits;
                //Console.WriteLine("Done ({0} credits)!", creditsBalance);
                if (!scanOptions.SandboxMode && creditsBalance == 0)
                {
                    MessageBox.Show("ERROR: You do not have enough credits to complete this scan. Your balance is {0}).", Convert.ToString(creditsBalance));

                    Environment.Exit(2);
                }

                #endregion

                #region callbacks

                // add a URL address to get notified using callbacks once the scan results are ready.
                //Read more https://api.copyleaks.com/documentation/headers/http-callback
                //scanOptions.HttpCallback = new Uri("http://callbackurl.com?pid={PID}");
                //scanOptions.InProgressResultsCallback = new Uri("http://callbackurl.com?pid={PID}");

                #endregion

                #region Submitting a new scan process to the server

                // Insert here the URL that you'd like to scan for plagiarism
                createdProcess = copyleaks.CreateByUrl(new Uri("http://cnn.com/"), scanOptions);

                // Insert here the file that you'd like to scan for plagiarism
                Addfac add = new Addfac();
                createdProcess = copyleaks.CreateByFile(new FileInfo(add.ansreturn()), scanOptions);


                //Console.WriteLine("Done (PID={0})!", createdProcess.PID);

                #endregion

                #region Waiting for server's process completion

                // Use this if you are not using callback
                sw.WriteLine("Scanning... ");
                ushort currentProgress;
                while (!createdProcess.IsCompleted(out currentProgress))
                {
                    sw.WriteLine(currentProgress + "%");
                    Thread.Sleep(5000);
                }
                sw.WriteLine("Done.");

                #endregion

                #region Processing finished. Getting results

                results = createdProcess.GetResults();
                if (results.Length == 0)
                {
                    sw.WriteLine("No results.");
                }
                else
                {
                    for (int i = 0; i < results.Length; ++i)
                    {
                        if (results[i].URL != null)
                        {
                            sw.WriteLine("Url: {0}", results[i].URL);
                        }
                        sw.WriteLine("Information: {0} copied words ({1}%)", results[i].NumberOfCopiedWords, results[i].Percents);
                        sw.WriteLine("Comparison report: {0}", results[i].ComparisonReport);
                        //Console.WriteLine("Title: {0}", results[i].Title);
                        //Console.WriteLine("Introduction: {0}", results[i].Introduction);
                        ////Console.WriteLine("Embeded comparison: {0}", results[i].EmbededComparison);
                        //Console.ReadKey();
                    }
                }

                #endregion
            }
            catch (UnauthorizedAccessException)
            {
                sw.WriteLine("Failed!");
                sw.WriteLine("Authentication with the server failed!");
                sw.WriteLine("Possible reasons:");
                sw.WriteLine("* You did not log in to Copyleaks cloud");
                sw.WriteLine("* Your login token has expired");
                Console.ReadKey();
            }
            catch (CommandFailedException theError)
            {
                sw.WriteLine("Failed!");
                sw.WriteLine("*** Error {0}:", theError.CopyleaksErrorCode);
                sw.WriteLine("{0}", theError.Message);
                Console.ReadKey();
            }

            sw.Close();
        }
        static void Main(string[] args)
        {
            // Usage:
            // SampleCode.exe -e "<YOUR_EMAIL>" -k "Your Account Key" --url "http://site.com/your-webpage"
            // OR
            // SampleCode.exe -e "<YOUR_EMAIL>" -k "Your Account Key" --local-document "C:\your-directory\document.doc"

            CommandLineOptions options = new CommandLineOptions();

            if (!CommandLine.Parser.Default.ParseArguments(args, options))
            {
                Environment.Exit(1);
            }
            else if ((options.URL == null && options.LocalFile == null) || (options.URL != null && options.LocalFile != null))
            {
                Console.WriteLine("Error: You can speicfy either a URL or a local document to scan. For more information please enter '--help'.");
                Environment.Exit(1);
            }

            Uri httpCallback = null;

            if (options.HttpCallback != null)
            {
                // Http callback example value:
                // https://your-website.com/copyleaks-gateway?id={PID}
                // Copyleaks server will replace the "{PID}" token with the actual process id.
                if (!Uri.TryCreate(options.HttpCallback, UriKind.Absolute, out httpCallback))
                {
                    Console.WriteLine("ERROR: Bad Http-Callback.");
                    Environment.Exit(3);
                }
            }

            // For more information, visit Copyleaks "How-To page": https://api.copyleaks.com/Guides/HowToUse
            // Creating Copyleaks account: https://copyleaks.com/Account/Register
            // Use your Copyleaks account information.
            // Generate your Account API Key: https://api.copyleaks.com/Home/Dashboard

            // Copyleaks api supports two products: Publishers and Academic.
            // Select the product the suitible for you.
            CopyleaksCloud   copyleaks = new CopyleaksCloud(eProduct.Businesses);
            CopyleaksProcess createdProcess;

            ResultRecord[] results;
            ProcessOptions scanOptions = new ProcessOptions();

            scanOptions.HttpCallback = httpCallback;

            // In Sandbox scan you don't need credits.
            // Read more @ https://api.copyleaks.com/Documentation/RequestHeaders#sandbox-mode
            // After you finish the integration with Copyleaks, remove this line.
            scanOptions.SandboxMode = true;

            try
            {
                #region Login to Copyleaks cloud

                Console.Write("Login to Copyleaks cloud...");
                copyleaks.Login(options.Email, options.ApiKey);
                Console.WriteLine("Done!");

                #endregion

                #region Checking account balance

                Console.Write("Checking account balance...");
                uint creditsBalance = copyleaks.Credits;
                Console.WriteLine("Done ({0} credits)!", creditsBalance);
                if (creditsBalance == 0)
                {
                    Console.WriteLine("ERROR: You do not have enough credits to complete this scan. Your current credit balance is {0}).", creditsBalance);
                    Environment.Exit(2);
                }

                #endregion

                #region Submitting a new scan process to the server

                Console.Write("Creating process...");
                if (options.URL != null)
                {
                    Uri uri;
                    if (!Uri.TryCreate(options.URL, UriKind.Absolute, out uri))
                    {
                        Console.WriteLine("ERROR: The URL ('{0}') is invalid.", options.URL);                         // Bad URL format.
                        Environment.Exit(1);
                    }

                    createdProcess = copyleaks.CreateByUrl(uri, scanOptions);
                }
                else
                {
                    if (!File.Exists(options.LocalFile))
                    {
                        Console.WriteLine("ERROR: The file '{0}' does not exist.", options.LocalFile);                         // Bad URL format.
                        Environment.Exit(1);
                    }

                    createdProcess = copyleaks.CreateByFile(new FileInfo(options.LocalFile), scanOptions);
                }
                Console.WriteLine("Done (PID={0})!", createdProcess.PID);

                #endregion

                #region Waiting for server's process completion

                // Note: We are strongly recommending to use "callbacks" instead of "busy-polling". Use HTTP-callbacks whenever it's possible.
                // Read more @ https://api.copyleaks.com/GeneralDocumentation/RequestHeaders#http-callbacks
                Console.Write("Scanning... ");
                using (var progress = new ProgressBar())
                {
                    ushort currentProgress;
                    while (!createdProcess.IsCompleted(out currentProgress))
                    {
                        progress.Report(currentProgress / 100d);
                        Thread.Sleep(5000);
                    }
                }
                Console.WriteLine("Done.");

                #endregion

                #region Processing finished. Getting results

                results = createdProcess.GetResults();

                if (results.Length == 0)
                {
                    Console.WriteLine("No results.");
                }
                else
                {
                    for (int i = 0; i < results.Length; ++i)
                    {
                        Console.WriteLine();
                        Console.WriteLine("Result {0}:", i + 1);
                        Console.WriteLine("Url: {0}", results[i].URL);
                        Console.WriteLine("Percents: {0}", results[i].Percents);
                        Console.WriteLine("CopiedWords: {0}", results[i].NumberOfCopiedWords);
                    }
                }

                #endregion
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Failed!");
                Console.WriteLine("Authentication with the server failed!");
                Console.WriteLine("Possible reasons:");
                Console.WriteLine("* You did not log in to Copyleaks cloud");
                Console.WriteLine("* Your login token has expired");
                Environment.Exit(1);
            }
            catch (CommandFailedException theError)
            {
                Console.WriteLine("Failed!");
                Console.WriteLine("*** Error {0}:", theError.CopyleaksErrorCode);
                Console.WriteLine("{0}", theError.Message);
                Environment.Exit(1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed!");
                Console.WriteLine("Unhandled Exception");
                Console.WriteLine(ex);
                Environment.Exit(1);
            }

            Environment.Exit(0);             // SUCCESS
        }
Beispiel #3
0
        private void Button1_Click(object sender, EventArgs e)
        {
            string xx       = "";
            string username = "******";
            string APIKey   = "525D9EEF-BC75-4C3D-8E3C-259774EB947A";

            string         url_to_scan = textBox1.Text; // Allowed formats: html, pdf, doc, docx, rtf, txt ...
            CopyleaksCloud copyleaks   = new CopyleaksCloud(eProduct.Education);

            ResultRecord[] results;
            ProcessOptions scanOptions = new ProcessOptions();

            scanOptions.HttpCallback = httpCallback;
            try
            {
                #region Login to Copyleaks cloud

                listBox1.Items.Add("Login to Cloud...");
                copyleaks.Login(username, APIKey);
                listBox1.Items.Add("Done!");

                #endregion

                #region Checking account balance

                //   listBox1.Items.Add("Checking account balance...");
                uint creditsBalance = copyleaks.Credits;
                // listBox1.Items.Add("Done ({" + creditsBalance + " credits)!");
                if (creditsBalance == 0)
                {
                    //   listBox1.Items.Add("ERROR: You do not have enough credits to complete this scan. Your current credit balance is " + creditsBalance);
                    Environment.Exit(2);
                }

                #endregion

                #region Submitting a new scan process to the server

                listBox1.Items.Add("Creating process...");
                if (url_to_scan != null)
                {
                    if (!File.Exists(url_to_scan))
                    {
                        listBox1.Items.Add("ERROR: The file '" + url_to_scan + "' does not exist.");  // Bad URL format.
                        Environment.Exit(1);
                    }

                    scanProcess = copyleaks.CreateByFile(new FileInfo(url_to_scan), scanOptions);

                    xx = scanProcess.PID.ToString();
                }
                else
                {
                    //    if (!File.Exists(options.LocalFile))
                    //    {
                    //        listBox1.Items.Add("ERROR: The file '{0}' does not exist.", options.LocalFile); // Bad URL format.
                    //        Environment.Exit(1);
                    //    }

                    //    scanProcess = copyleaks.CreateByFile(new FileInfo(options.LocalFile), scanOptions);
                }


                listBox1.Items.Add("Done. PID= " + xx);


                #endregion

                #region Waiting for server's process completion

                // Note: We are strongly recommending to use "callbacks" instead of "busy-polling". Use HTTP-callbacks whenever it's possible.
                // Read more @ https://api.copyleaks.com/GeneralDocumentation/RequestHeaders#http-callbacks
                listBox1.Items.Add("Scanning... ");
                using (var progress = new ProgressBar())
                {
                    ushort currentProgress;
                    while (!scanProcess.IsCompleted(out currentProgress))
                    {
                        // progress.Report(currentProgress / 100d);
                        Thread.Sleep(5000);
                    }
                }
                listBox1.Items.Add("Done.");

                #endregion

                #region Processing finished. Getting results

                results = scanProcess.GetResults();

                if (results.Length == 0)
                {
                    listBox1.Items.Add("No results.");
                }
                else
                {
                    for (int i = 0; i < results.Length; ++i)
                    {
                        richTextBox1.AppendText("------------------------------------------------");
                        richTextBox1.AppendText("\n");
                        richTextBox1.AppendText("Url: " + results[i].URL);
                        richTextBox1.AppendText("\n");
                        richTextBox1.AppendText("Information: " + results[i].NumberOfCopiedWords + " copied words (" + results[i].Percents + "%)");
                        richTextBox1.AppendText("\n");

                        //  richTextBox1.AppendText("Comparison report: " + results[i].ComparisonReport);
                        richTextBox1.AppendText("\n");
                        richTextBox1.AppendText("Title: " + results[i].Title);
                        richTextBox1.AppendText("\n");
                        richTextBox1.AppendText("Introduction: " + results[i].Introduction);
                        richTextBox1.AppendText("\n");
                        if (results[i].URL != null)
                        {
                            richTextBox1.AppendText("Url: " + results[i].URL);
                        }
                        richTextBox1.AppendText("\n");
                        //  richTextBox1.AppendText("Embeded comparison: " + results[i].EmbededComparison);
                        richTextBox1.AppendText("\n");

                        #region Optional: Download result full text. Uncomment to activate

                        //using (var stream = scanProcess.DownloadResultText(results[i]))
                        //using (var sr = new StreamReader(stream, Encoding.UTF8))
                        //{
                        //	string resultFullText = sr.ReadToEnd();
                        //	resultFullText = WebUtility.HtmlDecode(resultFullText); // Decode the text. Treat it like HTML.
                        //	listBox1.Items.Add("Result full-text:");
                        //	listBox1.Items.Add("*****************");
                        //	listBox1.Items.Add(resultFullText);
                        //}

                        #endregion

                        #region Optional: Download comparison report. Uncomment to activate

                        //ComparisonReport report = scanProcess.DownloadResultComparison(results[i]);

                        #endregion
                    }
                }

                #endregion

                #region Optional: Download source full text. Uncomment to activate.

                //using (var stream = scanProcess.DownloadSourceText())
                //using (var sr = new StreamReader(stream, Encoding.UTF8))
                //{
                //	string sourceFullText = sr.ReadToEnd();
                //	sourceFullText = WebUtility.HtmlDecode(sourceFullText); // Decode the text. Treat it like HTML.
                //	listBox1.Items.Add("Source full-text:");
                //	listBox1.Items.Add("*****************");
                //	listBox1.Items.Add(sourceFullText);
                //}

                #endregion
            }
            catch (UnauthorizedAccessException)
            {
                listBox1.Items.Add("Failed!");
                listBox1.Items.Add("Authentication with the server failed!");
                listBox1.Items.Add("Possible reasons:");
                listBox1.Items.Add("* You did not log in to cloud");
                listBox1.Items.Add("* Your login token has expired");
                Environment.Exit(1);
            }
            catch (CommandFailedException theError)
            {
                listBox1.Items.Add("Failed!");
                listBox1.Items.Add("*** Error {0}:" + theError.CopyleaksErrorCode);
                listBox1.Items.Add("{0}" + theError.Message);
                Environment.Exit(1);
            }
            catch (Exception ex)
            {
                listBox1.Items.Add("Failed!");
                listBox1.Items.Add("Unhandled Exception");
                listBox1.Items.Add(ex);
            }
        }