/FlashCard/WindowsFormsApplication1/Importers/SmartFM.cs
C# | 140 lines | 116 code | 24 blank | 0 comment | 15 complexity | 2536b951d224a8ab91ea09588945d784 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.Xml.Linq;
- using System.Text.RegularExpressions;
-
- namespace FlashCard
- {
- internal class SmartFM
- {
- const string ItemsListPath = "http://api.iknow.co.jp/lists/{0}/items.xml?per_page={1}&page={2}";
-
- public static List<Word> ImportList(int listId)
- {
- List<Word> results = new List<Word>();
-
- int total = 200;
- int perPage = 20;
-
- for (int i = 1; i < total/perPage + 1; i++)
- {
- string path = string.Format(ItemsListPath, listId, perPage, i);
-
- string xml = FetchString(path);
- string xmlns = "xmlns=\"http://api.smart.fm/specifications/vocabulary/1.0\"";
- xml = xml.Remove(xml.IndexOf(xmlns), xmlns.Length);
-
- XElement vocabulary = XElement.Parse(xml);
- XElement items = vocabulary.Element("items");
-
- foreach (XElement item in items.Elements("item"))
- {
- Word word = new Word();
-
- XElement cue = item.Element("cue");
- word.Note = cue.Attribute("part_of_speech").GetValue();
- word.KanaWord = cue.Element("text").GetValue();
- word.KanjiWord = cue.Element("character").GetValue();
-
- if (cue.Element("sound") != null)
- {
- string remotePath = cue.Element("sound").GetValue();
- string fileName = Path.GetFileNameWithoutExtension(remotePath) + ".mp3";
- FetchData(remotePath, fileName);
- word.WordSound = fileName;
- }
-
- XElement responses = item.Element("responses");
- XElement response = responses.Elements().Where(x => x.Attribute("language").GetValue() == "en").First();
- word.EnglishWord = response.Element("text").GetValue();
-
- XElement sentences = item.Element("sentences");
- XElement sentence = sentences.Element("sentence");
- word.KanjiSentence = sentence.Element("text").GetValue();
- word.KanaSentence = sentence.Element("transliterations").Elements().Where(x => x.Attribute("type").GetValue() != "Latn").First().GetValue();
- word.EnglishSentence = sentence.Element("translations").Element("sentence").Element("text").GetValue();
-
- if (sentence.Element("sound") != null)
- {
- string remotePath = sentence.Element("sound").GetValue();
- string fileName = Path.GetFileNameWithoutExtension(remotePath) + ".mp3";
- FetchData(remotePath, fileName);
- word.SentenceSound = fileName;
- }
-
- results.Add(word);
- }
- }
-
- return results;
- }
-
- private static string FetchString(string url)
- {
- for (int i = 0; i < 5; i++)
- {
- try
- {
- WebClient client = new WebClient();
- byte[] buffer = client.DownloadData(url);
-
- return Encoding.UTF8.GetString(buffer);
- }
- catch { if (i > 4) throw; }
- }
-
- return null;
- }
-
- private static void FetchData(string url, string path)
- {
- if (!File.Exists(path))
- {
- for (int i = 0; i < 5; i++)
- {
- try
- {
- WebClient client = new WebClient();
- client.DownloadFile(url, path);
- break;
- }
- catch { }
- }
- }
- }
- }
-
- internal static class Helper
- {
- public static string GetValue(this XElement element)
- {
- if (element != null)
- {
- return StripHTML(element.Value ?? string.Empty);
- }
-
- return string.Empty;
- }
-
- public static string GetValue(this XAttribute attribute)
- {
- if (attribute != null)
- {
- return StripHTML(attribute.Value ?? string.Empty);
- }
-
- return string.Empty;
- }
-
- const string HTML_TAG_PATTERN = "<.*?>";
-
- private static string StripHTML(string inputString)
- {
- return Regex.Replace(inputString, HTML_TAG_PATTERN, string.Empty);
- }
- }
- }