diff --git a/example/video_download.dart b/example/video_download.dart index c8353b8..75ada7d 100644 --- a/example/video_download.dart +++ b/example/video_download.dart @@ -3,28 +3,24 @@ import 'dart:async'; import 'dart:io'; -import 'package:dart_console/dart_console.dart'; +import 'package:console/console.dart'; import 'package:youtube_explode_dart/youtube_explode_dart.dart'; // Initialize the YoutubeExplode instance. final yt = YoutubeExplode(); -final console = Console(); - Future main() async { - console.writeLine('Type the video id or url: '); + stdout.writeln('Type the video id or url: '); var url = stdin.readLineSync().trim(); // Save the video to the download directory. Directory('downloads').createSync(); - console.hideCursor(); // Download the video. await download(url); yt.close(); - console.showCursor(); exit(0); } @@ -42,7 +38,6 @@ Future download(String id) async { // Compose the file name removing the unallowed characters in windows. var fileName = '${video.title}.${audio.container.name.toString()}' - .replaceAll('Container.', '') .replaceAll(r'\', '') .replaceAll('/', '') .replaceAll('*', '') @@ -53,9 +48,12 @@ Future download(String id) async { .replaceAll('|', ''); var file = File('downloads/$fileName'); - // Create the StreamedRequest to track the download status. + // Delete the file if exists. + if (file.existsSync()) { + file.deleteSync(); + } - // Open the file in appendMode. + // Open the file in writeAppend. var output = file.openWrite(mode: FileMode.writeOnlyAppend); // Track the file download status. @@ -64,24 +62,23 @@ Future download(String id) async { var oldProgress = -1; // Create the message and set the cursor position. - var msg = 'Downloading `${video.title}`(.${audio.container.name}): \n'; - print(msg); - // var row = console.cursorPosition.row; -// var col = msg.length - 2; -// console.cursorPosition = Coordinate(row, 0); -// console.write(msg); + var msg = 'Downloading ${video.title}.${audio.container.name}'; + stdout.writeln(msg); // Listen for data received. + var progressBar = ProgressBar(); await for (var data in audioStream) { + // Keep track of the current downloaded data. count += data.length; - var progress = ((count / len) * 100).round(); - if (progress != oldProgress) { -// console.cursorPosition = Coordinate(row, col); - print('$progress%'); - oldProgress = progress; - } + + // Calculate the current progress. + var progress = ((count / len) * 100).ceil(); + + // Update the progressbar. + progressBar.update(progress); + + // Write to file. output.add(data); } - console.writeLine(); await output.close(); } diff --git a/lib/src/reverse_engineering/responses/player_source.dart b/lib/src/reverse_engineering/responses/player_source.dart index f5a2b13..ad1b318 100644 --- a/lib/src/reverse_engineering/responses/player_source.dart +++ b/lib/src/reverse_engineering/responses/player_source.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import '../../exceptions/exceptions.dart'; import '../../retry.dart'; import '../cipher/cipher_operations.dart'; @@ -105,12 +107,22 @@ class PlayerSource { // Same as default constructor PlayerSource.parse(this._root); - static Future get(YoutubeHttpClient httpClient, String url) { - return retry(() async { - var raw = await httpClient.getString(url); - return PlayerSource.parse(raw); - }); + static Future get( + YoutubeHttpClient httpClient, String url) async { + if (_cache[url] == null) { + Timer(const Duration(minutes: 10), () { + _cache[url] = null; + }); + + return _cache[url] = await retry(() async { + var raw = await httpClient.getString(url); + return PlayerSource.parse(raw); + }); + } + return _cache[url]; } + + static final Map _cache = {}; } extension on String { diff --git a/lib/src/reverse_engineering/youtube_http_client.dart b/lib/src/reverse_engineering/youtube_http_client.dart index e6be526..5bb3ab6 100644 --- a/lib/src/reverse_engineering/youtube_http_client.dart +++ b/lib/src/reverse_engineering/youtube_http_client.dart @@ -147,8 +147,8 @@ class YoutubeHttpClient extends http.BaseClient { request.headers[key] = _defaultHeaders[key]; } }); - //print('Request: $request'); - //print('Stack:\n${StackTrace.current}'); +// print('Request: $request'); +// print('Stack:\n${StackTrace.current}'); return _httpClient.send(request); } } diff --git a/pubspec.yaml b/pubspec.yaml index cf0723a..4fe8648 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: youtube_explode_dart description: A port in dart of the youtube explode library. Supports several API functions without the need of Youtube API Key. -version: 1.3.2 +version: 1.3.3 homepage: https://github.com/Hexer10/youtube_explode_dart environment: @@ -17,5 +17,5 @@ dependencies: dev_dependencies: effective_dart: ^1.2.1 - dart_console: ^0.5.0 + console: ^3.1.0 test: ^1.12.0