Add parser library
This commit is contained in:
parent
9557960c46
commit
29ade01eed
12
README.md
12
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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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));
|
||||
});
|
||||
}
|
|
@ -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<JishoAPIResult> 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<KanjiResult> searchForKanji(String kanji) async {
|
||||
final uri = uriForKanjiSearch(kanji);
|
||||
return http.get(uri).then((response) => parseKanjiPageData(response.body, kanji));
|
||||
}
|
||||
|
||||
/// Scrape Jisho.org for examples.
|
||||
Future<ExampleResults> 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<PhrasePageScrapeResult> 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;
|
||||
}
|
||||
}
|
|
@ -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);
|
|
@ -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<JishoAPIResult> 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<KanjiResult> 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<ExampleResults> 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<PhrasePageScrapeResult> 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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
/// Support for doing something awesome.
|
||||
///
|
||||
/// More dartdocs go here.
|
||||
library unofficial_jisho_api;
|
||||
|
||||
export 'src/unofficial_jisho_api_base.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);
|
||||
}
|
||||
|
|
|
@ -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<String> getFilePaths(String dirname) {
|
||||
final currentdir = Directory.current.path;
|
||||
final filenames = Directory(path.join(currentdir, 'test', dirname)).listSync();
|
||||
|
@ -25,7 +23,7 @@ void runTestCases(List<String> 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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue