Make parser export functions directly

master
Oystein Kristoffer Tveit 2020-06-26 00:20:58 +02:00
parent b58c439418
commit d6509e6392
9 changed files with 18 additions and 32 deletions

View File

@ -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));
});
}

View File

@ -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}');

View File

@ -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}');

View File

@ -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));
});
}

View File

@ -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);
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;

View File

@ -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');

View File

@ -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, '<strong>', '</strong>')) : null;
}
/// Parses a jisho kanji search page to an object
KanjiResult parseKanjiPageData(String pageHtml, String kanji) {
final result = KanjiResult();
result.query = kanji;

View File

@ -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);

View File

@ -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)}';
}