2021-10-03 18:08:51 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
2021-12-31 23:38:50 -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;
|
|
|
|
|
|
|
|
|
|
// A class for general requests.
|
|
|
|
|
class SevenTV {
|
|
|
|
|
/// Returns a map of global 7TV 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.7tv.app/v2/emotes/global');
|
|
|
|
|
final response = await http.get(url);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
final decoded = jsonDecode(response.body) as List;
|
2021-12-31 23:38:50 -05:00
|
|
|
final emotes = decoded.map((emote) => Emote7TV.fromJson(emote));
|
2021-10-03 18:08:51 -04:00
|
|
|
|
2021-12-12 16:04:17 -05:00
|
|
|
return emotes.map((emote) => Emote.from7TV(emote, EmoteType.sevenTvGlobal)).toList();
|
2021-10-03 18:08:51 -04:00
|
|
|
} else {
|
|
|
|
|
debugPrint('Failed to get global 7TV 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 7TV emotes to their URL.
|
2021-12-12 16:04:17 -05:00
|
|
|
static Future<List<Emote>> getEmotesChannel({required String user}) async {
|
2021-10-03 18:08:51 -04:00
|
|
|
final url = Uri.parse('https://api.7tv.app/v2/users/$user/emotes');
|
|
|
|
|
final response = await http.get(url);
|
|
|
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
|
final decoded = jsonDecode(response.body) as List;
|
2021-12-31 23:38:50 -05:00
|
|
|
final emotes = decoded.map((emote) => Emote7TV.fromJson(emote));
|
2021-10-03 18:08:51 -04:00
|
|
|
|
2021-12-12 16:04:17 -05:00
|
|
|
return emotes.map((emote) => Emote.from7TV(emote, EmoteType.sevenTvChannel)).toList();
|
2021-10-03 18:08:51 -04:00
|
|
|
} else {
|
|
|
|
|
debugPrint('Failed to get channel 7TV emotes. Error code: ${response.statusCode}');
|
2021-12-12 16:04:17 -05:00
|
|
|
return [];
|
2021-10-03 18:08:51 -04:00
|
|
|
}
|
|
|
|
|
}
|
2021-12-31 23:38:50 -05:00
|
|
|
|
|
|
|
|
static Future<Map<String, List<BadgeInfo7TV>>?> getBadges() async {
|
|
|
|
|
final url = Uri.parse('https://api.7tv.app/v2/badges?user_identifier=twitch_id');
|
|
|
|
|
final response = await http.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<BadgeInfo7TV>>{};
|
|
|
|
|
for (final badge in badges) {
|
|
|
|
|
for (final userId in badge.users) {
|
|
|
|
|
final entry = result[userId];
|
|
|
|
|
if (entry == null) {
|
|
|
|
|
result[userId] = [badge];
|
|
|
|
|
} else {
|
|
|
|
|
entry.add(badge);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
debugPrint('Failed to get channel 7TV emotes. Error code: ${response.statusCode}');
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-03 18:08:51 -04:00
|
|
|
}
|