Flutter播客索引管理插件podcastindex_dart的使用

Flutter播客索引管理插件podcastindex_dart的使用

介绍

此库提供了对PodcastIndex API的便捷访问,以便查找播客源(feeds)和集数(episodes)。

功能

目前只实现了PodcastIndex API中最重要的一部分(共实现15/40个端点)。 有关术语,请参阅官方的API文档

注意事项

  • 当前进度:已实现15/40个端点。
  • 说明:一个feed代表整个播客;一个episode代表特定feed的一个单独集数。PodcastIndex将feedpodcast互换使用,尽管有些混淆,我们在这里也这样做。

提示

  • 注意方法名中的单复数使用。例如,findFeedById 返回 Future<Feed>,而 findFeedsByTerm 返回 Future<List<Feed>>
  • 所有方法都返回 Future<...>,因为HTTP调用是异步的。只需在方法调用前使用 await,并将调用方法标记为 async
void f() async {
  List<Feed> results = await feedService.findFeedsByTerm("some term"); 

  // 注意,由于使用了 await,返回类型不是 Future
}

搜索功能

重要:搜索类别中的端点同时使用了 FeedServiceEpisodeService!最好一次性实例化两者。

名称 已实现? 端点 方法
按词搜索播客 /search/byterm FeedService.findFeedsByTerm
按标题搜索播客 /search/bytitle FeedService.findFeedsByTitle
按人搜索集数 /search/byperson EpisodeService.findEpisodesByPerson
搜索音乐播客 /search/music/byterm FeedService.findMusicFeedsByTerm

播客源(feeds)

名称 已实现? 端点 方法
按Feed ID /podcasts/byfeedid FeedService.findFeedById
按Feed URL /podcasts/byfeedurl FeedService.findFeedByUrl
按iTunes ID /podcasts/byitunesid FeedService.findFeedByItunesId
按GUID /podcasts/byguid FeedService.findFeedByPodcastGuid
按标签 /podcasts/bytag -
按媒体 /oodcasts/bymedium FeedService.findFeedByMedium
趋势 /podcasts/trending -
死亡 /podcasts/dead -

注意:GUID是播客的唯一全局标识符。详情请参见 guid 规范

集数(episodes)

名称 已实现? 端点 方法
按Feed ID /episodes/byfeedid EpisodeService.findEpisodesByFeedId
按Feed URL /episodes/byfeedurl EpisodeService.findEpisodesByFeedUrl
按Podcast GUID /episodes/bypodcastguid EpisodeService.findEpisodesByPodcastGuid
按GUID /episodes/byguid EpisodeService.findEpisodeByGuid
按iTunes ID /episodes/byitunesid -
按ID /episodes/byid EpisodeService.findEpisodeById
直播 /episodes/live EpisodeService.findLiveEpisodes
随机 /episodes/random -

警告:不要混淆!Podcast GUID 不等于 GUID。前者是该集数所属feed的GUID,后者是单个具体集数的GUID。最后,ID 是PodcastIndex内部的ID(一个整数)。

其他端点

PodcastIndex API 还暴露了许多其他端点!以下类别尚未实现:

  • Recent
  • Value
  • Stats
  • Categories
  • Hub
  • Add
  • Apple Replacement

请注意,您可以手动调用尚未实现的端点,使用以下代码:

HttpUtil.get(endpoint)

其中 endpoint 形式为 /some-endpoint。此函数会为您处理身份验证。返回的是标准的 Future<Response>

开始使用

  1. 安装库:

    dart pub add podcastindex_dart
    

    或者,如果使用Flutter:

    dart pub add podcastindex_dart
    
  2. 如果还没有 .env 文件,创建一个位于项目根目录的 .env 文件,并粘贴以下内容:

    PODCASTINDEX_API_KEY='your_api_key'
    PODCASTINDEX_API_SECRET='your_api_secret'
    

    您可以通过 这里 注册一个免费的PodcastIndex账户获取这些值。

  3. 在文件中导入库:

    import 'package:podcastindex_dart/src/entity/episode.dart';
    import 'package:podcastindex_dart/src/service/episode_service.dart';
    import 'package:podcastindex_dart/src/service/feed_service.dart';
    

使用方法

