Update emotes API to use new common emote class

This commit is contained in:
tommyxchow 2021-12-12 16:04:17 -05:00
parent 934732770a
commit 522dc377a0
5 changed files with 30 additions and 58 deletions

View file

@ -5,27 +5,23 @@ import 'package:http/http.dart' as http;
class BTTV {
/// Returns a map of global BTTV emotes to their URL.
static Future<Map<String, String>?> getEmotesGlobal() async {
static Future<List<Emote>> getEmotesGlobal() async {
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;
final emotes = decoded.map((emote) => EmoteBTTVGlobal.fromJson(emote)).toList();
final emotes = decoded.map((emote) => EmoteBTTV.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.code] = 'https://cdn.betterttv.net/emote/${emote.id}/3x';
}
return emoteToUrl;
return emotes.map((emote) => Emote.fromBTTV(emote, EmoteType.bttvGlobal)).toList();
} else {
debugPrint('Failed to get global BTTV emotes. Error code: ${response.statusCode}');
return [];
}
}
/// Returns a map of a channel's BTTV emotes to their URL.
static Future<Map<String, String>?> getEmotesChannel({required String id}) async {
static Future<List<Emote>> getEmotesChannel({required String id}) async {
final url = Uri.parse('https://api.betterttv.net/3/cached/users/twitch/$id');
final response = await http.get(url);
@ -33,17 +29,14 @@ class BTTV {
final decoded = jsonDecode(response.body);
final result = EmoteBTTVChannel.fromJson(decoded);
final emoteToUrl = <String, String>{};
for (final emote in result.channelEmotes) {
emoteToUrl[emote.code] = 'https://cdn.betterttv.net/emote/${emote.id}/3x';
}
for (final emote in result.sharedEmotes) {
emoteToUrl[emote.code] = 'https://cdn.betterttv.net/emote/${emote.id}/3x';
}
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)));
return emoteToUrl;
} else {
debugPrint('Failed to get BTTV emotes for id: $id. Error code: ${response.statusCode}');
return [];
}
}
}

View file

@ -5,7 +5,7 @@ import 'package:http/http.dart' as http;
class FFZ {
/// Returns a map of global FFZ emotes to their URL.
static Future<Map<String, String>?> getEmotesGlobal() async {
static Future<List<Emote>> getEmotesGlobal() async {
final url = Uri.parse('https://api.betterttv.net/3/cached/frankerfacez/emotes/global');
final response = await http.get(url);
@ -13,19 +13,15 @@ class FFZ {
final decoded = jsonDecode(response.body) as List;
final emotes = decoded.map((emote) => EmoteFFZ.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.code] = emote.images.url4x ?? emote.images.url1x;
}
return emoteToUrl;
return emotes.map((emote) => Emote.fromFFZ(emote, EmoteType.ffzGlobal)).toList();
} else {
debugPrint('Failed to get global FFZ emotes. Error code: ${response.statusCode}');
return [];
}
}
/// Returns a map of a channel's FFZ emotes to their URL.
static Future<Map<String, String>?> getEmotesChannel({required String id}) async {
static Future<List<Emote>> getEmotesChannel({required String id}) async {
final url = Uri.parse('https://api.betterttv.net/3/cached/frankerfacez/users/twitch/$id');
final response = await http.get(url);
@ -33,14 +29,10 @@ class FFZ {
final decoded = jsonDecode(response.body) as List;
final emotes = decoded.map((emote) => EmoteFFZ.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.code] = emote.images.url4x ?? emote.images.url1x;
}
return emoteToUrl;
return emotes.map((emote) => Emote.fromFFZ(emote, EmoteType.ffzChannel)).toList();
} else {
debugPrint('Failed to get FFZ emotes for id: $id. Error code: ${response.statusCode}');
return [];
}
}
}

View file

