From 29ade01eed8896ea3bb6b63ebb1a84ad072d633e Mon Sep 17 00:00:00 2001 From: h7x4abk3g Date: Thu, 25 Jun 2020 22:42:16 +0200 Subject: [PATCH] Add parser library --- README.md | 12 ++--- example/example_search_example.dart | 3 +- example/kanji_search_example.dart | 3 +- example/phrase_scrape_example.dart | 3 +- example/phrase_search_example.dart | 5 +- lib/api.dart | 54 ++++++++++++++++++++ lib/parser.dart | 28 +++++++++++ lib/src/unofficial_jisho_api_base.dart | 70 -------------------------- lib/unofficial_jisho_api.dart | 7 --- test/create_test_cases.dart | 9 ++-- test/unofficial_jisho_api_test.dart | 10 ++-- 11 files changed, 99 insertions(+), 105 deletions(-) create mode 100644 lib/api.dart create mode 100644 lib/parser.dart delete mode 100644 lib/src/unofficial_jisho_api_base.dart delete mode 100644 lib/unofficial_jisho_api.dart diff --git a/README.md b/README.md index 7882093..792a228 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,7 @@ Below are some basic examples. This returns the same results as the official [Jisho.org](https://jisho.org/) API. See the discussion of that [here](https://jisho.org/forum/54fefc1f6e73340b1f160000-is-there-any-kind-of-search-api). ```dart -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { jisho.searchForPhrase('日').then((result) { @@ -28,8 +27,7 @@ void main() async { ```dart import 'dart:convert' show jsonEncode; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForKanji('語').then((result) { @@ -78,8 +76,7 @@ Jisho Uri: https://jisho.org/search/%E8%AA%9E%23kanji ```dart import 'dart:convert' show jsonEncode; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForExamples('日').then((result) { @@ -126,8 +123,7 @@ This scrapes the word/phrase page on Jisho.org. This can get you some data that ```dart import 'dart:convert'; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; final encoder = JsonEncoder.withIndent(' '); void main() async { diff --git a/example/example_search_example.dart b/example/example_search_example.dart index d60f0ad..662b788 100644 --- a/example/example_search_example.dart +++ b/example/example_search_example.dart @@ -1,6 +1,5 @@ import 'dart:convert' show jsonEncode; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForExamples('日').then((result) { diff --git a/example/kanji_search_example.dart b/example/kanji_search_example.dart index 78bf3ef..211bdd3 100644 --- a/example/kanji_search_example.dart +++ b/example/kanji_search_example.dart @@ -1,6 +1,5 @@ import 'dart:convert' show jsonEncode; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; void main() async { await jisho.searchForKanji('語').then((result) { diff --git a/example/phrase_scrape_example.dart b/example/phrase_scrape_example.dart index fcb3cbc..6062d90 100644 --- a/example/phrase_scrape_example.dart +++ b/example/phrase_scrape_example.dart @@ -1,6 +1,5 @@ import 'dart:convert'; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; final encoder = JsonEncoder.withIndent(' '); void main() async { diff --git a/example/phrase_search_example.dart b/example/phrase_search_example.dart index 1470950..8402c3b 100644 --- a/example/phrase_search_example.dart +++ b/example/phrase_search_example.dart @@ -1,10 +1,9 @@ import 'dart:convert'; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; -final jisho = JishoApi(); +import 'package:unofficial_jisho_api/api.dart' as jisho; final encoder = JsonEncoder.withIndent(' '); void main() async { - await jisho.searchForPhrase('反対').then((result) { + await jisho.searchForPhrase('日').then((result) { print(encoder.convert(result)); }); } \ No newline at end of file diff --git a/lib/api.dart b/lib/api.dart new file mode 100644 index 0000000..0d2f6be --- /dev/null +++ b/lib/api.dart @@ -0,0 +1,54 @@ +library unofficial_jisho_api; +import './src/objects.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; + +import './src/phraseSearch.dart'; +import './src/kanjiSearch.dart'; +import './src/exampleSearch.dart'; +import './src/phraseScrape.dart'; + +/// Query the official Jisho API for a word or phrase +/// +/// See https://jisho.org/forum/54fefc1f6e73340b1f160000-is-there-any-kind-of-search-api +/// for discussion about the official API. +Future searchForPhrase(String phrase) async { + final uri = uriForPhraseSearch(phrase); + return await http.get(uri).then((response) => JishoAPIResult.fromJson(jsonDecode(response.body))); +} + +/// Scrape Jisho.org for information about a kanji character. +Future searchForKanji(String kanji) async { + final uri = uriForKanjiSearch(kanji); + return http.get(uri).then((response) => parseKanjiPageData(response.body, kanji)); +} + +/// Scrape Jisho.org for examples. +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. +/// +/// This allows you to get some information that isn't provided by the official API, such as +/// part-of-speech and JLPT level. However, the official API should be preferred +/// if it has the information you need. This function scrapes https://jisho.org/word/XXX. +/// In general, you'll want to include kanji in your search term, for example 掛かる +/// instead of かかる (no results). +Future scrapeForPhrase(String phrase) async { + final uri = uriForPhraseScrape(phrase); + try { + final response = await http.get(uri); + return parsePhrasePageData(response.body, phrase); + } catch (err) { + // if (response.statusCode == 404) { + // return PhrasePageScrapeResult( + // query: phrase, + // found: false, + // ); + // } + + throw err; + } +} \ No newline at end of file diff --git a/lib/parser.dart b/lib/parser.dart new file mode 100644 index 0000000..d32a100 --- /dev/null +++ b/lib/parser.dart @@ -0,0 +1,28 @@ +library unofficial_jisho_parser; +import './src/objects.dart'; + +import './src/phraseSearch.dart' as phrase_search; +import './src/kanjiSearch.dart' as kanji_search; +import './src/exampleSearch.dart' as example_search; +import './src/phraseScrape.dart' as phrase_scrape; + +/// Provides the URI for a phrase search +String uriForPhraseSearch(String phrase) => phrase_search.uriForPhraseSearch(phrase); + +/// Provides the URI for a kanji search +String uriForKanjiSearch(String kanji) => kanji_search.uriForKanjiSearch(kanji); + +/// Provides the URI for an example search +String uriForExampleSearch(String phrase) => example_search.uriForExampleSearch(phrase); + +/// Provides the URI for a phrase scrape +String uriForPhraseScrape(String searchTerm) => phrase_scrape.uriForPhraseScrape(searchTerm); + +/// Parses a jisho kanji search page to an object +KanjiResult parseKanjiPageHtml(String pageHtml, String kanji) => kanji_search.parseKanjiPageData(pageHtml, kanji); + +/// Parses a jisho example sentence search page to an object +ExampleResults parseExamplePageHtml(String pageHtml, String phrase) => example_search.parseExamplePageData(pageHtml, phrase); + +/// Parses a jisho word search page to an object +PhrasePageScrapeResult parsePhraseScrapeHtml(String pageHtml, String query) => phrase_scrape.parsePhrasePageData(pageHtml, query); \ No newline at end of file diff --git a/lib/src/unofficial_jisho_api_base.dart b/lib/src/unofficial_jisho_api_base.dart deleted file mode 100644 index 635d983..0000000 --- a/lib/src/unofficial_jisho_api_base.dart +++ /dev/null @@ -1,70 +0,0 @@ -import './objects.dart'; -import 'package:http/http.dart' as http; -import 'dart:convert'; - -import './phraseSearch.dart'; -import './kanjiSearch.dart'; -import './exampleSearch.dart'; -import './phraseScrape.dart'; - - -class JishoApi { - - /// Query the official Jisho API for a word or phrase - /// - /// See [here]{@link https://jisho.org/forum/54fefc1f6e73340b1f160000-is-there-any-kind-of-search-api} - /// for discussion about the official API. - /// @param {string} phrase The search term to search for. - /// @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 - Future searchForPhrase(String phrase) async { - final uri = uriForPhraseSearch(phrase); - return await http.get(uri).then((response) => JishoAPIResult.fromJson(jsonDecode(response.body))); - } - - /// 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. - /// - /// This allows you to get some information that isn't provided by the official API, such as - /// part-of-speech and JLPT level. However, the official API should be preferred - /// if it has the information you need. This function scrapes https://jisho.org/word/XXX. - /// In general, you'll want to include kanji in your search term, for example 掛かる - /// instead of かかる (no results). - /// @param {string} phrase The search term to search for. - /// @returns {PhrasePageScrapeResult} Information about the searched query. - /// @async - Future scrapeForPhrase(String phrase) async { - final uri = uriForPhraseScrape(phrase); - try { - final response = await http.get(uri); - return parsePhrasePageData(response.body, phrase); - } catch (err) { - // if (response.statusCode == 404) { - // return PhrasePageScrapeResult( - // query: phrase, - // found: false, - // ); - // } - - throw err; - } - } -} \ No newline at end of file diff --git a/lib/unofficial_jisho_api.dart b/lib/unofficial_jisho_api.dart deleted file mode 100644 index 495acfd..0000000 --- a/lib/unofficial_jisho_api.dart +++ /dev/null @@ -1,7 +0,0 @@ -/// Support for doing something awesome. -/// -/// More dartdocs go here. -library unofficial_jisho_api; - -export 'src/unofficial_jisho_api_base.dart'; - diff --git a/test/create_test_cases.dart b/test/create_test_cases.dart index 6931ede..fe0c35b 100644 --- a/test/create_test_cases.dart +++ b/test/create_test_cases.dart @@ -2,9 +2,8 @@ import 'dart:io'; import 'package:path/path.dart' as path; import 'dart:convert'; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; +import 'package:unofficial_jisho_api/api.dart'; -final jisho = JishoApi(); final encoder = JsonEncoder.withIndent(' '); final currentdir = Directory.current.path; @@ -25,7 +24,7 @@ const exampleQueries = ['車', '日本人', '彼*叩く', '皆', 'ネガティ const phraseQueries = ['車', '日本人', '皆', 'ネガティブ', 'grlgmregmneriireg']; void main() async { - await writeCases(jisho.searchForKanji, 'kanji_test_cases', kanjiQueries); - await writeCases(jisho.searchForExamples, 'example_test_cases', exampleQueries); - await writeCases(jisho.scrapeForPhrase, 'phrase_scrape_test_cases', phraseQueries); + await writeCases(searchForKanji, 'kanji_test_cases', kanjiQueries); + await writeCases(searchForExamples, 'example_test_cases', exampleQueries); + await writeCases(scrapeForPhrase, 'phrase_scrape_test_cases', phraseQueries); } diff --git a/test/unofficial_jisho_api_test.dart b/test/unofficial_jisho_api_test.dart index 4d476e1..b909f93 100644 --- a/test/unofficial_jisho_api_test.dart +++ b/test/unofficial_jisho_api_test.dart @@ -2,11 +2,9 @@ import 'dart:io'; import 'package:path/path.dart' as path; import 'dart:convert'; -import 'package:unofficial_jisho_api/unofficial_jisho_api.dart'; +import 'package:unofficial_jisho_api/api.dart'; import 'package:test/test.dart'; -final jisho = JishoApi(); - List getFilePaths(String dirname) { final currentdir = Directory.current.path; final filenames = Directory(path.join(currentdir, 'test', dirname)).listSync(); @@ -25,7 +23,7 @@ void runTestCases(List testCaseFiles, Function apiFunction) async { } void main() async { - await runTestCases(getFilePaths('kanji_test_cases'), jisho.searchForKanji); - await runTestCases(getFilePaths('example_test_cases'), jisho.searchForExamples); - await runTestCases(getFilePaths('phrase_scrape_test_cases'), jisho.scrapeForPhrase); + await runTestCases(getFilePaths('kanji_test_cases'), searchForKanji); + await runTestCases(getFilePaths('example_test_cases'), searchForExamples); + await runTestCases(getFilePaths('phrase_scrape_test_cases'), scrapeForPhrase); }