save new movie
This commit is contained in:
+183
-34
@@ -1,6 +1,44 @@
|
||||
import 'package:logger/logger.dart';
|
||||
import 'package:multimedia/log_printer.dart';
|
||||
|
||||
int getInt(Map<dynamic, dynamic> json, String key) {
|
||||
if (json.containsKey(key)) {
|
||||
return json[key] + 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool getBool(Map<dynamic, dynamic> json, String key) {
|
||||
if (json.containsKey(key)) {
|
||||
return json[key];
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
double getDouble(Map<dynamic, dynamic> json, String key) {
|
||||
if (json.containsKey(key)) {
|
||||
return json[key] + 0.0;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
String getString(Map<dynamic, dynamic> json, String key) {
|
||||
if (json.containsKey(key)) {
|
||||
if (json[key] == null) {
|
||||
return '';
|
||||
}
|
||||
return json[key];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
DateTime getDateTime(Map<dynamic, dynamic> json, String key) {
|
||||
if (json.containsKey(key)) {
|
||||
return DateTime.parse(json[key].toString());
|
||||
}
|
||||
return DateTime(1900);
|
||||
}
|
||||
|
||||
class Movie {
|
||||
int movieID;
|
||||
String movieTitle;
|
||||
@@ -130,42 +168,153 @@ class Movie {
|
||||
}
|
||||
|
||||
factory Movie.fromJsonDynamic(Map<dynamic, dynamic> json) {
|
||||
// dynamic languages = json['languages'];
|
||||
// dynamic productionCountries = json['productionCountries'];
|
||||
// dynamic productionCompanies = json['productionCompanies'];
|
||||
// dynamic cast = json['cast'];
|
||||
// dynamic crew = json['crew'];
|
||||
// dynamic genre = json['genre'];
|
||||
int movieID = getInt(json, 'id');
|
||||
String movieTitle = getString(json, 'title');
|
||||
String originalTitle = getString(json, 'original_title');
|
||||
String overview = getString(json, 'overview');
|
||||
DateTime releaseDate = getDateTime(json, 'release_date');
|
||||
int state = 1;
|
||||
String resolution = '';
|
||||
String backdropPath = getString(json, 'backdrop_path');
|
||||
String posterPath = getString(json, 'poster_path');
|
||||
double voteAverage = getDouble(json, 'vote_average');
|
||||
int voteCount = getInt(json, 'vote_count');
|
||||
String localPath = '';
|
||||
dynamic externalIDs = json['external_ids'];
|
||||
String imdbID = getString(externalIDs, 'imdb_id');
|
||||
double popularity = getDouble(json, 'popularity');
|
||||
String originalLanguage = getString(json, 'original_language');
|
||||
int adult = getBool(json, 'adult') == true ? 1 : 0;
|
||||
double budget = getDouble(json, 'budget');
|
||||
String homepage = getString(json, 'homepage');
|
||||
double revenue = getDouble(json, 'revenue');
|
||||
int runtime = getInt(json, 'runtime');
|
||||
String tagline = getString(json, 'tagline');
|
||||
|
||||
dynamic languages = json['spoken_languages'];
|
||||
dynamic productionCountries = json['production_countries'];
|
||||
dynamic productionCompanies = json['production_companies'];
|
||||
dynamic credits = json['credits'];
|
||||
dynamic genres = json['genres'];
|
||||
dynamic casts = credits['cast'];
|
||||
dynamic crews = credits['crew'];
|
||||
String belongsToCollection = '';
|
||||
|
||||
String languageList = '';
|
||||
String castList = '';
|
||||
String crewList = '';
|
||||
String genreList = '';
|
||||
String productionCompaniesList = '';
|
||||
String productionCountriesList = '';
|
||||
bool first;
|
||||
|
||||
if (json.containsKey('belongsToCollection')) {
|
||||
belongsToCollection = json['belongsToCollection'];
|
||||
}
|
||||
|
||||
first = true;
|
||||
for (var i = 0; i < languages.length; i++) {
|
||||
dynamic language = languages[i];
|
||||
String languageName = language['name'];
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
languageList = languageList + ',';
|
||||
}
|
||||
languageList = languageName;
|
||||
}
|
||||
|
||||
first = true;
|
||||
for (var i = 0; i < casts.length; i++) {
|
||||
dynamic cast = casts[i];
|
||||
String castName = cast['name'];
|
||||
String castChar = cast['character'];
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
castList = castList + '|';
|
||||
}
|
||||
castList = castList + castName + ',' + castChar;
|
||||
}
|
||||
|
||||
first = true;
|
||||
for (var i = 0; i < crews.length; i++) {
|
||||
dynamic crew = crews[i];
|
||||
String crewName = crew['name'];
|
||||
String crewDepartment = crew['department'];
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
crewList = crewList + '|';
|
||||
}
|
||||
crewList = crewList + crewName + ',' + crewDepartment;
|
||||
}
|
||||
|
||||
first = true;
|
||||
for (var i = 0; i < productionCountries.length; i++) {
|
||||
dynamic productionCountry = productionCountries[i];
|
||||
String name = productionCountry['name'];
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
productionCountriesList = productionCountriesList + ',';
|
||||
}
|
||||
productionCountriesList = productionCountriesList + name;
|
||||
}
|
||||
|
||||
first = true;
|
||||
for (var i = 0; i < productionCompanies.length; i++) {
|
||||
dynamic productionCompany = productionCompanies[i];
|
||||
String name = productionCompany['name'];
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
productionCountriesList = productionCompaniesList + ',';
|
||||
}
|
||||
productionCompaniesList = productionCompaniesList + name;
|
||||
}
|
||||
|
||||
first = true;
|
||||
for (var i = 0; i < genres.length; i++) {
|
||||
dynamic genre = genres[i];
|
||||
String genreName = genre['name'];
|
||||
if (first) {
|
||||
first = false;
|
||||
} else {
|
||||
genreList = genreList + ',';
|
||||
}
|
||||
genreList = genreList + genreName;
|
||||
}
|
||||
|
||||
return Movie(
|
||||
movieID: json['id'],
|
||||
movieTitle: json['title'],
|
||||
originalTitle: json['original_title'],
|
||||
overview: json['overview'],
|
||||
releaseDate: DateTime.parse(json['release_date'].toString()),
|
||||
state: int.tryParse(['status'].toString()) ?? 0,
|
||||
resolution: '',
|
||||
backdropPath: json['backdrop_path'],
|
||||
posterPath: '',
|
||||
languages: '',
|
||||
productionCountries: '',
|
||||
productionCompanies: '',
|
||||
voteAverage: double.tryParse(['vote_average'].toString()) ?? 0,
|
||||
voteCount: int.tryParse(['vote_count'].toString()) ?? 0,
|
||||
cast: '',
|
||||
crew: '',
|
||||
genre: '',
|
||||
localPath: '',
|
||||
imdbID: '',
|
||||
originalLanguage: '',
|
||||
popularity: 0,
|
||||
adult: false,
|
||||
belongsToCollection: '',
|
||||
budget: 0,
|
||||
homepage: '',
|
||||
revenue: 0,
|
||||
runtime: 0,
|
||||
tagline: '',
|
||||
movieID: movieID,
|
||||
movieTitle: movieTitle,
|
||||
originalTitle: originalTitle,
|
||||
overview: overview,
|
||||
releaseDate: releaseDate,
|
||||
state: state,
|
||||
resolution: resolution,
|
||||
backdropPath: backdropPath,
|
||||
posterPath: posterPath,
|
||||
languages: languageList,
|
||||
productionCountries: productionCountriesList,
|
||||
productionCompanies: productionCompaniesList,
|
||||
voteAverage: voteAverage,
|
||||
voteCount: voteCount,
|
||||
cast: castList,
|
||||
crew: crewList,
|
||||
genre: genreList,
|
||||
localPath: localPath,
|
||||
imdbID: imdbID,
|
||||
originalLanguage: originalLanguage,
|
||||
popularity: popularity,
|
||||
adult: adult == 1,
|
||||
belongsToCollection: belongsToCollection,
|
||||
budget: budget,
|
||||
homepage: homepage,
|
||||
revenue: revenue,
|
||||
runtime: runtime,
|
||||
tagline: tagline,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user