notube-export/lib/dlForm/cubit/dl_form_cubit.dart

70 lines
1.8 KiB
Dart

import 'dart:convert';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:notube/constants.dart';
import 'package:notube/models/video.dart';
import 'package:notube/services/download.dart';
part 'dl_form_state.dart';
class DlFormCubit extends Cubit<DlFormState> {
DlFormCubit() : super(DlFormInitial());
late DLServices dlService;
void setFormat(Format newFormat) {
emit(state.copyWith(
format: newFormat,
));
print(newFormat);
}
void setUrl(String newUrl) {
emit(state.copyWith(
url: newUrl,
));
}
void parseUrl() async {
dlService = await DLServices.init();
var shellStream = await dlService.analyseUrl(state.url);
shellStream.listen((line) {
if (line[0] == '{') {
var dataInfos = jsonDecode(line);
var extractor = dataInfos['extractor_key'];
var isPlaylist = dataInfos['_type'] == 'playlist';
var videos = <Video>[];
if (isPlaylist) {
var playlistTitle = dataInfos['title'];
for (var videoTmp in dataInfos['entries']) {
var video = Video.fromJson(videoTmp);
videos.add(video.copyWith(
format: state.format,
filename: '${video.id}_${state.format.format}'));
}
} else {
var video = Video.fromJson(dataInfos);
videos.add(video.copyWith(
format: state.format,
filename: '${video.id}_${state.format.format}'));
}
emit(state.copyWith(
isParsed: true,
videos: videos,
extractor: extractor,
));
}
});
}
void clearForm() {
emit(state.copyWith(
videos: [],
isParsed: false,
));
}
}