extractPerfume function
Implementation
Perfume? extractPerfume(BeautifulSoup bs, PerfumeRef ref) {
List<Rating> ratings = [];
List<SmileyRating> smileyRatings = [];
List<Accord> accords = [];
String? description;
String? quote;
List<ImageProvider> images = [];
List<ProOrCon> pros = [];
List<ProOrCon> cons = [];
List<RatingCategory> ratingCategories = [];
List<Note> topNotes = [];
List<Note> middleNotes = [];
List<Note> baseNotes = [];
// accords
List<Bs4Element> accordBars = bs.findAll('*', class_: 'accord-bar');
accords = accordBars
.map((accordBar) => _extractAccord(accordBar))
.nonNulls
.toList();
// description and quote
Bs4Element? descriptionAndQuoteBox =
bs.find('*', attrs: {'itemprop': "description"});
if (descriptionAndQuoteBox != null) {
var paragraph = descriptionAndQuoteBox.children.elementAtOrNull(0);
var quoteBox = descriptionAndQuoteBox.children.elementAtOrNull(1);
description = paragraph?.text;
quote = quoteBox?.text;
}
// pros and cons
var proAndConBox =
bs.findAll('*', class_: 'grid-x grid-margin-x').elementAtOrNull(6);
if (proAndConBox != null) {
var prosBox = proAndConBox.children.elementAtOrNull(0);
var consBox = proAndConBox.children.elementAtOrNull(1);
if (prosBox != null && consBox != null) {
pros = _extractProOrCons(prosBox);
cons = _extractProOrCons(consBox);
}
}
// pyramid
var topPyramid = bs.find('pyramid-level', attrs: {'notes': "top"});
var middlePyramid = bs.find('pyramid-level', attrs: {'notes': "middle"});
var basePyramid = bs.find('pyramid-level', attrs: {'notes': "base"});
if (topPyramid != null && middlePyramid != null && basePyramid != null) {
topNotes = _extractPyramidNotes(topPyramid);
middleNotes = _extractPyramidNotes(middlePyramid);
baseNotes = _extractPyramidNotes(basePyramid);
}
// images
var fragCarousel = bs.find('*', class_: 'fragramcarousel');
var imageElements =
fragCarousel?.findAll('*', class_: 'carousel-cell-photo') ?? [];
for (var image in imageElements) {
var imgSrc = image.find('img')?.getAttrValue("src");
if (imgSrc != null) {
images.add(Image.network(imgSrc).image);
}
}
if (description != null && quote != null) {
return Perfume(
producer: ref.producer,
id: ref.id,
ratings: ratings,
smileyRatings: smileyRatings,
accords: accords,
description: description,
quote: quote,
pros: pros,
cons: cons,
images: images,
topNotes: topNotes,
middleNotes: middleNotes,
baseNotes: baseNotes,
ratingCategories: ratingCategories);
}
return null;
}