PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/FlashCard/WindowsFormsApplication1/Importers/SmartFM.cs

https://bitbucket.org/floAr/personal
C# | 140 lines | 116 code | 24 blank | 0 comment | 15 complexity | 2536b951d224a8ab91ea09588945d784 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Xml.Linq;
  8. using System.Text.RegularExpressions;
  9. namespace FlashCard
  10. {
  11. internal class SmartFM
  12. {
  13. const string ItemsListPath = "http://api.iknow.co.jp/lists/{0}/items.xml?per_page={1}&page={2}";
  14. public static List<Word> ImportList(int listId)
  15. {
  16. List<Word> results = new List<Word>();
  17. int total = 200;
  18. int perPage = 20;
  19. for (int i = 1; i < total/perPage + 1; i++)
  20. {
  21. string path = string.Format(ItemsListPath, listId, perPage, i);
  22. string xml = FetchString(path);
  23. string xmlns = "xmlns=\"http://api.smart.fm/specifications/vocabulary/1.0\"";
  24. xml = xml.Remove(xml.IndexOf(xmlns), xmlns.Length);
  25. XElement vocabulary = XElement.Parse(xml);
  26. XElement items = vocabulary.Element("items");
  27. foreach (XElement item in items.Elements("item"))
  28. {
  29. Word word = new Word();
  30. XElement cue = item.Element("cue");
  31. word.Note = cue.Attribute("part_of_speech").GetValue();
  32. word.KanaWord = cue.Element("text").GetValue();
  33. word.KanjiWord = cue.Element("character").GetValue();
  34. if (cue.Element("sound") != null)
  35. {
  36. string remotePath = cue.Element("sound").GetValue();
  37. string fileName = Path.GetFileNameWithoutExtension(remotePath) + ".mp3";
  38. FetchData(remotePath, fileName);
  39. word.WordSound = fileName;
  40. }
  41. XElement responses = item.Element("responses");
  42. XElement response = responses.Elements().Where(x => x.Attribute("language").GetValue() == "en").First();
  43. word.EnglishWord = response.Element("text").GetValue();
  44. XElement sentences = item.Element("sentences");
  45. XElement sentence = sentences.Element("sentence");
  46. word.KanjiSentence = sentence.Element("text").GetValue();
  47. word.KanaSentence = sentence.Element("transliterations").Elements().Where(x => x.Attribute("type").GetValue() != "Latn").First().GetValue();
  48. word.EnglishSentence = sentence.Element("translations").Element("sentence").Element("text").GetValue();
  49. if (sentence.Element("sound") != null)
  50. {
  51. string remotePath = sentence.Element("sound").GetValue();
  52. string fileName = Path.GetFileNameWithoutExtension(remotePath) + ".mp3";
  53. FetchData(remotePath, fileName);
  54. word.SentenceSound = fileName;
  55. }
  56. results.Add(word);
  57. }
  58. }
  59. return results;
  60. }
  61. private static string FetchString(string url)
  62. {
  63. for (int i = 0; i < 5; i++)
  64. {
  65. try
  66. {
  67. WebClient client = new WebClient();
  68. byte[] buffer = client.DownloadData(url);
  69. return Encoding.UTF8.GetString(buffer);
  70. }
  71. catch { if (i > 4) throw; }
  72. }
  73. return null;
  74. }
  75. private static void FetchData(string url, string path)
  76. {
  77. if (!File.Exists(path))
  78. {
  79. for (int i = 0; i < 5; i++)
  80. {
  81. try
  82. {
  83. WebClient client = new WebClient();
  84. client.DownloadFile(url, path);
  85. break;
  86. }
  87. catch { }
  88. }
  89. }
  90. }
  91. }
  92. internal static class Helper
  93. {
  94. public static string GetValue(this XElement element)
  95. {
  96. if (element != null)
  97. {
  98. return StripHTML(element.Value ?? string.Empty);
  99. }
  100. return string.Empty;
  101. }
  102. public static string GetValue(this XAttribute attribute)
  103. {
  104. if (attribute != null)
  105. {
  106. return StripHTML(attribute.Value ?? string.Empty);
  107. }
  108. return string.Empty;
  109. }
  110. const string HTML_TAG_PATTERN = "<.*?>";
  111. private static string StripHTML(string inputString)
  112. {
  113. return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);
  114. }
  115. }
  116. }