Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Fixed spotify to youtube conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroRojas committed Mar 12, 2024
1 parent d754d7f commit 22cd889
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
20 changes: 11 additions & 9 deletions lib/Helpers/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,9 @@ class FormatResponse {
return res?.toString();
});
String youtubeId = '';
if (cacheId == null) {
if (cacheId == null || cacheId == 'null') {
Logger.root.info(
'searching yt music search for ${element['name']} ${artists.join(', ')} ${element['album']?['name']}',
'searching yt music for ${element['name']} ${artists.join(', ')} ${element['album']?['name']}',
);
final List ytResult =
await YtMusicService().search("${element['name']} ${artists.join(
Expand All @@ -694,13 +694,15 @@ class FormatResponse {
} catch (e) {
youtubeId = (ytResult[1]['items'] as List).first['id'].toString();
} finally {
await MyApp.hiveMutex.protect(() async {
if (!Hive.isBoxOpen('spoty2youtube')) {
Hive.init(jsonObject['path'].toString());
}
await Hive.openBox('spoty2youtube');
await Hive.box('spoty2youtube').put(element['id'], youtubeId);
});
if (youtubeId != 'null' && youtubeId != null) {
await MyApp.hiveMutex.protect(() async {
if (!Hive.isBoxOpen('spoty2youtube')) {
Hive.init(jsonObject['path'].toString());
}
await Hive.openBox('spoty2youtube');
await Hive.box('spoty2youtube').put(element['id'], youtubeId);
});
}
}
} else {
youtubeId = cacheId;
Expand Down
21 changes: 18 additions & 3 deletions lib/Services/youtube_services.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,26 @@ class YouTubeServices {
return results;
}

Future<Video?> getVideoFromId(String id) async {
Future<Video?> getVideoFromId(String id, String? path) async {
try {
final Video result = await yt.videos.get(id);
return result;
} catch (e) {
if (e.toString().contains(('Invalid YouTube video ID or URL'))) {
//Remove video from cache as it's invalid
if (path != null) {
await MyApp.hiveMutex.protect(() async {
if (!Hive.isBoxOpen('spoty2youtube')) {
Hive.init(path);
}
});
}
await Hive.openBox('spoty2youtube');
Hive.box('spoty2youtube').delete(
Hive.box('spoty2youtube').keys.firstWhere(
(element) => Hive.box('spoty2youtube').get(element) == id),
);
}
Logger.root.severe('Error while getting video from id', e);
return null;
}
Expand All @@ -63,7 +78,7 @@ class YouTubeServices {
bool? getUrl,
String? path,
}) async {
final Video? vid = await getVideoFromId(id);
final Video? vid = await getVideoFromId(id, path);
if (vid == null) {
return null;
}
Expand Down Expand Up @@ -97,7 +112,7 @@ class YouTubeServices {
}

Future<Map?> refreshLink(String id) async {
final Video? res = await getVideoFromId(id);
final Video? res = await getVideoFromId(id, null);
if (res == null) {
return null;
}
Expand Down
29 changes: 16 additions & 13 deletions lib/Services/yt_music.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,22 @@ class YtMusicService {
}
}
}
final List idNav = (type == 'Song' || type == 'Video')
? [
'musicResponsiveListItemRenderer',
'playlistItemData',
'videoId'
]
: [
'musicResponsiveListItemRenderer',
'navigationEndpoint',
'browseEndpoint',
'browseId'
];
final String id = nav(childItem, idNav).toString();
String id;
if(type == 'Song' || type == 'Video')
{
id = nav(childItem, [
'musicResponsiveListItemRenderer',
'playlistItemData',
'videoId'
]).toString();
}
else {
id =
nav(nav(nav(
childItem, ['musicResponsiveListItemRenderer', 'flexColumns'])[0],
['musicResponsiveListItemFlexColumnRenderer', 'text', 'runs'])[0],
['navigationEndpoint','watchEndpoint', 'videoId']).toString();
}
sectionSearchResults.add({
'id': id,
'type': type,
Expand Down
2 changes: 1 addition & 1 deletion macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
DeviceInfoPlusMacosPlugin.register(with: registry.registrar(forPlugin: "DeviceInfoPlusMacosPlugin"))
FlutterArchivePlugin.register(with: registry.registrar(forPlugin: "FlutterArchivePlugin"))
JustAudioPlugin.register(with: registry.registrar(forPlugin: "JustAudioPlugin"))
FLTPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FLTPackageInfoPlusPlugin"))
FPPPackageInfoPlusPlugin.register(with: registry.registrar(forPlugin: "FPPPackageInfoPlusPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dependencies:
get_it: ^7.2.0
hive: ^2.2.3
hive_flutter: ^1.1.0
isolate_manager: ^3.0.0
isolate_manager: ^4.1.5
html_unescape: ^2.0.0
just_audio: ^0.9.33
# Discontinued, Use just_audio_windows instead.
Expand All @@ -45,7 +45,7 @@ dependencies:
marquee: ^2.2.3
material_design_icons_flutter: ^6.0.7096
mutex: ^3.0.1
metadata_god: ^0.4.1
metadata_god: ^0.5.2
on_audio_query: ^2.9.0
package_info_plus: ^4.0.0
palette_generator: ^0.3.3+2
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#include "generated_plugin_registrant.h"

#include <app_links/app_links_plugin_c_api.h>
#include <permission_handler_windows/permission_handler_windows_plugin.h>
#include <share_plus/share_plus_windows_plugin_c_api.h>
#include <url_launcher_windows/url_launcher_windows.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
AppLinksPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AppLinksPluginCApi"));
PermissionHandlerWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("PermissionHandlerWindowsPlugin"));
SharePlusWindowsPluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi"));
UrlLauncherWindowsRegisterWithRegistrar(
Expand Down
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

list(APPEND FLUTTER_PLUGIN_LIST
app_links
permission_handler_windows
share_plus
url_launcher_windows
)
Expand Down

0 comments on commit 22cd889

Please sign in to comment.