Advertisement
ivandrofly

SubtitleEdit: Check embedded dictionary status

Oct 25th, 2024
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO.Compression;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Threading.Tasks;
  7. using System.Xml.Linq;
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9. using Nikse.SubtitleEdit;
  10.  
  11. namespace Test.Dictionaries
  12. {
  13.     [TestClass]
  14.     public class HunspellDictionaryTest
  15.     {
  16.         [TestMethod]
  17.         public async Task DictionaryStatusTest()
  18.         {
  19.             var assembly = typeof(Program).Assembly;
  20.  
  21.             using (var gzipStream = assembly.GetManifestResourceStream("Nikse.SubtitleEdit.Resources.HunspellDictionaries.xml.gz"))
  22.             using (var gzip = new GZipStream(gzipStream, CompressionMode.Decompress))
  23.             using (var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(3) })
  24.             {
  25.                 // todo: add httpclient header, some sites might drop the request if some header is not provided
  26.                 var doc = XDocument.Load(gzip);
  27.  
  28.                 foreach (var dictionaryEl in doc.Root.Elements("Dictionary"))
  29.                 {
  30.                     string name = dictionaryEl.Element("EnglishName").Value;
  31.                     Debug.WriteLine($"Processing document: {name}");
  32.                     var url = dictionaryEl.Element("DownloadLink").Value;
  33.                     try
  34.                     {
  35.                         var response = await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Get, url),
  36.                             HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
  37.                         Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, $"{name} is not available!");
  38.                     }
  39.                     catch (Exception e)
  40.                     {
  41.                         Debug.WriteLine($"Failed to get response: {name}: {e.Message}");
  42.                     }
  43.                     await Task.Delay(500).ConfigureAwait(false);
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement