2021-10-03 18:08:51 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2022-01-01 00:58:06 -05:00
|
|
|
import 'package:frosty/models/badges.dart';
|
2021-10-03 18:08:51 -04:00
|
|
|
import 'package:frosty/models/emotes.dart';
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
|
|
|
|
class BTTV {
|
|
|
|
|
/// Returns a map of global BTTV emotes to their URL.
|
2021-12-12 16:04:17 -05:00
|
|
|
static Future<List<Emote>> getEmotesGlobal() async {
|
2021-10-03 18:08:51 -04:00
|
|
|
final url = Uri.parse('https://api.betterttv.net/3/cached/emotes/global');
|
|
|
|
|
final response = await http.get(url);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
final decoded = jsonDecode(response.body) as List;
|
2021-12-12 16:04:17 -05:00
|
|
|
final emotes = decoded.map((emote) => EmoteBTTV.fromJson(emote)).toList();
|
2021-10-03 18:08:51 -04:00
|
|
|
|
2021-12-12 16:04:17 -05:00
|
|
|
return emotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvGlobal)).toList();
|
2021-10-03 18:08:51 -04:00
|
|
|
} else {
|
|
|
|
|
debugPrint('Failed to get global BTTV emotes. Error code: ${response.statusCode}');
|
2021-12-12 16:04:17 -05:00
|
|
|
return [];
|
2021-10-03 18:08:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a map of a channel's BTTV emotes to their URL.
|
2021-12-12 16:04:17 -05:00
|
|
|
static Future<List<Emote>> getEmotesChannel({required String id}) async {
|
2021-10-03 18:08:51 -04:00
|
|
|
final url = Uri.parse('https://api.betterttv.net/3/cached/users/twitch/$id');
|
|
|
|
|
final response = await http.get(url);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
final decoded = jsonDecode(response.body);
|
|
|
|
|
final result = EmoteBTTVChannel.fromJson(decoded);
|
|
|
|
|
|
2021-12-12 16:04:17 -05:00
|
|
|
final emoteToUrl = <Emote>[];
|
|
|
|
|
emoteToUrl.addAll(result.channelEmotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvChannel)));
|
|
|
|
|
emoteToUrl.addAll(result.sharedEmotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvShared)));
|
2021-10-03 18:08:51 -04:00
|
|
|
|
|
|
|
|
return emoteToUrl;
|
|
|
|
|
} else {
|
|
|
|
|
debugPrint('Failed to get BTTV emotes for id: $id. Error code: ${response.statusCode}');
|
2021-12-12 16:04:17 -05:00
|
|
|
return [];
|
2021-10-03 18:08:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-01-01 00:58:06 -05:00
|
|
|
|
|
|
|
|
static Future<Map<String, BadgeInfoBTTV>?> getBadges() async {
|
|
|
|
|
final url = Uri.parse('https://api.betterttv.net/3/cached/badges');
|
|
|
|
|
final response = await http.get(url);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
final badges = jsonDecode(response.body) as List;
|
|
|
|
|
|
|
|
|
|
final badgeObjects = badges.map((badge) => BadgeInfoBTTV.fromJson(badge)).toList();
|
|
|
|
|
|
|
|
|
|
return {for (final badge in badgeObjects) badge.providerId: badge};
|
|
|
|
|
}
|
|
|
|
|
debugPrint('Failed to get FFZ badges');
|
|
|
|
|
}
|
2021-10-03 18:08:51 -04:00
|
|
|
}
|