Exemple #1
0
        private void ConfigureAspNet(IServiceCollection services)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            services.AddHttpContextAccessor();
            services.AddResponseCompression(x => x.EnableForHttps = true);
            services.AddGrpc();
            services.AddMvc().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.NullValueHandling     = NullValueHandling.Include;
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Formatting            = IsProduction ? Formatting.None : Formatting.Indented;
            });

            services.AddWebEncoders(o =>
            {
                var textEncoderSettings = new TextEncoderSettings();
                textEncoderSettings.AllowRange(UnicodeRanges.All);
                o.TextEncoderSettings = textEncoderSettings;
            });
            services.AddLogging();
            services.AddSession();
            services.AddControllers().AddNewtonsoftJson();
            services.AddAuthorization();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(x =>
            {
                x.AccessDeniedPath = "/Auth";
                x.LoginPath        = "/Auth";
                x.LogoutPath       = "/Auth/Logout";
            });
        }
        public static void Run()
        {
            string          jsonString;
            WeatherForecast weatherForecast =
                WeatherForecastFactories.CreateWeatherForecastCyrillic();

            weatherForecast.DisplayPropertyValues();

            Console.WriteLine("Default serialization - non-ASCII escaped");
            var options = new JsonSerializerOptions
            {
                WriteIndented = true
            };

            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            Console.WriteLine(jsonString);
            Console.WriteLine();

            Console.WriteLine("Serialize language sets unescaped");
            // <LanguageSets>
            options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic),
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </LanguageSets>
            Console.WriteLine(jsonString);
            Console.WriteLine();

            Console.WriteLine("Serialize selected unescaped characters");
            // <SelectedCharacters>
            var encoderSettings = new TextEncoderSettings();

            encoderSettings.AllowCharacters('\u0436', '\u0430');
            encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
            options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.Create(encoderSettings),
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </SelectedCharacters>
            Console.WriteLine(jsonString);
            Console.WriteLine();

            Console.WriteLine("Serialize using unsafe relaxed encoder");
            // <UnsafeRelaxed>
            options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
                WriteIndented = true
            };
            jsonString = JsonSerializer.Serialize(weatherForecast, options);
            // </UnsafeRelaxed>
            Console.WriteLine(jsonString);
        }
Exemple #3
0
        public JsonSerializerOptions GetJsonSerializerOptions(
            bool writeIndented            = false,
            JsonNamingPolicy?namingPolicy = null)
        {
            var encoderSettings = new TextEncoderSettings();

            // Additional options are needed to correct serialization of polish diacritic letters.
            encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
            encoderSettings.AllowRange(UnicodeRanges.Latin1Supplement);
            encoderSettings.AllowRange(UnicodeRanges.LatinExtendedA);
            var options = new JsonSerializerOptions()
            {
                WriteIndented = writeIndented,
                Encoder       = JavaScriptEncoder.Create(encoderSettings)
            };

            if (namingPolicy != null)
            {
                options.PropertyNamingPolicy = namingPolicy;
            }

            return(options);
        }
        static JsonSerialisation()
        {
            var encoderSettings = new TextEncoderSettings();

            //encoderSettings.AllowCharacter('u0027');
            //encoderSettings.AllowCharacter('u0022');
            encoderSettings.AllowRange(UnicodeRanges.BasicLatin);

            Options = new JsonSerializerOptions
            {
                WriteIndented = true,
                Encoder       = JavaScriptEncoder.UnsafeRelaxedJsonEscaping //JavaScriptEncoder.Create(encoderSettings)
            };
        }
        public async Task StorageTranslatedWordAsync(List <Word> wordList, string path)
        {
            var encoderSettings = new TextEncoderSettings();

            encoderSettings.AllowCharacters('\u0027'); //Invalid
            encoderSettings.AllowRange(UnicodeRanges.All);
            var options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.Create(encoderSettings),
                WriteIndented = true
            };
            string txt = JsonSerializer.Serialize(wordList, options);

            txt = txt.Replace("\\u0027", "'");
            await File.WriteAllTextAsync(path, txt);
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            var encoderSettings = new TextEncoderSettings();

            encoderSettings.AllowRange(UnicodeRanges.All);
            encoderSettings.AllowCharacters(':');

            services.AddControllers().AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
            });

            services.AddSingleton <IImageTableStorage, ImageTableStorage>();
            services.AddSingleton <IUserNameProvider, UserNameProvider>();
            services.AddSingleton <ICloudStorageAccountProvider, CloudStorageAccountProvider>();
            services.AddSingleton <IConnectionStringProvider, ConnectionStringProvider>();
        }
