diff --git a/README.md b/README.md index 792a228..8d3a677 100644 --- a/README.md +++ b/README.md @@ -181,6 +181,75 @@ This outputs the following: } ``` +## Parsing HTML strings + +You can provide the HTML responses from Jisho yourself. This can be useful if you need to use a CORS proxy or something. You can do whatever you need to do to get the HTML and then provide it to this module's parsing functions. For example: + +### Parse kanji page HTML + +```dart +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; + +final encoder = JsonEncoder.withIndent(' '); + +const SEARCH_KANJI = '車'; +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); + print('JLPT level: ${parsedResult.jlptLevel}'); + print('Stroke count: ${parsedResult.strokeCount}'); + print('Meaning: ${parsedResult.meaning}'); + }); +} +``` + +### Parse example page HTML + +```dart +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; + +final encoder = JsonEncoder.withIndent(' '); + +const SEARCH_EXAMPLE = '保護者'; +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); + print('English: ${parsedResult.results[0].english}'); + print('Kanji ${parsedResult.results[0].kanji}'); + print('Kana: ${parsedResult.results[0].kana}'); + }); +} +``` + +### Parse phrase page HTML + +```dart +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; + +final encoder = JsonEncoder.withIndent(' '); + +const SEARCH_EXAMPLE = '保護者'; +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); + print(encoder.convert(parsedResult)); + }); +} +``` + ## About Permission to scrape granted by Jisho's admin Kimtaro: https://jisho.org/forum/54fefc1f6e73340b1f160000-is-there-any-kind-of-search-api \ No newline at end of file diff --git a/example/example_search_example.dart b/example/api/example_search.dart similarity index 92% rename from example/example_search_example.dart rename to example/api/example_search.dart index 662b788..4baa4af 100644 --- a/example/example_search_example.dart +++ b/example/api/example_search.dart @@ -6,7 +6,7 @@ void main() async { print('Jisho Uri: ' + result.uri); print(''); - for (int i = 0; i < 3; i++) { + for (var i = 0; i < 3; i++) { var example = result.results[i]; print(example.kanji); print(example.kana); diff --git a/example/kanji_search_example.dart b/example/api/kanji_search.dart similarity index 100% rename from example/kanji_search_example.dart rename to example/api/kanji_search.dart diff --git a/example/phrase_scrape_example.dart b/example/api/phrase_scrape.dart similarity index 100% rename from example/phrase_scrape_example.dart rename to example/api/phrase_scrape.dart diff --git a/example/phrase_search_example.dart b/example/api/phrase_search.dart similarity index 100% rename from example/phrase_search_example.dart rename to example/api/phrase_search.dart diff --git a/example/parser/parse_example_page.dart b/example/parser/parse_example_page.dart new file mode 100644 index 0000000..a9649f1 --- /dev/null +++ b/example/parser/parse_example_page.dart @@ -0,0 +1,17 @@ +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; + +final encoder = JsonEncoder.withIndent(' '); + +const SEARCH_EXAMPLE = '保護者'; +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); + print('English: ${parsedResult.results[0].english}'); + print('Kanji ${parsedResult.results[0].kanji}'); + print('Kana: ${parsedResult.results[0].kana}'); + }); +} \ No newline at end of file diff --git a/example/parser/parse_kanji_page.dart b/example/parser/parse_kanji_page.dart new file mode 100644 index 0000000..133f79c --- /dev/null +++ b/example/parser/parse_kanji_page.dart @@ -0,0 +1,17 @@ +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; + +final encoder = JsonEncoder.withIndent(' '); + +const SEARCH_KANJI = '車'; +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); + print('JLPT level: ${parsedResult.jlptLevel}'); + print('Stroke count: ${parsedResult.strokeCount}'); + print('Meaning: ${parsedResult.meaning}'); + }); +} \ No newline at end of file diff --git a/example/parser/parse_phrase_page.dart b/example/parser/parse_phrase_page.dart new file mode 100644 index 0000000..a248aff --- /dev/null +++ b/example/parser/parse_phrase_page.dart @@ -0,0 +1,16 @@ +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:unofficial_jisho_api/parser.dart' as jisho_parser; + +final encoder = JsonEncoder.withIndent(' '); + +const SEARCH_EXAMPLE = '保護者'; +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); + print(encoder.convert(parsedResult)); + }); +} \ No newline at end of file