@ -1,6 +1,7 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:frosty/models/badges.dart';
import 'package:frosty/models/emotes.dart';
import 'package:frosty/models/irc.dart';
class IRC {
@ -52,7 +53,7 @@ class IRC {
/// Returns an [InlineSpan] list that corresponds to the badges, username, words, and emotes of the given [IRCMessage].
static List<InlineSpan> generateSpan({
required IRCMessage ircMessage,
required Map<String, String> emoteToUrl,
required Map<String, Emote> emoteToObject,
required Map<String, BadgeInfoTwitch> badgeToObject,
bool hideMessage = false,
}) {
@ -154,7 +155,7 @@ class IRC {
for (final word in words) {
buffer.write(' ');
final emoteUrl = emoteToUrl[word] ?? localEmoteToUrl[word];
final emoteUrl = emoteToObject[word]?.url ?? localEmoteToUrl[word];
if (emoteUrl != null) {
span.add(TextSpan(text: buffer.toString()));

View file

@ -6,7 +6,7 @@ import 'package:http/http.dart' as http;
// A class for general requests.
class SevenTV {
/// Returns a map of global 7TV emotes to their URL.
static Future<Map<String, String>?> getEmotesGlobal() async {
static Future<List<Emote>> getEmotesGlobal() async {
final url = Uri.parse('https://api.7tv.app/v2/emotes/global');
final response = await http.get(url);
@ -14,18 +14,15 @@ class SevenTV {
final decoded = jsonDecode(response.body) as List;
final emotes = decoded.map((emote) => Emote7TV.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.name] = emote.urls[3][1];
}
return emoteToUrl;
return emotes.map((emote) => Emote.from7TV(emote, EmoteType.sevenTvGlobal)).toList();
} else {
debugPrint('Failed to get global 7TV emotes. Error code: ${response.statusCode}');
return [];
}
}
/// Returns a map of a channel's 7TV emotes to their URL.
static Future<Map<String, String>?> getEmotesChannel({required String user}) async {
static Future<List<Emote>> getEmotesChannel({required String user}) async {
final url = Uri.parse('https://api.7tv.app/v2/users/$user/emotes');
final response = await http.get(url);
@ -33,13 +30,10 @@ class SevenTV {
final decoded = jsonDecode(response.body) as List;
final emotes = decoded.map((emote) => Emote7TV.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.name] = emote.urls[3][1];
}
return emoteToUrl;
return emotes.map((emote) => Emote.from7TV(emote, EmoteType.sevenTvChannel)).toList();
} else {
debugPrint('Failed to get channel 7TV emotes. Error code: ${response.statusCode}');
return [];
}
}
}

View file

@ -11,7 +11,7 @@ import 'package:frosty/models/user.dart';
import 'package:http/http.dart' as http;
class Twitch {
static Future<Map<String, String>?> getEmotesGlobal({required Map<String, String>? headers}) async {
static Future<List<Emote>> getEmotesGlobal({required Map<String, String>? headers}) async {
final url = Uri.parse('https://api.twitch.tv/helix/chat/emotes/global');
final response = await http.get(url, headers: headers);
@ -19,19 +19,15 @@ class Twitch {
final decoded = jsonDecode(response.body)['data'] as List;
final emotes = decoded.map((emote) => EmoteTwitch.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.name] = 'https://static-cdn.jtvnw.net/emoticons/v2/${emote.id}/default/dark/3.0';
}
return emoteToUrl;
return emotes.map((emote) => Emote.fromTwitch(emote, EmoteType.twitchGlobal)).toList();
} else {
debugPrint('Failed to get global Twitch emotes. Error code: ${response.statusCode}');
return [];
}
}
/// Returns a map of a channel's Twitch emotes to their URL.
static Future<Map<String, String>?> getEmotesChannel({required String id, required Map<String, String>? headers}) async {
static Future<List<Emote>> getEmotesChannel({required String id, required Map<String, String>? headers}) async {
final url = Uri.parse('https://api.twitch.tv/helix/chat/emotes?broadcaster_id=$id');
final response = await http.get(url, headers: headers);
@ -39,14 +35,10 @@ class Twitch {
final decoded = jsonDecode(response.body)['data'] as List;
final emotes = decoded.map((emote) => EmoteTwitch.fromJson(emote)).toList();
final emoteToUrl = <String, String>{};
for (final emote in emotes) {
emoteToUrl[emote.name] = 'https://static-cdn.jtvnw.net/emoticons/v2/${emote.id}/default/dark/3.0';
}
return emoteToUrl;
return emotes.map((emote) => Emote.fromTwitch(emote, EmoteType.twitchChannel)).toList();
} else {
debugPrint('Failed to get Twitch emotes for id: $id. Error code: ${response.statusCode}');
return [];
}
}