Exemple #7
0
        public static string Serialize(object obj)
        {
            // Reuse options, https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-configure-options?pivots=dotnet-5-0
            // https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-character-encoding
            var encoderSettings = new TextEncoderSettings();

            // ä, Ö, å, Ä, ö
            encoderSettings.AllowCharacters('\u00E4', '\u00D6', '\u00E5', '\u00C4', '\u00F6');
            encoderSettings.AllowRange(UnicodeRanges.BasicLatin);

            var options = new JsonSerializerOptions
            {
                Encoder = JavaScriptEncoder.Create(encoderSettings)
            };

            return(JsonSerializer.Serialize(obj, options));
        }
Exemple #8
0
        public void ConfigureServices(IServiceCollection services)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            services.AddMvc();
            services.AddSingleton <Chrome>();
            services.AddSingleton <ScrapeService>();

            var connectionString    = Configuration.GetConnectionString("AzureStorage");
            var cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
            var keysStorage         = cloudStorageAccount.CreateCloudBlobClient().GetContainerReference("xmlkeys");

            keysStorage.CreateIfNotExistsAsync().GetAwaiter().GetResult();

            var scrapers = GetType().Assembly.GetTypes().Where(v => v.IsSubclassOf(typeof(GenericScraper))).ToList();

            foreach (var s in scrapers)
            {
                services.AddTransient(typeof(GenericScraper), s);
            }

            services.AddWebEncoders(o =>
            {
                var textEncoderSettings = new TextEncoderSettings();
                textEncoderSettings.AllowRange(UnicodeRanges.All);
                o.TextEncoderSettings = textEncoderSettings;
            });
            services.AddSingleton(_ => cloudStorageAccount);
            services.AddSingleton(_ => _.GetRequiredService <CloudStorageAccount>().CreateCloudBlobClient());
            services.AddSingleton(_ => _.GetRequiredService <CloudStorageAccount>().CreateCloudTableClient());
            services.AddSingleton <IStorage, AzureTableContext>();
            services.AddSingleton <ObjectRepository>();
            services.AddSingleton <ScriptService>();
            services.AddSingleton <SmsRuleProcessor>();
            services.AddLogging();
            services.AddSession();
            services.AddHangfire(x => { });
            services.AddDataProtection().PersistKeysToAzureBlobStorage(keysStorage, "keys.xml");
            services.AddAuthorization();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(x =>
            {
                x.AccessDeniedPath = "/Auth";
                x.LoginPath        = "/Auth";
                x.LogoutPath       = "/Auth/Logout";
            });
        }
        public void AllowRange()
        {
            // Arrange
            var filter = new TextEncoderSettings();

            filter.AllowRange(UnicodeRanges.LatinExtendedA);

            // Assert
            for (int i = 0; i < 0x0100; i++)
            {
                Assert.False(filter.IsCharacterAllowed((char)i));
            }
            for (int i = 0x0100; i <= 0x017F; i++)
            {
                Assert.True(filter.IsCharacterAllowed((char)i));
            }
            for (int i = 0x0180; i <= char.MaxValue; i++)
            {
                Assert.False(filter.IsCharacterAllowed((char)i));
            }
        }
        public void AllowRange_NullRange()
        {
            TextEncoderSettings filter = new TextEncoderSettings();

            Assert.Throws <ArgumentNullException>("range", () => filter.AllowRange(null));
        }
