Skip to content

Commit

Permalink
feat: replace YouTube API with piped API
Browse files Browse the repository at this point in the history
  • Loading branch information
Kingkor Roy Tirtho committed May 14, 2023
1 parent edb3d47 commit 1ecc36d
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 183 deletions.
26 changes: 9 additions & 17 deletions lib/components/player/sibling_tracks_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:piped_client/piped_client.dart';

import 'package:spotube/components/shared/image/universal_image.dart';
import 'package:spotube/extensions/context.dart';
import 'package:spotube/models/spotube_track.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/utils/primitive_utils.dart';
import 'package:youtube_explode_dart/youtube_explode_dart.dart';

class SiblingTracksSheet extends HookConsumerWidget {
final bool floating;
Expand All @@ -26,7 +26,7 @@ class SiblingTracksSheet extends HookConsumerWidget {

final siblings = playlist.isFetching == false
? (playlist.activeTrack as SpotubeTrack).siblings
: <Video>[];
: <PipedSearchItemStream>[];

final borderRadius = floating
? BorderRadius.circular(10)
Expand Down Expand Up @@ -77,7 +77,7 @@ class SiblingTracksSheet extends HookConsumerWidget {
leading: Padding(
padding: const EdgeInsets.all(8.0),
child: UniversalImage(
path: video.thumbnails.lowResUrl,
path: video.thumbnail,
height: 60,
width: 60,
),
Expand All @@ -86,26 +86,18 @@ class SiblingTracksSheet extends HookConsumerWidget {
borderRadius: BorderRadius.circular(5),
),
trailing: Text(
PrimitiveUtils.toReadableDuration(
video.duration ?? Duration.zero,
),
PrimitiveUtils.toReadableDuration(video.duration),
),
subtitle: Text(video.author),
subtitle: Text(video.uploaderName),
enabled: playlist.isFetching != true,
selected: playlist.isFetching != true &&
video.id.value ==
(playlist.activeTrack as SpotubeTrack)
.ytTrack
.id
.value,
video.id ==
(playlist.activeTrack as SpotubeTrack).ytTrack.id,
selectedTileColor: theme.popupMenuTheme.color,
onTap: () async {
if (playlist.isFetching == false &&
video.id.value !=
(playlist.activeTrack as SpotubeTrack)
.ytTrack
.id
.value) {
video.id !=
(playlist.activeTrack as SpotubeTrack).ytTrack.id) {
await playlistNotifier.swapSibling(video);
}
},
Expand Down
41 changes: 41 additions & 0 deletions lib/extensions/piped.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'dart:convert';

import 'package:catcher/catcher.dart';
import 'package:http/http.dart';
import 'package:piped_client/piped_client.dart';
import 'package:spotube/entities/cache_track.dart';
import 'package:spotube/models/logger.dart';
import 'package:spotube/models/track.dart';
import 'package:spotube/provider/user_preferences_provider.dart';
import 'package:spotube/services/youtube.dart';
import 'package:spotube/utils/duration.dart';

extension PipedSearchItemExtension on PipedSearchItem {
static PipedSearchItemStream fromCacheTrack(CacheTrack cacheTrack) {
return PipedSearchItemStream(
type: PipedSearchItemType.stream,
url: "watch?v=${cacheTrack.id}",
title: cacheTrack.title,
uploaderName: cacheTrack.author,
uploaderUrl: "/channel/${cacheTrack.channelId}",
uploaded: -1,
uploadedDate: cacheTrack.uploadDate ?? "",
shortDescription: cacheTrack.description,
isShort: false,
thumbnail: "",
duration: cacheTrack.duration != null
? tryParseDuration(cacheTrack.duration!) ?? Duration.zero
: Duration.zero,
uploaderAvatar: "",
uploaderVerified: false,
views: cacheTrack.engagement.viewCount,
);
}
}

extension PipedStreamResponseExtension on PipedStreamResponse {
static Future<PipedStreamResponse> fromBackendTrack(
BackendTrack track) async {
return await PipedSpotube.client.streams(track.youtubeId);
}
}
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Future<void> main(List<String> rawArgs) async {
exit(0);
}

await PipedSpotube.initialize();
final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();

FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
Expand Down Expand Up @@ -134,7 +135,6 @@ Future<void> main(List<String> rawArgs) async {
return Downloader(
ref,
queueInstance,
yt: youtube,
downloadPath: ref.watch(
userPreferencesProvider.select(
(s) => s.downloadLocation,
Expand Down Expand Up @@ -211,7 +211,7 @@ class SpotubeState extends ConsumerState<Spotube> {
/// For enabling hot reload for audio player
if (!kDebugMode) return;
audioPlayer.dispose();
youtube.close();
// youtube.close();
};
}, []);

Expand Down
Loading

0 comments on commit 1ecc36d

Please sign in to comment.