主要有两个服务处理API返回的两种基本类型:FeedServiceEpisodeService

只需实例化所需的服务:

var feedService = FeedService();
var episodeService = EpisodeService();

这些对象上的方法名称反映了PodcastIndex API端点的名称,且非常直观。

如需更多信息,请参阅 官方API文档

示例

假设我们要根据给定的词匹配所有播客(feeds)(典型搜索栏用途)。

这是如何实现的:

String term = searchBarInput.value; /*Dumb code, just for you to get the idea!*/

List<Feed> searchResults = feedService.findFeedsByTerm(term);

要限制搜索结果到前10个,我们可以修改代码以包含 max 可选参数:

List<Feed> searchResults = feedService.findFeedsByTerm(term, max: 10);

此外,为了排除明确内容,我们可以设置 clean 标志:

List<Feed> searchResults = feedService.findFeedsByTerm(term, max: 10, clean: true);

要播放一个集数(实际上是获取特定集数的流URL),我们可以这样写:

int episodeId = 16795089; // 通过其他调用找到
Episode episode = await episodeService.findEpisodeById(episodeId);
String playbackUrl = episode.enclosureUrl.toString();

audioplayer.play(playbackUrl); // Dumb code, to get the idea!

更多关于Flutter播客索引管理插件podcastindex_dart的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter播客索引管理插件podcastindex_dart的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


podcastindex_dart 是一个用于与 Podcast Index API 进行交互的 Dart 插件。Podcast Index 是一个开源的播客目录,提供了丰富的播客元数据和搜索功能。通过 podcastindex_dart,你可以在 Flutter 应用中轻松地集成 Podcast Index 的功能,如搜索播客、获取播客元数据、获取剧集列表等。

安装

首先,你需要在 pubspec.yaml 文件中添加 podcastindex_dart 依赖:

dependencies:
  flutter:
    sdk: flutter
  podcastindex_dart: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来安装依赖。

初始化

在使用 podcastindex_dart 之前,你需要初始化 PodcastIndex 对象。Podcast Index API 需要认证,因此你需要提供 API Key 和 API Secret。

import 'package:podcastindex_dart/podcastindex_dart.dart';

void main() {
  final podcastIndex = PodcastIndex(
    apiKey: 'YOUR_API_KEY',
    apiSecret: 'YOUR_API_SECRET',
  );
}

搜索播客

你可以使用 search 方法来搜索播客。以下是一个简单的示例:

void searchPodcasts(PodcastIndex podcastIndex) async {
  final result = await podcastIndex.search('Flutter');
  
  if (result.feeds != null) {
    for (var feed in result.feeds!) {
      print('Title: ${feed.title}');
      print('URL: ${feed.url}');
      print('Description: ${feed.description}');
      print('---');
    }
  }
}

获取播客元数据

你可以使用 getPodcastByFeedUrlgetPodcastById 方法来获取特定播客的元数据。

void getPodcastMetadata(PodcastIndex podcastIndex) async {
  final result = await podcastIndex.getPodcastByFeedUrl('https://example.com/feed.xml');
  
  if (result.feed != null) {
    print('Title: ${result.feed!.title}');
    print('Author: ${result.feed!.author}');
    print('Description: ${result.feed!.description}');
  }
}

获取播客剧集列表

你可以使用 getEpisodesByFeedUrlgetEpisodesById 方法来获取播客的剧集列表。

void getPodcastEpisodes(PodcastIndex podcastIndex) async {
  final result = await podcastIndex.getEpisodesByFeedUrl('https://example.com/feed.xml');
  
  if (result.items != null) {
    for (var episode in result.items!) {
      print('Episode Title: ${episode.title}');
      print('Published Date: ${episode.datePublished}');
      print('Duration: ${episode.duration}');
      print('---');
    }
  }
}

错误处理

在使用 API 时,建议添加错误处理逻辑,以应对网络问题或 API 返回的错误。

void getPodcastMetadataWithErrorHandling(PodcastIndex podcastIndex) async {
  try {
    final result = await podcastIndex.getPodcastByFeedUrl('https://example.com/feed.xml');
    
    if (result.feed != null) {
      print('Title: ${result.feed!.title}');
    }
  } catch (e) {
    print('Error: $e');
  }
}
回到顶部