Exemple #11
0
        static async System.Threading.Tasks.Task <int> Main(string[] args)
        {
            PaulsOKWrapper github;
            // Processors not cores, probably need something else in the future
            int ProcessorCount = Environment.ProcessorCount;

            int NumThreadsToRun = ThreadsPerProcessor * ProcessorCount;

            if (args.Length < 3)
            {
                Console.WriteLine("need atleast 3 args");
                return(1);
            }
            string[] extensionList = new string[] { ".jpg", ".png", ".jpeg", ".JPG", ".PNG", ".JPEG", ".bmp", ".BMP" };
            System.IO.DirectoryInfo ImagesDirectory;
            System.IO.FileInfo      fi;


            var CurrentBranch  = "master";
            var GHPages        = "gh-pages";
            var AutoMergeLabel = "automerge";
            var Repo           = "Repo";
            var Owner          = "Owner";

            var ImgDir   = args[0];
            var jsonPath = args[1] + "\\" + Jekyll_data_Folder + "\\" + Jekyll_data_File;
            var repopath = args[1];
            var domain   = args[2];

            if (ImgDir is string)
            {
                ImagesDirectory = new System.IO.DirectoryInfo(ImgDir);
            }
            else if (ImgDir as String != null)
            {
                ImagesDirectory = new System.IO.DirectoryInfo(ImgDir as string);
            }
            else
            {
                Console.WriteLine("First Arg must be a directory");
                return(1);
            }

            var GitHubStuff = false;

            if (args.Length >= 4 && args[3] is string && bool.Parse(args[3]))
            {
                GitHubStuff = bool.Parse(args[3]);
            }

            github = new PaulsOKWrapper(GitHubStuff);
            if (GitHubStuff)
            {
                github.AttemptLogin();
            }

            bool CommitContainedImages = true;

            if (GitHubStuff && github.CleanlyLoggedIn)
            {
                {// Setup the Owner and the Repo
                    Repo  = args[4] as string;
                    Owner = args[5] as string;
                    github.SetOwnerAndRepo(Owner, Repo);
                }
                {// Setup for merge between branches by label
                    if (args.Length >= 7 && args[6] is string)
                    {
                        CurrentBranch = args[6] as string;
                    }
                    if (args.Length >= 8 && args[7] is string)
                    {
                        GHPages = args[7] as string;
                    }
                    if (args.Length >= 9 && args[8] is string)
                    {
                        AutoMergeLabel = args[8] as string;
                    }

                    github.SetupForMergesByLabel(AutoMergeLabel, CurrentBranch, GHPages);

                    bool commitsetup = await github.SetupCommit();

                    Console.WriteLine("Setup to commit changes: " + commitsetup.ToString());

                    CommitContainedImages = await github.CommitContainedImages(ImagesDirectory);
                }
            }
            if (repopath is string || repopath as string != null)
            {
                github.RepoDirectory = new System.IO.DirectoryInfo(repopath as string);
            }
            else
            {
                Console.WriteLine("Second Arg must be a directory");
                return(1);
            }

            if (jsonPath is string)
            {
                fi = new System.IO.FileInfo(jsonPath);
            }
            else if (jsonPath as String != null)
            {
                fi = new System.IO.FileInfo(jsonPath as string);
            }
            else
            {
                Console.WriteLine("Second Arg must be a directory that can lead to " + "\\" + Jekyll_data_Folder + "\\" + Jekyll_data_File);
                return(1);
            }
            if (!fi.Directory.Exists)
            {
                fi.Directory.Create();
            }
            if (ImagesDirectory.Exists == false) // VSCode keeps offering to "fix this" for me...
            {
                Console.WriteLine("Directory [" + ImgDir + "] Doesn't exist");
                return(1);
            }
            if (!(domain is string))
            {
                Console.WriteLine("arg 3 needs to be your domain");
            }

            int successfull = 0;

            if (CommitContainedImages)
            {
                int NumCommitted = 0;

                DirectoryDescriptor DD = new DirectoryDescriptor(ImagesDirectory.Name, ImagesDirectory.FullName);

                // Traverse Image Directory
                ImageHunter ih = new ImageHunter(ref DD, ImagesDirectory, extensionList);

                string v = ih.NumberImagesFound.ToString();
                Console.WriteLine(v + " Images Found");

                var ImagesList = ih.ImageList;

                System.IO.DirectoryInfo thumbnail = new System.IO.DirectoryInfo(ImgDir + "\\" + THUMBNAILS);

                if (!thumbnail.Exists)
                {
                    thumbnail.Create();
                }

                Console.WriteLine("Images to be resized");

                ImageResizer ir = new ImageResizer(thumbnail, 256, 256, 1024, 1024, true, true);

                //@@ Add Multithreading via thread pool here
                foreach (ImageDescriptor id in ImagesList)
                {
                    try{
                        System.Console.WriteLine("Image: " + id.Name);
                        id.FillBasicInfo();

                        if (ir.ThumbnailNeeded(id))
                        {
                            var increase_count = ir.GenerateThumbnail(id, github);
                            if (github.DoGitHubStuff && increase_count)
                            {
                                ++NumCommitted;
                            }
                        }

                        if (ir.NeedsResize(id))
                        { // when our algorithm gets better, or or image sizes change
                            var increase_count = ir.ResizeImages(id, github);
                            if (github.DoGitHubStuff && increase_count)
                            {
                                ++NumCommitted;
                            }
                        }
                    }catch (Exception ex) {
                        Console.WriteLine(ex.ToString());
                    }
                }
                Console.WriteLine("Images have been resized");

                Console.WriteLine("fixing up paths");
                DD.FixUpPaths(github.RepoDirectory);


                github.SetNumberImagesCommitted(NumCommitted);

                if (github.DoGitHubStuff && NumCommitted >= 1) // Need more than 1 changed file to try and commit things
                {
                    Console.WriteLine(NumCommitted + " Images were generated, now to push to push them to master");

                    int RateLimitCallsLeft = github.DecrementAPICallsBy(0);
                    if ((NumCommitted * 2) > RateLimitCallsLeft)
                    {
                        Console.WriteLine();
                        Console.WriteLine("WARNING!!!!");
                        Console.WriteLine();
                        Console.WriteLine("WARNING!!!!");
                        Console.WriteLine();
                        Console.WriteLine("WARNING!!!!");
                        Console.WriteLine();
                        Console.WriteLine("GitHub API Will rate limit to 1000 calls an hour. you are adding " + NumCommitted + " files and you have " + RateLimitCallsLeft + " API calls left");
                        Console.WriteLine();
                        Console.WriteLine("WARNING!!!!");
                        Console.WriteLine();
                        Console.WriteLine("WARNING!!!!");
                        Console.WriteLine();
                        Console.WriteLine("WARNING!!!!");
                        Console.WriteLine();
                    }

                    //https://laedit.net/2016/11/12/GitHub-commit-with-Octokit-net.html
                    if (!await github.CommitAndPush())
                    {
                        successfull = 2;
                    }

                    Console.WriteLine(" --- ");
                }
                else
                {
                    Console.WriteLine("No images were altered, nothing to push to main");
                }

                Console.WriteLine("Domain Set to: " + domain);

                DD.SaveMDFiles(domain, ImagesDirectory, github); //This follows another path...
                Console.WriteLine("Image indexes written");



                var encoderSettings = new TextEncoderSettings();
                encoderSettings.AllowCharacters('\u0436', '\u0430');
                encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
                var options = new JsonSerializerOptions
                {
                    IgnoreReadOnlyProperties = false,
                    WriteIndented            = true,
                    IgnoreNullValues         = false,
                    //Encoder = JavaScriptEncoder.Create(UnicodeRanges.BasicLatin, UnicodeRanges.Cyrillic),
                };
                var jsonString = JsonSerializer.Serialize <DirectoryDescriptor>(DD, options);
                {
                    var fs = fi.Create();
                    System.IO.TextWriter tw = new System.IO.StreamWriter(fs);
                    tw.Write(jsonString);
                    tw.Close();
                    //fs.Close();
                }
                Console.WriteLine("Json written");

                if (github.DoGitHubStuff && NumCommitted >= 1)
                {
                    Console.WriteLine("Committing Json files");

                    await github.ImmediatlyAddorUpdateTextFile(fi);

                    System.IO.FileInfo NewPath = new System.IO.FileInfo(args[1] + "\\_includes\\gallery.json");
                    if (NewPath.Exists)
                    {
                        NewPath.Delete();
                    }
                    System.IO.File.Copy(fi.FullName, NewPath.FullName);

                    await github.ImmediatlyAddorUpdateTextFile(NewPath);

                    Console.WriteLine("Json files Committed");
                    Console.WriteLine(" --- ");
                }
                else
                {
                    Console.WriteLine("No Images Altered, No Json files Committed");
                    Console.WriteLine(" --- ");
                }

                // do we need a synronication point here? lots of things could be going on in parallel
                // DO NOT TRY AND GET CLEVER AND || your github api usage. it will end in tears
                // if you are looking at this call, you are about to do something stupid, don't do it.
                await github.SyncPoint(true);
            }

            bool APICALLSAVING = true; //code that we are only doing because we have 1k API calls an hour

            if (github.DoGitHubStuff)
            {
                if (!github.CleanlyLoggedIn)
                {
                    Console.WriteLine("GitHub Login State unclear, bad things may happen");
                }

                string PRname = "From " + CurrentBranch + " to " + GHPages;

                bool StalePRSuccess = await github.FindStalePullRequests(PRname);

                if (!StalePRSuccess && successfull == 0)
                {
                    successfull = 3;
                }

                if (!APICALLSAVING && StalePRSuccess)
                {
                    //would prefer to close out and re-open, but need to save API Calls
                    Console.WriteLine("add Attempt to close stale pull request here");
                    StalePRSuccess = await github.CloseStalePullRequests(PRname);
                }


                if (APICALLSAVING)
                {
                    // would prefer to close PR"s and re-open, but gotta save them API calls,
                    //    only get 1k an hour
                    if (!StalePRSuccess)
                    {
                        bool CreatePRSuccess = await github.CreateAndLabelPullRequest(PRname);

                        if (!CreatePRSuccess && successfull == 0)
                        {
                            successfull = 4;
                        }
                    }
                    else
                    {
                        Console.WriteLine("PR Already exists, so not creating one - saving API calls");
                    }
                }
                else
                {
                    bool CreatePRSuccess = await github.CreateAndLabelPullRequest(PRname);

                    if (!CreatePRSuccess && successfull == 0)
                    {
                        successfull = 4;
                    }
                }

                github.RefreshRateLimits();
                Console.WriteLine("Run has finished Exiting...");
                Console.WriteLine("Current Code is : " + successfull.ToString());
            }

            github.PrintRateLimits();
            if (successfull == 3 || successfull == 4)
            {// let's not say we failed in these cases.
                return(0);
            }
            return(successfull);
        }
