diff --git a/README.md b/README.md index 8d3a677..4820216 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,7 @@ final SEARCH_URI = jisho_parser.uriForKanjiSearch(SEARCH_KANJI); void main() async { await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseKanjiPageHtml(result.body, SEARCH_KANJI); + final parsedResult = jisho_parser.parseKanjiPageData(result.body, SEARCH_KANJI); print('JLPT level: ${parsedResult.jlptLevel}'); print('Stroke count: ${parsedResult.strokeCount}'); print('Meaning: ${parsedResult.meaning}'); @@ -221,7 +221,7 @@ final SEARCH_URI = jisho_parser.uriForExampleSearch(SEARCH_EXAMPLE); void main() async { await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseExamplePageHtml(result.body, SEARCH_EXAMPLE); + final parsedResult = jisho_parser.parseExamplePageData(result.body, SEARCH_EXAMPLE); print('English: ${parsedResult.results[0].english}'); print('Kanji ${parsedResult.results[0].kanji}'); print('Kana: ${parsedResult.results[0].kana}'); @@ -244,7 +244,7 @@ final SEARCH_URI = jisho_parser.uriForPhraseScrape(SEARCH_EXAMPLE); void main() async { await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parsePhraseScrapeHtml(result.body, SEARCH_EXAMPLE); + final parsedResult = jisho_parser.parsePhrasePageData(result.body, SEARCH_EXAMPLE); print(encoder.convert(parsedResult)); }); } diff --git a/example/parser/parse_example_page.dart b/example/parser/parse_example_page.dart index a9649f1..0b90182 100644 --- a/example/parser/parse_example_page.dart +++ b/example/parser/parse_example_page.dart @@ -9,7 +9,7 @@ final SEARCH_URI = jisho_parser.uriForExampleSearch(SEARCH_EXAMPLE); void main() async { await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseExamplePageHtml(result.body, SEARCH_EXAMPLE); + final parsedResult = jisho_parser.parseExamplePageData(result.body, SEARCH_EXAMPLE); print('English: ${parsedResult.results[0].english}'); print('Kanji ${parsedResult.results[0].kanji}'); print('Kana: ${parsedResult.results[0].kana}'); diff --git a/example/parser/parse_kanji_page.dart b/example/parser/parse_kanji_page.dart index 133f79c..7f92aee 100644 --- a/example/parser/parse_kanji_page.dart +++ b/example/parser/parse_kanji_page.dart @@ -9,7 +9,7 @@ final SEARCH_URI = jisho_parser.uriForKanjiSearch(SEARCH_KANJI); void main() async { await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parseKanjiPageHtml(result.body, SEARCH_KANJI); + final parsedResult = jisho_parser.parseKanjiPageData(result.body, SEARCH_KANJI); print('JLPT level: ${parsedResult.jlptLevel}'); print('Stroke count: ${parsedResult.strokeCount}'); print('Meaning: ${parsedResult.meaning}'); diff --git a/example/parser/parse_phrase_page.dart b/example/parser/parse_phrase_page.dart index a248aff..6adb004 100644 --- a/example/parser/parse_phrase_page.dart +++ b/example/parser/parse_phrase_page.dart @@ -10,7 +10,7 @@ final SEARCH_URI = jisho_parser.uriForPhraseScrape(SEARCH_EXAMPLE); void main() async { await http.get(SEARCH_URI).then((result) { - final parsedResult = jisho_parser.parsePhraseScrapeHtml(result.body, SEARCH_EXAMPLE); + final parsedResult = jisho_parser.parsePhrasePageData(result.body, SEARCH_EXAMPLE); print(encoder.convert(parsedResult)); }); } \ No newline at end of file diff --git a/lib/parser.dart b/lib/parser.dart index f033d1a..6e2c316 100644 --- a/lib/parser.dart +++ b/lib/parser.dart @@ -4,30 +4,8 @@ /// This might be useful for using a CORS proxy, or if you have your own system/library /// for providing HTML. 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 +export './src/phraseSearch.dart'; +export './src/kanjiSearch.dart' show uriForKanjiSearch, parseKanjiPageData; +export './src/exampleSearch.dart' show uriForExampleSearch, parseExamplePageData; +export './src/phraseScrape.dart' show uriForPhraseScrape, parsePhrasePageData; \ No newline at end of file diff --git a/lib/src/exampleSearch.dart b/lib/src/exampleSearch.dart index f8eba01..e9ed310 100644 --- a/lib/src/exampleSearch.dart +++ b/lib/src/exampleSearch.dart @@ -6,6 +6,7 @@ import 'package:html/dom.dart'; final RegExp kanjiRegex = RegExp(r'[\u4e00-\u9faf\u3400-\u4dbf]'); +/// Provides the URI for an example search String uriForExampleSearch(String phrase) { return '${SCRAPE_BASE_URI}${Uri.encodeComponent(phrase)}%23sentences'; } @@ -107,6 +108,7 @@ ExampleResultData parseExampleDiv(Element div) { return result; } +/// Parses a jisho example sentence search page to an object ExampleResults parseExamplePageData(String pageHtml, String phrase) { final document = parse(pageHtml); final divs = document.querySelectorAll('.sentence_content'); diff --git a/lib/src/kanjiSearch.dart b/lib/src/kanjiSearch.dart index 53adcee..d4efb94 100644 --- a/lib/src/kanjiSearch.dart +++ b/lib/src/kanjiSearch.dart @@ -12,6 +12,7 @@ String removeNewlines(String str) { return str.replaceAll(RegExp(r'(?:\r|\n)') , '').trim(); } +/// Provides the URI for a kanji search String uriForKanjiSearch(String kanji) { return '${SCRAPE_BASE_URI}${Uri.encodeComponent(kanji)}%23kanji'; } @@ -187,6 +188,7 @@ int getNewspaperFrequencyRank(String pageHtml) { return (frequencySection != null) ? int.parse(getStringBetweenStrings(frequencySection, '', '')) : null; } +/// Parses a jisho kanji search page to an object KanjiResult parseKanjiPageData(String pageHtml, String kanji) { final result = KanjiResult(); result.query = kanji; diff --git a/lib/src/phraseScrape.dart b/lib/src/phraseScrape.dart index 3324cf9..053c61a 100644 --- a/lib/src/phraseScrape.dart +++ b/lib/src/phraseScrape.dart @@ -137,10 +137,12 @@ PhrasePageScrapeResult getMeaningsOtherFormsAndNotes(Document document) { return returnValues; } +/// Provides the URI for a phrase scrape String uriForPhraseScrape(String searchTerm) { return 'https://jisho.org/word/${Uri.encodeComponent(searchTerm)}'; } +/// Parses a jisho word search page to an object PhrasePageScrapeResult parsePhrasePageData(String pageHtml, String query) { final document = parse(pageHtml); final result = getMeaningsOtherFormsAndNotes(document); diff --git a/lib/src/phraseSearch.dart b/lib/src/phraseSearch.dart index 211ceb3..fb5da74 100644 --- a/lib/src/phraseSearch.dart +++ b/lib/src/phraseSearch.dart @@ -1,5 +1,7 @@ import './baseURI.dart'; + +/// Provides the URI for a phrase search String uriForPhraseSearch(String phrase) { return '${JISHO_API}?keyword=${Uri.encodeComponent(phrase)}'; } \ No newline at end of file