Skip to content

Commit

Permalink
Add warning print for unexpected null values
Browse files Browse the repository at this point in the history
  • Loading branch information
Hedon-dev committed Sep 29, 2024
1 parent 87983fa commit a2f21af
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
49 changes: 49 additions & 0 deletions lib/backend/universal_formats.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,23 @@ class UniversalSearchResult {
// convert all dynamics to strings, as logger only accepts strings
logger.d(result.map((key, value) => MapEntry(key, value.toString())));
}

/// Print values that are null, but the plugin didn't expect to be null
// TODO: Set up automatic/user prompted reporting
void printNullKeys(String pluginCodeName, List<String> exceptions) {
Map<String, dynamic> objectAsMap = convertToMap();
List<String> nullKeys = [];
// Check whether key is not in exception list and whether value is null
objectAsMap.forEach((key, value) {
if (!exceptions.contains(key) && value == null) {
nullKeys.add(key);
}
});
if (nullKeys.isNotEmpty) {
logger.w(
"$pluginCodeName: UniversalSearchResult ($videoID): Failed to scrape keys: $nullKeys");
}
}
}

class UniversalVideoMetadata {
Expand Down Expand Up @@ -249,6 +266,23 @@ class UniversalVideoMetadata {
void printAllAttributes() {
logger.d(convertToMap());
}

/// Print values that are null, but the plugin didn't expect to be null
// TODO: Set up automatic/user prompted reporting
void printNullKeys(String pluginCodeName, List<String> exceptions) {
Map<String, dynamic> objectAsMap = convertToMap();
List<String> nullKeys = [];
// Check whether key is not in exception list and whether value is null
objectAsMap.forEach((key, value) {
if (!exceptions.contains(key) && value == null) {
nullKeys.add(key);
}
});
if (nullKeys.isNotEmpty) {
logger.w(
"$pluginCodeName: UniversalVideoMetadata ($videoID): Failed to scrape keys: $nullKeys");
}
}
}

class UniversalComment {
Expand Down Expand Up @@ -314,4 +348,19 @@ class UniversalComment {
void printAllAttributes() {
logger.d(convertToMap());
}

/// Print values that are null, but the plugin didn't expect to be null
// TODO: Set up automatic/user prompted reporting
void printNullKeys(String pluginCodeName, List<String> exceptions) {
Map<String, dynamic> objectAsMap = convertToMap();
List<String> nullKeys = [];
// Check whether key is not in exception list and whether value is null
objectAsMap.forEach((key, value) {
if (!exceptions.contains(key) && value == null) {
nullKeys.add(key);
}
});
logger.d(
"$pluginCodeName: UniversalComment ($commentID): Failed to scrape keys: $nullKeys");
}
}
23 changes: 17 additions & 6 deletions lib/plugins/official_plugins/pornhub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,27 @@ class PornhubPlugin extends PluginBase implements PluginInterface {
// TODO: determine video resolution
// pornhub only offers up to 1080p

results.add(UniversalSearchResult(
UniversalSearchResult uniResult = UniversalSearchResult(
videoID: iD ?? "-",
title: title ?? "-",
plugin: this,
author: author ?? "-",
// All authors on pornhub are verified
verifiedAuthor: true,
thumbnail: thumbnail,
videoPreview: videoPreview != null ? Uri.parse(videoPreview) : null,
duration: duration,
viewsTotal: views,
ratingsPositivePercent: ratings,
maxQuality: null,
virtualReality: virtualReality,
));
author: author,
// All authors on pornhub are verified
verifiedAuthor: true,
);

// print warnings if some data is missing
uniResult.printNullKeys(codeName,
["thumbnailBinary", "lastWatched", "firstWatched", "maxQuality"]);

results.add(uniResult);
} catch (e) {
displayError("Failed to scrape video result: $e");
}
Expand Down Expand Up @@ -397,7 +403,7 @@ class PornhubPlugin extends PluginBase implements PluginInterface {
}
}

return UniversalVideoMetadata(
UniversalVideoMetadata metadata = UniversalVideoMetadata(
videoID: videoId,
m3u8Uris: m3u8Map,
title: jscriptMap["video_title"] ?? "-",
Expand All @@ -417,6 +423,11 @@ class PornhubPlugin extends PluginBase implements PluginInterface {
virtualReality: jscriptMap["isVR"] == 1,
chapters: null,
rawHtml: rawHtml);

// print warnings if some data is missing
metadata.printNullKeys(codeName, ["tags", "uploadDate", "chapters"]);

return metadata;
}

@override
Expand Down
25 changes: 22 additions & 3 deletions lib/plugins/official_plugins/xhamster.dart
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class XHamsterPlugin extends PluginBase implements PluginInterface {
}
}

results.add(UniversalSearchResult(
UniversalSearchResult uniResult = UniversalSearchResult(
videoID: iD ?? "-",
title: title ?? "-",
plugin: this,
Expand All @@ -211,7 +211,20 @@ class XHamsterPlugin extends PluginBase implements PluginInterface {
ratingsPositivePercent: null,
maxQuality: resolution,
virtualReality: virtualReality,
));
author: author,
// Set to false if null or if the author is "Unknown amateur author"
verifiedAuthor: author != null && author != "Unknown amateur author",
);

// print warnings if some data is missing
uniResult.printNullKeys(codeName, [
"thumbnailBinary",
"lastWatched",
"firstWatched",
"ratingsPositivePercent"
]);

results.add(uniResult);
}
} catch (e) {
displayError("Failed to scrape video result: $e");
Expand Down Expand Up @@ -331,7 +344,8 @@ class XHamsterPlugin extends PluginBase implements PluginInterface {
// convert master m3u8 to list of media m3u8
Map<int, Uri> m3u8Map =
await parseM3U8(Uri.parse(videoM3u8.attributes["href"]!));
return UniversalVideoMetadata(

UniversalVideoMetadata metadata = UniversalVideoMetadata(
videoID: videoId,
m3u8Uris: m3u8Map,
title: videoTitle.text,
Expand All @@ -350,6 +364,11 @@ class XHamsterPlugin extends PluginBase implements PluginInterface {
virtualReality: false,
chapters: null,
rawHtml: rawHtml);

// print warnings if some data is missing
metadata.printNullKeys(codeName, ["tags", "chapters"]);

return metadata;
}
}

Expand Down

0 comments on commit a2f21af

Please sign in to comment.