1. Duolingo4D
Dart 中最易用的 Duolingo API 封装库!
Duolingo4D 是一个开源的 Dart 库。
使用 Duolingo4D,您可以轻松地将您的应用程序与 Duolingo API 集成。
Duolingo4D 是一个非官方库。
1.1. 支持的 Duolingo API
| 名称 | 需要身份验证 | 备注 |
|---|---|---|
| 版本信息 | ❌ | 您可以获取 Duolingo 服务各种配置的元数据。 |
| 认证 | ❌ | 通过使用 Duolingo 注册的用户名或电子邮件地址和密码来验证用户。 |
| 用户 | ✅ | 您可以获取详细的用户信息。 |
| 概述 | ✅ | 您可以获取用户在 Duolingo 中学习过的所有单词的信息。 |
| 单词提示 | ❌ | 您可以获取单词和句子的提示信息。 |
| 切换语言 | ✅ | 您可以切换学习语言。 |
1.2. 介绍
1.2.1. 安装库
使用 Dart
dart pub add duolingo4d
使用 Flutter
flutter pub add duolingo4d
1.2.2. 导入
import 'package:duolingo4d/duolingo4d.dart';
1.2.3. 使用 Duolingo4D
最简单的使用方法是获取 Duolingo 的单例实例并调用每个方法。
在内部处理 HTTP 通信时,Duolingo 方法返回的所有响应实体都包含状态码和头信息。
例如,在为用户执行身份验证过程时,它会是这样的
void main() async {
final duolingo = Duolingo.getInstance();
final authResponse = await duolingo.authenticate(
username: 'test_username',
password: 'test_password',
);
if (authResponse.statusCode != 200) {
// Client or Server error or something.
authResponse.reasonPhrase;
authResponse.headers;
return;
}
if (authResponse.hasError) {
// When there is an error on authentication.
final authError = authResponse.error!;
print(authError.code);
print(authError.reason);
authError.isInvalidUser; // Returns true if user is invalid.
authError.isInvalidPassword; // Returns true if password is invalid.
return;
}
// Do something if user is authenticated.
}
1.3. 使用方法
1.3.1. 版本信息 API
| 需要身份验证 | 代码片段 | JSON |
|---|---|---|
| ❌ | final response = await Duolingo.getInstance().versionInfo(); | 检查! |
版本信息 API 允许您获取 Duolingo 服务配置的元数据。
这些元数据包括用于播放单词音频的 TTS 数据,以及 Duolingo 支持的语言的映射信息。
void main() async {
final duolingo = Duolingo.getInstance();
final versionInfoResponse = await duolingo.versionInfo();
final ttsVoiceConfiguration = versionInfoResponse.ttsVoiceConfiguration;
for (final voiceDirections in ttsVoiceConfiguration.voiceDirections) {
print(voiceDirections.language);
print(voiceDirections.voice);
}
}
1.3.2. 身份验证 API
| 需要身份验证 | 代码片段 | JSON |
|---|---|---|
| ❌ | final response = await Duolingo.getInstance().authenticate(username: ‘test_username’, password: ‘test_password’,); | 检查! |
您可以使用用户名和密码来验证注册于 Duolingo 的用户。除了用户名,您还可以使用注册的电子邮件地址进行身份验证。
为了从每个需要身份验证的 API 中获取您期望的响应,首先您需要使用此身份验证 API 来验证输入的用户名和密码是否有效。
笔记
当用户通过身份验证后,Duolingo4D 会在内部管理 Duolingo 服务的 cookie 信息和会话信息,因此在调用 API 后无需关注这些信息。
void main() async {
final duolingo = Duolingo.getInstance();
final authResponse = await duolingo.authenticate(
username: 'test_username',
password: 'test_password',
);
if (authResponse.status.isNotOk) {
// Client or Server error or something.
authResponse.status.reasonPhrase;
authResponse.headers;
return;
}
if (authResponse.hasError) {
// When there is an error on authentication.
final authError = authResponse.error!;
print(authError.code);
print(authError.reason);
authError.isInvalidUser; // Returns true if user is invalid.
authError.isInvalidPassword; // Returns true if password is invalid.
return;
}
// Do something if user is authenticated.
}
笔记
如果您尝试使用已验证账户的信息再次进行身份验证,Duolingo API 将返回无效密码的响应。
1.3.3. 用户 API
| 需要身份验证 | 代码片段 | JSON |
|---|---|---|
| ✅ | final response = await Duolingo.getInstance().user(userId: authResponse.userId); | 检查! |
通过用户 API,您可以获取与用户 ID 关联的详细用户信息。
用户 ID 包含在用户身份验证完成后返回的响应中。有关详细信息,请参阅以下实现。
用户信息还包括用户学过的所有课程信息和技能信息。
如果用户身份验证未完成,则无法获取数据。
void main() async {
final duolingo = Duolingo.getInstance();
final authResponse = await duolingo.authenticate(
username: 'test_username',
password: 'test_password',
);
final userResponse = await duolingo.user(userId: authResponse.userId);
print(userResponse.name);
print(userResponse.lingots);
for (final course in userResponse.courses) {
print(course.title);
print(course.xp);
}
for (final skill in userResponse.currentCourse.skills) {
if (skill.isAccessible) {
print(skill.name);
print(skill.proficiency);
print(skill.tipsAndNotes);
}
}
}
1.3.4. 概览 API
| 需要身份验证 | 代码片段 | JSON |
|---|---|---|
| ✅ | final response = await Duolingo.getInstance().overview(); | 检查! |
通过概览 API,您可以获取所选课程中所有已学单词的信息。有关单词的详细信息会因课程和单词而异。
如果用户身份验证未完成,则无法获取数据。
void main() async {
final duolingo = Duolingo.getInstance();
final authResponse = await duolingo.authenticate(
username: 'test_username',
password: 'test_password',
);
final overviewResponse = await duolingo.overview();
for (final vocabulary in overviewResponse.vocabularies) {
print(vocabulary.word);
}
}
1.3.5. 单词提示 API
| 需要身份验证 | 代码片段 | JSON |
|---|---|---|
| ❌ | final response = await Duolingo.getInstance().wordHint(fromLanguage: ‘en’, learningLanguage: ‘es’, sentence: ‘boligrafos’,); | 检查! |
通过单词提示 API,您可以获取任何单词或句子的提示信息。由于提示信息是 Duolingo 管理的提示数据,因此无法保证翻译数据的准确性。
参数 learningLanguage 应为单词对应的语言代码,fromLanguage 应为提示对应的语言代码。sentence 可以是单个单词,也可以是句子。
void main() async {
final duolingo = Duolingo.getInstance();
final wordHintResponse = await duolingo.wordHint(
fromLanguage: 'en',
learningLanguage: 'es',
sentence: 'boligrafos',
);
for (final token in wordHintResponse.tokens) {
final headers = token.table.headers;
for (final header in headers) {
print(header.selected);
print(header.token);
}
final rows = token.table.rows;
for (final row in rows) {
for (final cell in row.cells) {
print(cell.hint);
}
}
}
}
1.3.6. 切换语言 API
| 需要身份验证 | 代码片段 |
|---|---|
| ✅ | final response = await Duolingo.getInstance().switchLanguage(fromLanguage: ‘es’, learningLanguage: ‘en’,); |
切换语言 API 允许您在 Duolingo 支持的课程之间进行切换。可以使用切换语言 API 进行切换的课程映射信息可以从版本信息 API 中获取。
对于参数 learningLanguage,指定切换后的学习语言。对于 fromLanguage,指定用于学习 learningLanguage 的语言。
void main() async {
final duolingo = Duolingo.getInstance();
final authResponse = await duolingo.authenticate(
username: 'test_username',
password: 'test_password',
);
final switchLanguageResponse = await duolingo.switchLanguage(
fromLanguage: 'es',
learningLanguage: 'en',
);
print(switchLanguageResponse.statusCode);
print(switchLanguageResponse.reasonPhrase);
}
1.4. 许可证
Copyright (c) 2021, Kato Shinya. All rights reserved.
Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
1.5. 更多信息
Duolingo4D 由 加藤 慎也 设计和实现。
无论沟通方式或内容如何,如果您有任何疑问或顾虑,我都很乐意倾听。我不会经常查看我的邮箱,所以回复可能会延迟,但感谢您的关注!