Exemple #12
0
        public void ConfigureServices(IServiceCollection services)
        {
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            services.AddResponseCompression(x => x.EnableForHttps = true);
            services.AddMvc().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.NullValueHandling     = NullValueHandling.Include;
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Formatting            = IsProduction ? Formatting.None : Formatting.Indented;
            });
            services.AddSingleton <Chrome>();
            services.AddSingleton <ScrapeService>();

            ObjectRepository objectRepository;
            {
                var liteDb  = Configuration.GetConnectionString("LiteDb");
                var azureDb = Configuration.GetConnectionString("AzureStorage");

                IStorage storage = null;

                if (!String.IsNullOrEmpty(liteDb))
                {
                    var connectionString = new ConnectionString(liteDb);
                    DbFileName = connectionString.Filename;
                    var liteDbDatabase = new LiteDatabase(connectionString);
                    liteDbDatabase.Engine.Shrink();
                    storage = new LiteDbStorage(liteDbDatabase);
                }
                else if (!String.IsNullOrEmpty(azureDb))
                {
                    var cloudStorageAccount = CloudStorageAccount.Parse(azureDb);
                    storage = new AzureTableContext(cloudStorageAccount.CreateCloudTableClient());
                }
                else
                {
                    throw new Exception("Connection string for either 'AzureStorage' or 'LiteDb' should been specified.");
                }

                objectRepository = new ObjectRepository(storage, NullLoggerFactory.Instance);

                services.AddSingleton(storage);
                services.AddSingleton(objectRepository);
            }

            var scrapers = GetType().Assembly.GetTypes().Where(v => v.IsSubclassOf(typeof(GenericScraper))).ToList();

            foreach (var s in scrapers)
            {
                services.AddSingleton(typeof(GenericScraper), s);
            }

            services.AddWebEncoders(o =>
            {
                var textEncoderSettings = new TextEncoderSettings();
                textEncoderSettings.AllowRange(UnicodeRanges.All);
                o.TextEncoderSettings = textEncoderSettings;
            });
            services.AddTransient(x => new TableViewModelFactory(x.GetRequiredService <ObjectRepository>()));
            services.AddSingleton <ScriptService>();
            services.AddSingleton <SmsRuleProcessor>();
            services.AddSingleton <UpdateService>();
            services.AddLogging();
            services.AddSession();
            services.AddControllers().AddNewtonsoftJson();
            services.AddHangfire(x => { });
            services.AddDataProtection().AddKeyManagementOptions(options =>
            {
                options.XmlRepository = new ObjectRepositoryXmlStorage(objectRepository);
            });
            services.AddAuthorization();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(x =>
            {
                x.AccessDeniedPath = "/Auth";
                x.LoginPath        = "/Auth";
                x.LogoutPath       = "/Auth/Logout";
            });
        }
