diff --git a/lib/src/objects.dart b/lib/src/objects.dart index 1e86dca..ecbcd6e 100644 --- a/lib/src/objects.dart +++ b/lib/src/objects.dart @@ -1,3 +1,175 @@ +/* -------------------------------------------------------------------------- */ +/* searchForKanji related classes */ +/* -------------------------------------------------------------------------- */ + +class YomiExample { + String example; + String reading; + String meaning; + + YomiExample({String example, String reading, String meaning}) + { + this.example = example; + this.reading = reading; + this.meaning = meaning; + } + + Map toJson() => + { + 'example': example, + 'reading': reading, + 'meaning': meaning + }; + +} + +class Radical { + String symbol; + List forms; + String meaning; + + Radical({String symbol, List forms, String meaning}){ + this.symbol = symbol; + this.forms = forms; + this.meaning = meaning; + } + + Map toJson() => + { + 'symbol': symbol, + 'forms': forms, + 'meaning': meaning + }; + +} + +class KanjiResult { + String query; + bool found; + + String taughtIn; + String jlptLevel; + int newspaperFrequencyRank; + int strokeCount; + String meaning; + List kunyomi; + List onyomi; + List kunyomiExamples; + List onyomiExamples; + Radical radical; + List parts; + String strokeOrderDiagramUri; + String strokeOrderSvgUri; + String strokeOrderGifUri; + String uri; + + Map toJson() { + + if (found == false) { + return { + 'query': query, + 'found': found + }; + } + + var returnObject = { + 'query': query, + 'found': found, + 'taughtIn': taughtIn, + 'jlptLevel': jlptLevel, + 'newspaperFrequencyRank': newspaperFrequencyRank, + 'strokeCount': strokeCount, + 'meaning': meaning, + 'kunyomi': kunyomi, + 'onyomi': onyomi, + 'onyomiExamples': onyomiExamples, + 'kunyomiExamples': kunyomiExamples, + 'radical': radical.toJson(), + 'parts': parts, + 'strokeOrderDiagramUri': strokeOrderDiagramUri, + 'strokeOrderSvgUri': strokeOrderSvgUri, + 'strokeOrderGifUri': strokeOrderGifUri, + 'uri': uri + }; + + return returnObject; + } +} + +/* -------------------------------------------------------------------------- */ +/* searchForExamples related classes */ +/* -------------------------------------------------------------------------- */ + +class ExampleSentencePiece { + String lifted; + String unlifted; + + ExampleSentencePiece({String lifted, String unlifted}){ + this.lifted = lifted; + this.unlifted = unlifted; + } + + Map toJson() { + return { + 'lifted': lifted, + 'unlifted': unlifted + }; + } +} + +class ExampleResultData { + String kanji; + String kana; + String english; + List pieces; + + ExampleResultData({String english, String kanji, String kana, List pieces}){ + this.english = english; + this.kanji = kanji; + this.kana = kana; + this.pieces = pieces; + } + + Map toJson() { + return { + 'english': english, + 'kanji': kanji, + 'kana': kana, + 'pieces': pieces + }; + } +} + +class ExampleResults { + String query; + bool found; + String uri; + List results; + String phrase; + + ExampleResults({String query, bool found, List results, String uri, String phrase}){ + this.query = query; + this.found = found; + this.results = results; + this.uri = uri; + this.phrase = phrase; + } + + Map toJson() { + return { + 'query': query, + 'found': found, + 'results': results, + 'uri': uri, + 'phrase': phrase + }; + } +} + +/* -------------------------------------------------------------------------- */ +/* scrapeForPhrase related classes */ +/* -------------------------------------------------------------------------- */ + class PhraseScrapeSentence { String english; String japanese; @@ -108,166 +280,151 @@ class PhrasePageScrapeResult { }; } -class YomiExample { - String example; +/* -------------------------------------------------------------------------- */ +/* searchForPhrase related classes */ +/* -------------------------------------------------------------------------- */ + +class JishoJapaneseWord { + String word; String reading; - String meaning; - YomiExample({String example, String reading, String meaning}) - { - this.example = example; - this.reading = reading; - this.meaning = meaning; - } + JishoJapaneseWord({this.word, this.reading}); - Map toJson() => - { - 'example': example, - 'reading': reading, - 'meaning': meaning - }; - -} - -class Radical { - String symbol; - List forms; - String meaning; - - Radical({String symbol, List forms, String meaning}){ - this.symbol = symbol; - this.forms = forms; - this.meaning = meaning; - } - - Map toJson() => - { - 'symbol': symbol, - 'forms': forms, - 'meaning': meaning - }; - -} - -class KanjiResult { - String query; - bool found; - - String taughtIn; - String jlptLevel; - int newspaperFrequencyRank; - int strokeCount; - String meaning; - List kunyomi; - List onyomi; - List kunyomiExamples; - List onyomiExamples; - Radical radical; - List parts; - String strokeOrderDiagramUri; - String strokeOrderSvgUri; - String strokeOrderGifUri; - String uri; - - Map toJson() { - - if (found == false) { - return { - 'query': query, - 'found': found - }; - } - - var returnObject = { - 'query': query, - 'found': found, - 'taughtIn': taughtIn, - 'jlptLevel': jlptLevel, - 'newspaperFrequencyRank': newspaperFrequencyRank, - 'strokeCount': strokeCount, - 'meaning': meaning, - 'kunyomi': kunyomi, - 'onyomi': onyomi, - 'onyomiExamples': onyomiExamples, - 'kunyomiExamples': kunyomiExamples, - 'radical': radical.toJson(), - 'parts': parts, - 'strokeOrderDiagramUri': strokeOrderDiagramUri, - 'strokeOrderSvgUri': strokeOrderSvgUri, - 'strokeOrderGifUri': strokeOrderGifUri, - 'uri': uri - }; + factory JishoJapaneseWord.fromJson(Map json){ + return JishoJapaneseWord( + word: json['word'] as String, + reading: json['reading'] as String + ); - return returnObject; } } -class ExampleSentencePiece { - String lifted; - String unlifted; +class JishoSenseLink { + String text; + String url; - ExampleSentencePiece({String lifted, String unlifted}){ - this.lifted = lifted; - this.unlifted = unlifted; - } + JishoSenseLink({this.text, this.url}); - Map toJson() { - return { - 'lifted': lifted, - 'unlifted': unlifted - }; + factory JishoSenseLink.fromJson(Map json){ + return JishoSenseLink( + text: json['text'] as String, + url: json['url'] as String + ); } } -class ExampleResultData { - String kanji; - String kana; - String english; - List pieces; +class JishoWordSense { + List english_definitions; + List parts_of_speech; + List links; + List tags; + List see_also; + List antonyms; + List source; + List info; + List restrictions; - ExampleResultData({String english, String kanji, String kana, List pieces}){ - this.english = english; - this.kanji = kanji; - this.kana = kana; - this.pieces = pieces; - } + JishoWordSense({ + this.english_definitions, + this.parts_of_speech, + this.links, + this.tags, + this.see_also, + this.antonyms, + this.source, + this.info, + this.restrictions + }); - Map toJson() { - return { - 'english': english, - 'kanji': kanji, - 'kana': kana, - 'pieces': pieces - }; + factory JishoWordSense.fromJson(Map json){ + return JishoWordSense( + english_definitions: json['english_definitions'] as List, + parts_of_speech: json['parts_of_speech'] as List, + links: json['links'] as List, + tags: json['tags'] as List, + see_also: json['see_also'] as List, + antonyms: json['antonyms'] as List, + source: json['source'] as List, + info: json['info'] as List, + restrictions: json['restrictions'] as List + ); } } -class ExampleResults { - String query; - bool found; - String uri; - List results; - String phrase; +class JishoAttribution { + bool jmdict; + bool jmnedict; + bool dbpedia; - ExampleResults({String query, bool found, List results, String uri, String phrase}){ - this.query = query; - this.found = found; - this.results = results; - this.uri = uri; - this.phrase = phrase; - } + JishoAttribution({ + this.jmdict, + this.jmnedict, + this.dbpedia + }); - Map toJson() { - return { - 'query': query, - 'found': found, - 'results': results, - 'uri': uri, - 'phrase': phrase - }; + factory JishoAttribution.fromJson(Map json){ + return JishoAttribution( + jmdict: json['jmdict'] as bool, + jmnedict: json['jmnedict'] as bool, + dbpedia: json['dbpedia'] as bool + ); } } +class JishoResult { + String slug; + bool is_common; + List tags; + List jlpt; + List japanese; + List senses; + JishoAttribution attribution; + JishoResult({ + this.slug, + this.is_common, + this.tags, + this.jlpt, + this.japanese, + this.senses, + this.attribution + }) + factory JishoResult.fromJson(Map json){ + return JishoResult( + slug: json['slug'] as String, + is_common: json['is_common'] as bool, + tags: json['tags'] as List, + jlpt: json['jlpt'] as List, + japanese: json['japanese'] as List, + senses: json['senses'] as List, + attribution: json['attribution'] as JishoAttribution + ); + } +} +class JishoResultMeta { + int status; + + JishoResultMeta({this.status}); + + factory JishoResultMeta.fromJson(Map json){ + return JishoResultMeta( + status: json['status'] as int + ); + } +} + +class JishoAPIResult { + JishoResultMeta meta; + List data; + + JishoAPIResult({this.meta, this.data}); + + factory JishoAPIResult.fromJson(Map json){ + return JishoAPIResult( + meta: json['meta'] as JishoResultMeta, + data: json['data'] as List + ); + } +} \ No newline at end of file diff --git a/lib/src/unofficial_jisho_api_base.dart b/lib/src/unofficial_jisho_api_base.dart index d603c2f..14ee018 100644 --- a/lib/src/unofficial_jisho_api_base.dart +++ b/lib/src/unofficial_jisho_api_base.dart @@ -487,9 +487,28 @@ class JishoApi { /// @returns {Object} The response data from the official Jisho.org API. Its format is somewhat /// complex and is not documented, so put on your trial-and-error hat. /// @async - searchForPhrase(String phrase) async { + Future> searchForPhrase(String phrase) async { final uri = uriForPhraseSearch(phrase); - return http.get(uri).then((response) => jsonDecode(response.body).data); + final JishoAPIResult jsonData = await http.get(uri).then((response) => jsonDecode(response.body)); + return jsonData.data; + } + + /// Scrape Jisho.org for information about a kanji character. + /// @param {string} kanji The kanji to search for. + /// @returns {KanjiResult} Information about the searched kanji. + /// @async + Future searchForKanji(String kanji) async { + final uri = uriForKanjiSearch(kanji); + return http.get(uri).then((response) => parseKanjiPageData(response.body, kanji)); + } + + /// Scrape Jisho.org for examples. + /// @param {string} phrase The word or phrase to search for. + /// @returns {ExampleResults} + /// @async + Future searchForExamples(String phrase) async { + final uri = uriForExampleSearch(phrase); + return http.get(uri).then((response) => parseExamplePageData(response.body, phrase)); } /// Scrape the word page for a word/phrase. @@ -518,22 +537,4 @@ class JishoApi { throw err; } } - - /// Scrape Jisho.org for information about a kanji character. - /// @param {string} kanji The kanji to search for. - /// @returns {KanjiResult} Information about the searched kanji. - /// @async - Future searchForKanji(String kanji) async { - final uri = uriForKanjiSearch(kanji); - return http.get(uri).then((response) => parseKanjiPageData(response.body, kanji)); - } - - /// Scrape Jisho.org for examples. - /// @param {string} phrase The word or phrase to search for. - /// @returns {ExampleResults} - /// @async - Future searchForExamples(String phrase) async { - final uri = uriForExampleSearch(phrase); - return http.get(uri).then((response) => parseExamplePageData(response.body, phrase)); - } } \ No newline at end of file