2024-08-26 15:21:22 +00:00
|
|
|
library song_link;
|
|
|
|
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:html/parser.dart';
|
|
|
|
|
|
|
|
part 'model.dart';
|
|
|
|
|
|
|
|
part 'song_link.freezed.dart';
|
|
|
|
part 'song_link.g.dart';
|
|
|
|
|
|
|
|
abstract class SongLinkService {
|
|
|
|
static Future<List<SongLink>> links(String spotifyId) async {
|
|
|
|
try {
|
|
|
|
final client = GetConnect();
|
|
|
|
final res = await client.get(
|
2024-08-27 06:48:31 +00:00
|
|
|
'https://song.link/s/$spotifyId',
|
2024-08-26 15:21:22 +00:00
|
|
|
headers: {
|
2024-08-27 06:48:31 +00:00
|
|
|
'Accept':
|
|
|
|
'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
|
|
|
|
'User-Agent':
|
|
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36'
|
2024-08-26 15:21:22 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
final document = parse(res.body);
|
|
|
|
|
2024-08-27 06:48:31 +00:00
|
|
|
final script = document.getElementById('__NEXT_DATA__')?.text;
|
2024-08-26 15:21:22 +00:00
|
|
|
|
|
|
|
if (script == null) {
|
|
|
|
return <SongLink>[];
|
|
|
|
}
|
|
|
|
|
|
|
|
final pageProps = jsonDecode(script) as Map<String, dynamic>;
|
2024-08-27 06:48:31 +00:00
|
|
|
final songLinks = pageProps['props']?['pageProps']?['pageData']
|
|
|
|
?['sections']
|
2024-08-26 15:21:22 +00:00
|
|
|
?.firstWhere(
|
2024-08-27 06:48:31 +00:00
|
|
|
(section) => section?['sectionId'] == 'section|auto|links|listen',
|
|
|
|
)?['links'] as List?;
|
2024-08-26 15:21:22 +00:00
|
|
|
|
|
|
|
return songLinks?.map((link) => SongLink.fromJson(link)).toList() ??
|
|
|
|
<SongLink>[];
|
|
|
|
} catch (e) {
|
|
|
|
log('[SongLink] Unable get song link: $e');
|
|
|
|
return <SongLink>[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|