Exemple #13
0
        public async Task <int> Handle(RateNewsCommand request, CancellationToken cancellationToken)
        {
            var articles =
                _mapper.Map <List <ArticleDto> >(await _dbContext.Articles
                                                 .Where(a => !a.GoodFactor.HasValue).Take(30)
                                                 .AsNoTracking()
                                                 .ToListAsync());

            var articleContent = new Dictionary <Guid, List <string> >();

            foreach (var item in articles)
            {
                articleContent.Add(item.Id, new List <string>());

                var text = Regex.Replace(item.Content, "<[^>]+>", " ");
                text = Regex.Replace(text, @"[\u0000-\u001F]", " ");

                var responseString = string.Empty;
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    var httpRequest = new HttpRequestMessage(HttpMethod.Post,
                                                             "http://api.ispras.ru/texterra/v1/nlp?targetType=lemma&apikey=e4d5ecb62d3755f4845dc098adf3d25993efc96c")
                    {
                        Content = new StringContent("[{\"text\":\"" + text + "\"}]", Encoding.UTF8, "application/json")
                    };
                    var httpResponse = await httpClient.SendAsync(httpRequest);

                    responseString = await httpResponse.Content.ReadAsStringAsync();
                }

                if (string.IsNullOrWhiteSpace(responseString))
                {
                    Log.Error("Error while response getting.");
                    continue;
                }

                using (var doc = JsonDocument.Parse(responseString))
                {
                    try
                    {
                        var         root               = doc.RootElement;
                        var         arrayElement       = root[0];
                        var         annotationsElement = arrayElement.GetProperty("annotations");
                        var         lemmaElement       = annotationsElement.GetProperty("lemma");
                        JsonElement valueJson;

                        foreach (var element in lemmaElement.EnumerateArray())
                        {
                            if (element.TryGetProperty("value", out valueJson))
                            {
                                string valueString = valueJson.ToString();
                                if (!string.IsNullOrWhiteSpace(valueString))
                                {
                                    articleContent[item.Id].Add(valueString);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Error while adding words from article. More:\n" + ex.Message);
                    }
                }
            }

            var  temp        = 0;
            var  jsonContent = String.Empty;
            bool saveUnknownWords;

            if (!Boolean.TryParse(_configuration["Constants:SaveUnknownWords"], out saveUnknownWords))
            {
                Log.Error("Constants:SaveUnknownWords field is not valid");
                saveUnknownWords = false;
            }

            using (var sr = new StreamReader(AFINNRUJSON))
            {
                jsonContent = await sr.ReadToEndAsync();
            }

            if (string.IsNullOrWhiteSpace(jsonContent))
            {
                Log.Error("Empty afinn json file content");
                return(-1);
            }

            var notFoundWords = new Dictionary <string, int>();

            using (var doc = JsonDocument.Parse(jsonContent))
            {
                var         root = doc.RootElement;
                JsonElement valueJson;
                var         valueCounter = 0;
                var         wordsCounter = 0;
                var         goodFactor   = 0f;

                foreach (var item in articleContent)
                {
                    valueCounter = 0;
                    wordsCounter = 0;

                    foreach (var word in item.Value)
                    {
                        if (root.TryGetProperty(word, out valueJson) && valueJson.TryGetInt32(out temp))
                        {
                            valueCounter += temp;
                            wordsCounter++;

                            temp = 0;
                        }
                        else if (saveUnknownWords && !notFoundWords.ContainsKey(word))
                        {
                            notFoundWords.Add(word, 0);
                        }
                    }

                    if (wordsCounter == 0)
                    {
                        goodFactor = 0;
                        Log.Warning("0 words found for article " + item.Key.ToString());
                    }
                    else
                    {
                        goodFactor = (float)valueCounter / wordsCounter;
                    }

                    articles[articles.FindIndex(a => a.Id == item.Key)].GoodFactor = goodFactor;
                }
            }

            _dbContext.Articles.UpdateRange(_mapper.Map <List <Article> >(articles));
            var result = await _dbContext.SaveChangesAsync(cancellationToken);

            if (saveUnknownWords)
            {
                var fileName      = Path.Combine(UNKNOWNWORDSDIR, UNKNOWNWORDSFILE);
                var existingItems = new Dictionary <string, int>();

                try
                {
                    if (!Directory.Exists(UNKNOWNWORDSDIR))
                    {
                        Directory.CreateDirectory(UNKNOWNWORDSDIR);
                    }
                    if (!File.Exists(fileName))
                    {
                        File.Create(fileName);
                    }

                    using (var r = new StreamReader(fileName))
                    {
                        var json = await r.ReadToEndAsync();

                        try
                        {
                            existingItems = JsonSerializer.Deserialize <Dictionary <string, int> >(json);
                        }
                        catch (Exception e)
                        {
                            Log.Error(e.Message);
                            existingItems = new Dictionary <string, int>();
                        }
                    }

                    foreach (var notFoundWord in notFoundWords)
                    {
                        if (!existingItems.ContainsKey(notFoundWord.Key))
                        {
                            existingItems.Add(notFoundWord.Key, notFoundWord.Value);
                        }
                    }

                    using (var fs = new StreamWriter(fileName, false, Encoding.UTF8))
                    {
                        var encoderSettings = new TextEncoderSettings();
                        encoderSettings.AllowRange(UnicodeRanges.All);
                        var options = new JsonSerializerOptions
                        {
                            Encoder       = JavaScriptEncoder.Create(encoderSettings),
                            WriteIndented = true
                        };

                        var jsonString = JsonSerializer.Serialize(existingItems, options);
                        await fs.WriteLineAsync(jsonString);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                }
            }

            return(result);
        }
Exemple #14
0
        /// <summary>
        /// Creates a JSON objectstring to render the reserved Seats in the view
        /// </summary>
        /// <param name="Room">A Room of type Room.</param>
        /// <param name="Seats">A list of type ReservedSeat.</param>
        /// <returns>A JSON string to the ViewModel</returns>
        public static string JSONSeating(Room Room, List <ReservedSeat> Seats)
        {
            if (Room == null)
            {
                Room = new Room();
            }

            List <ObjRow> ObjRowList = new List <ObjRow>();

            for (var j = 1; j <= Room.Rows; j++)
            {
                List <ObjSeat> objSeatList = new List <ObjSeat>();
                for (var i = 1; i <= (Room.Capacity / Room.Rows); i++)
                {
                    ObjSeat ObjSeat = new ObjSeat {
                        GridSeatNum = i, seatNumber = i, SeatStatus = "0"
                    };
                    var seatTaken = 0;
                    Seats.ForEach(s =>
                    {
                        if (s.SeatId == i & s.RowId == j & s.SeatState == SeatState.Reserved)
                        {
                            seatTaken = 1;
                        }
                        if (s.SeatId == i & s.RowId == j & s.SeatState == SeatState.Disabled)
                        {
                            seatTaken = 2;
                        }
                    });
                    if (seatTaken > 0)
                    {
                        ObjSeat.SeatStatus = seatTaken.ToString();
                        seatTaken          = 0;
                    }

                    objSeatList.Add(ObjSeat);
                }
                ObjRow ObjRow = new ObjRow {
                    GridRowId = j, PhyRowId = j.ToString(), objSeat = objSeatList
                };
                ObjRowList.Add(ObjRow);
            }

            ObjArea ObjArea = new ObjArea {
                AreaDesc = "EXECUTIVE", AreaCode = "0000000003", AreaNum = "1", HasCurrentOrder = true, objRow = ObjRowList
            };
            List <ObjArea> ObjAreaList = new List <ObjArea>();

            ObjAreaList.Add(ObjArea);


            ColAreas ColAreas = new ColAreas {
                Count = 1, intMaxSeatId = 21, intMinSeatId = 2, objArea = ObjAreaList
            };
            SeatLayout SeatLayout = new SeatLayout {
                colAreas = ColAreas
            };
            List <object> areaList         = new List <object>();
            List <object> groupedSeatsList = new List <object>();

            Root Root = new Root
            {
                product_id   = 46539040,
                freeSeating  = false,
                tempTransId  = "1ecae165f2d86315fea19963d0ded41a",
                seatLayout   = SeatLayout,
                areas        = areaList,
                groupedSeats = groupedSeatsList
            };

            var encoderSettings = new TextEncoderSettings();

            encoderSettings.AllowCharacters('\u0022');
            encoderSettings.AllowRange(UnicodeRanges.BasicLatin);
            var options = new JsonSerializerOptions
            {
                Encoder       = JavaScriptEncoder.Create(encoderSettings),
                WriteIndented = true
            };
            string jsonString = JsonSerializer.Serialize(Root, options);

            return(jsonString);
        }