freezer/lib/api/spotify.dart

221 lines
6.8 KiB
Dart
Raw Normal View History

2020-06-25 12:28:56 +00:00
import 'package:freezer/api/deezer.dart';
2021-04-05 20:22:32 +00:00
import 'package:freezer/api/importer.dart';
import 'package:freezer/settings.dart';
2020-06-25 12:28:56 +00:00
import 'package:html/parser.dart';
import 'package:html/dom.dart' as dom;
import 'package:http/http.dart' as http;
2021-04-05 20:22:32 +00:00
import 'package:spotify/spotify.dart';
2020-06-25 12:28:56 +00:00
import 'dart:convert';
2021-04-05 20:22:32 +00:00
import 'dart:io';
2020-06-25 12:28:56 +00:00
2021-04-05 20:22:32 +00:00
import 'package:url_launcher/url_launcher.dart';
2020-06-25 12:28:56 +00:00
2021-04-05 20:22:32 +00:00
class SpotifyScrapper {
2020-06-25 12:28:56 +00:00
//Parse spotify URL to URI (spotify:track:1234)
2021-04-05 20:22:32 +00:00
static String parseUrl(String url) {
2020-06-25 12:28:56 +00:00
Uri uri = Uri.parse(url);
if (uri.pathSegments.length > 3) return null; //Invalid URL
if (uri.pathSegments.length == 3) return 'spotify:${uri.pathSegments[1]}:${uri.pathSegments[2]}';
if (uri.pathSegments.length == 2) return 'spotify:${uri.pathSegments[0]}:${uri.pathSegments[1]}';
return null;
}
//Get spotify embed url from uri
2021-04-05 20:22:32 +00:00
static String getEmbedUrl(String uri) => 'https://embed.spotify.com/?uri=$uri';
2020-06-25 12:28:56 +00:00
//https://link.tospotify.com/ or https://spotify.app.link/
2021-04-05 20:22:32 +00:00
static Future resolveLinkUrl(String url) async {
http.Response response = await http.get(Uri.parse(url));
Match match = RegExp(r'window\.top\.location = validate\("(.+)"\);').firstMatch(response.body);
return match.group(1);
}
2021-04-05 20:22:32 +00:00
static Future resolveUrl(String url) async {
if (url.contains("link.tospotify") || url.contains("spotify.app.link")) {
return parseUrl(await resolveLinkUrl(url));
}
return parseUrl(url);
}
2020-06-25 12:28:56 +00:00
//Extract JSON data form spotify embed page
2021-04-05 20:22:32 +00:00
static Future<Map> getEmbedData(String url) async {
2020-06-25 12:28:56 +00:00
//Fetch
http.Response response = await http.get(url);
2020-06-25 12:28:56 +00:00
//Parse
dom.Document document = parse(response.body);
dom.Element element = document.getElementById('resource');
//Some are URL encoded
try {
return jsonDecode(element.innerHtml);
} catch (e) {
return jsonDecode(Uri.decodeComponent(element.innerHtml));
}
2020-06-25 12:28:56 +00:00
}
2021-04-05 20:22:32 +00:00
static Future<SpotifyPlaylist> playlist(String uri) async {
2020-06-25 12:28:56 +00:00
//Load data
String url = getEmbedUrl(uri);
Map data = await getEmbedData(url);
//Parse
SpotifyPlaylist playlist = SpotifyPlaylist.fromJson(data);
return playlist;
}
//Get Deezer track ID from Spotify URI
2021-04-05 20:22:32 +00:00
static Future<String> convertTrack(String uri) async {
Map data = await getEmbedData(getEmbedUrl(uri));
SpotifyTrack track = SpotifyTrack.fromJson(data);
Map deezer = await deezerAPI.callPublicApi('track/isrc:' + track.isrc);
return deezer['id'].toString();
}
//Get Deezer album ID by UPC
2021-04-05 20:22:32 +00:00
static Future<String> convertAlbum(String uri) async {
Map data = await getEmbedData(getEmbedUrl(uri));
SpotifyAlbum album = SpotifyAlbum.fromJson(data);
Map deezer = await deezerAPI.callPublicApi('album/upc:' + album.upc);
return deezer['id'].toString();
}
2020-06-25 12:28:56 +00:00
}
class SpotifyTrack {
String title;
2021-04-05 20:22:32 +00:00
List<String> artists;
2020-06-25 12:28:56 +00:00
String isrc;
SpotifyTrack({this.title, this.artists, this.isrc});
//JSON
factory SpotifyTrack.fromJson(Map json) => SpotifyTrack(
title: json['name'],
2021-04-05 20:22:32 +00:00
artists: json['artists'].map<String>((a) => a["name"].toString()).toList(),
2020-06-25 12:28:56 +00:00
isrc: json['external_ids']['isrc']
);
2021-04-05 20:22:32 +00:00
//Convert track to importer track
ImporterTrack toImporter() {
return ImporterTrack(title, artists, isrc: isrc);
}
2020-06-25 12:28:56 +00:00
}
class SpotifyPlaylist {
String name;
String description;
List<SpotifyTrack> tracks;
String image;
SpotifyPlaylist({this.name, this.description, this.tracks, this.image});
//JSON
factory SpotifyPlaylist.fromJson(Map json) => SpotifyPlaylist(
name: json['name'],
description: json['description'],
image: (json['images'].length > 0) ? json['images'][0]['url'] : null,
2020-06-25 12:28:56 +00:00
tracks: json['tracks']['items'].map<SpotifyTrack>((j) => SpotifyTrack.fromJson(j['track'])).toList()
);
2021-04-05 20:22:32 +00:00
//Convert to importer tracks
List<ImporterTrack> toImporter() {
return tracks.map((t) => t.toImporter()).toList();
}
2020-06-25 12:28:56 +00:00
}
class SpotifyAlbum {
String upc;
SpotifyAlbum({this.upc});
//JSON
factory SpotifyAlbum.fromJson(Map json) => SpotifyAlbum(
upc: json['external_ids']['upc']
);
}
2021-04-05 20:22:32 +00:00
class SpotifyAPIWrapper {
HttpServer _server;
SpotifyApi spotify;
User me;
//Try authorize with saved credentials
Future<bool> trySaved() async {
print(settings.spotifyCredentials);
if (settings.spotifyClientId == null || settings.spotifyClientSecret == null || settings.spotifyCredentials == null) return false;
final credentials = SpotifyApiCredentials(
settings.spotifyClientId,
settings.spotifyClientSecret,
accessToken: settings.spotifyCredentials.accessToken,
refreshToken: settings.spotifyCredentials.refreshToken,
scopes: settings.spotifyCredentials.scopes,
expiration: settings.spotifyCredentials.expiration
);
spotify = SpotifyApi(credentials);
me = await spotify.me.get();
await _save();
return true;
}
2021-04-05 20:22:32 +00:00
Future authorize(String clientId, String clientSecret) async {
//Spotify
SpotifyApiCredentials credentials = SpotifyApiCredentials(clientId, clientSecret);
spotify = SpotifyApi(credentials);
//Create server
_server = await HttpServer.bind(InternetAddress.loopbackIPv4, 42069);
String responseUri;
//Get URL
final grant = SpotifyApi.authorizationCodeGrant(credentials);
final redirectUri = "http://localhost:42069";
final scopes = ['user-read-private', 'playlist-read-private', 'playlist-read-collaborative', 'user-library-read'];
final authUri = grant.getAuthorizationUrl(Uri.parse(redirectUri), scopes: scopes);
launch(authUri.toString());
//Wait for code
await for (HttpRequest request in _server) {
//Exit window
request.response.headers.set("Content-Type", "text/html; charset=UTF-8");
request.response.write("<body><h1>You can close this page and go back to Freezer.</h1></body><script>window.close();</script>");
request.response.close();
//Get token
if (request.uri.queryParameters["code"] != null) {
_server.close();
_server = null;
responseUri = request.uri.toString();
break;
}
}
//Create spotify
spotify = SpotifyApi.fromAuthCodeGrant(grant, responseUri);
me = await spotify.me.get();
//Save
await _save();
}
Future _save() async {
//Save credentials
final spotifyCredentials = await spotify.getCredentials();
final saveCredentials = SpotifyCredentialsSave(
accessToken: spotifyCredentials.accessToken,
refreshToken: spotifyCredentials.refreshToken,
scopes: spotifyCredentials.scopes,
expiration: spotifyCredentials.expiration
);
settings.spotifyClientSecret = spotifyCredentials.clientId;
settings.spotifyClientSecret = spotifyCredentials.clientSecret;
settings.spotifyCredentials = saveCredentials;
await settings.save();
2021-04-05 20:22:32 +00:00
}
//Cancel authorization
void cancelAuthorize() {
if (_server != null) {
_server.close(force: true);
_server = null;
}
}
}