Files
frosty/lib/apis/seventv_api.dart
Tommy Chow bc67aad670 Fix 7TV emotes not using custom names (#291)
* Fix 7TV emotes not using custom set names

* Show real name in emote details
2023-11-01 17:55:49 -04:00

76 lines
2.4 KiB
Dart

import 'dart:convert';
import 'package:frosty/models/badges.dart';
import 'package:frosty/models/emotes.dart';
import 'package:http/http.dart';
/// The 7TV service for making API calls.
class SevenTVApi {
final Client _client;
const SevenTVApi(this._client);
/// Returns a map of global 7TV emotes to their URL.
Future<List<Emote>> getEmotesGlobal() async {
final url = Uri.parse('https://7tv.io/v3/emote-sets/global');
final response = await _client.get(url);
if (response.statusCode == 200) {
final decoded = jsonDecode(response.body)['emotes'] as List;
final emotes = decoded.map((emote) => Emote7TV.fromJson(emote));
return emotes
.map((emote) => Emote.from7TV(emote, EmoteType.sevenTVGlobal))
.toList();
} else {
return Future.error('Failed to get 7TV global emotes');
}
}
/// Returns a map of a channel's 7TV emotes to their URL.
Future<List<Emote>> getEmotesChannel({required String id}) async {
final url = Uri.parse('https://7tv.io/v3/users/twitch/$id');
final response = await _client.get(url);
if (response.statusCode == 200) {
final decoded = jsonDecode(response.body)['emote_set']['emotes'] as List;
final emotes = decoded.map((emote) => Emote7TV.fromJson(emote));
return emotes
.map((emote) => Emote.from7TV(emote, EmoteType.sevenTVChannel))
.toList();
} else {
return Future.error('Failed to get 7TV channel emotes');
}
}
/// Returns a map of user IDS to a list of their 7TV badges.
Future<Map<String, List<ChatBadge>>> getBadges() async {
final url =
Uri.parse('https://api.7tv.app/v2/badges?user_identifier=twitch_id');
final response = await _client.get(url);
if (response.statusCode == 200) {
final decoded = jsonDecode(response.body)['badges'] as List;
final badges = decoded.map((emote) => BadgeInfo7TV.fromJson(emote));
final result = <String, List<ChatBadge>>{};
for (final badge in badges) {
for (final userId in badge.users) {
final entry = result[userId];
final normalBadge = ChatBadge.from7TV(badge);
if (entry == null) {
result[userId] = [normalBadge];
} else {
entry.add(normalBadge);
}
}
}
return result;
} else {
return Future.error('Failed to get 7TV badges');
}
}
}