mirror of
https://github.com/KRTirtho/spotube.git
synced 2025-09-13 16:05:18 +00:00
chore: add error handling in song link
This commit is contained in:
parent
96a2a1f5a6
commit
a850ed270b
@ -2,6 +2,7 @@ library song_link;
|
|||||||
|
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:catcher_2/catcher_2.dart';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||||
import 'package:html/parser.dart';
|
import 'package:html/parser.dart';
|
||||||
@ -12,9 +13,9 @@ part 'song_link.freezed.dart';
|
|||||||
part 'song_link.g.dart';
|
part 'song_link.g.dart';
|
||||||
|
|
||||||
abstract class SongLinkService {
|
abstract class SongLinkService {
|
||||||
|
static final dio = Dio();
|
||||||
static Future<List<SongLink>> links(String spotifyId) async {
|
static Future<List<SongLink>> links(String spotifyId) async {
|
||||||
final dio = Dio();
|
try {
|
||||||
|
|
||||||
final res = await dio.get(
|
final res = await dio.get(
|
||||||
"https://song.link/s/$spotifyId",
|
"https://song.link/s/$spotifyId",
|
||||||
options: Options(
|
options: Options(
|
||||||
@ -37,11 +38,17 @@ abstract class SongLinkService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final pageProps = jsonDecode(script) as Map<String, dynamic>;
|
final pageProps = jsonDecode(script) as Map<String, dynamic>;
|
||||||
final songLinks =
|
final songLinks = pageProps["props"]?["pageProps"]?["pageData"]
|
||||||
pageProps["props"]["pageProps"]["pageData"]["sections"].firstWhere(
|
?["sections"]
|
||||||
(section) => section["sectionId"] == "section|auto|links|listen",
|
?.firstWhere(
|
||||||
)["links"] as List;
|
(section) => section?["sectionId"] == "section|auto|links|listen",
|
||||||
|
)?["links"] as List?;
|
||||||
|
|
||||||
return songLinks.map((link) => SongLink.fromJson(link)).toList();
|
return songLinks?.map((link) => SongLink.fromJson(link)).toList() ??
|
||||||
|
<SongLink>[];
|
||||||
|
} catch (e, stackTrace) {
|
||||||
|
Catcher2.reportCheckedError(e, stackTrace);
|
||||||
|
return <SongLink>[];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user