unofficial_jisho_api_dart/example
Oystein Kristoffer Tveit a84cfe5c02 Update to 3.0.0
- Add fromJson factories for all objects
- Make some properties use getters, to reduce redundant data
- Make all objects comparable with the equatable package
- Make all objects immutable with a constant constructor
    as a result of making them comparable
2022-05-08 02:06:45 +02:00
..
api Update to 3.0.0 2022-05-08 02:06:45 +02:00
parser Upgrade to 2.0.0, see changelog 2021-07-25 22:57:02 +02:00
README.md Fix examples readme 2020-06-26 18:44:16 +02:00

README.md

Examples

Examples for use of the api functions, and for parsing.

These examples are also available on the main readme.

API searching/scraping

These are usage examples of the api library, providing results directly.

Word/phrase search (provided by official Jisho API)

This returns the same results as the official Jisho.org API. See the discussion of that here.

import 'package:unofficial_jisho_api/api.dart' as jisho;

void main() async {
  jisho.searchForPhrase('日').then((result) {
    ...
    ...
    ...
  });
}
import 'dart:convert' show jsonEncode;
import 'package:unofficial_jisho_api/api.dart' as jisho;

void main() async {
  await jisho.searchForKanji('語').then((result) {
    print('Found: ${result.found}');
    print('Taught in: ${result.taughtIn}');
    print('JLPT level: ${result.jlptLevel}');
    print('Newspaper frequency rank: ${result.newspaperFrequencyRank}');
    print('Stroke count: ${result.strokeCount}');
    print('Meaning: ${result.meaning}');
    print('Kunyomi: ${jsonEncode(result.kunyomi)}');
    print('Kunyomi example: ${jsonEncode(result.kunyomiExamples[0])}');
    print('Onyomi: ${jsonEncode(result.onyomi)}');
    print('Onyomi example: ${jsonEncode(result.onyomiExamples[0])}');
    print('Radical: ${jsonEncode(result.radical)}');
    print('Parts: ${jsonEncode(result.parts)}');
    print('Stroke order diagram: ${result.strokeOrderDiagramUri}');
    print('Stroke order SVG: ${result.strokeOrderSvgUri}');
    print('Stroke order GIF: ${result.strokeOrderGifUri}');
    print('Jisho Uri: ${result.uri}');
  });
}
import 'dart:convert' show jsonEncode;
import 'package:unofficial_jisho_api/api.dart' as jisho;

void main() async {
  await jisho.searchForExamples('日').then((result) {
    print('Jisho Uri: ${result.uri}');
    print('');

    for (var i = 0; i < 3; i++) {
      var example = result.results[i];
      print(example.kanji);
      print(example.kana);
      print(example.english);
      print(jsonEncode(example.pieces));
      print('');
    }
  });
}

Word/phrase scraping

This scrapes the word/phrase page on Jisho.org. This can get you some data that the official API doesn't have, such as JLPT level and part-of-speech. The official API (searchForPhrase) should be preferred if it has the data you need.

import 'dart:convert';
import 'package:unofficial_jisho_api/api.dart' as jisho;
final JsonEncoder encoder = JsonEncoder.withIndent('  ');

void main() async {
  await jisho.scrapeForPhrase('谷').then((data) {
    print(encoder.convert(data));
  });
}

Parsing

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

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:unofficial_jisho_api/parser.dart' as jisho_parser;

final JsonEncoder encoder = JsonEncoder.withIndent('  ');

const String searchKanji = '車';
final String searchURI = jisho_parser.uriForKanjiSearch(searchKanji);

void main() async {
  await http.get(searchURI).then((result) {
    final parsedResult = jisho_parser.parseKanjiPageData(result.body, searchKanji);
    print('JLPT level: ${parsedResult.jlptLevel}');
    print('Stroke count: ${parsedResult.strokeCount}');
    print('Meaning: ${parsedResult.meaning}');
  });
}

Parse example page HTML

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:unofficial_jisho_api/parser.dart' as jisho_parser;

final JsonEncoder encoder = JsonEncoder.withIndent('  ');

const String searchExample = '保護者';
final String searchURI = jisho_parser.uriForExampleSearch(searchExample);

void main() async {
  await http.get(searchURI).then((result) {
    final parsedResult = jisho_parser.parseExamplePageData(result.body, searchExample);
    print('English: ${parsedResult.results[0].english}');
    print('Kanji ${parsedResult.results[0].kanji}');
    print('Kana: ${parsedResult.results[0].kana}');
  });
}

Parse phrase page HTML

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:unofficial_jisho_api/parser.dart' as jisho_parser;

final JsonEncoder encoder = JsonEncoder.withIndent('  ');

const String searchExample = '保護者';
final String searchURI = jisho_parser.uriForPhraseScrape(searchExample);

void main() async {

  await http.get(searchURI).then((result) {
    final parsedResult = jisho_parser.parsePhrasePageData(result.body, searchExample);
    print(encoder.convert(parsedResult));
  });
}