summaryrefslogtreecommitdiff
path: root/plugins/af_redditimgur/init.php
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/af_redditimgur/init.php')
-rwxr-xr-xplugins/af_redditimgur/init.php79
1 files changed, 69 insertions, 10 deletions
diff --git a/plugins/af_redditimgur/init.php b/plugins/af_redditimgur/init.php
index 1aa4793ea..9fd93dde4 100755
--- a/plugins/af_redditimgur/init.php
+++ b/plugins/af_redditimgur/init.php
@@ -1,14 +1,15 @@
<?php
class Af_RedditImgur extends Plugin {
- /* @var PluginHost $host */
+ /** @var PluginHost $host */
private $host;
private $domain_blacklist = [ "github.com" ];
private $dump_json_data = false;
private $fallback_preview_urls = [];
+ private $default_max_score = 100;
function about() {
- return array(1.0,
+ return array(null,
"Inline images (and other content) in Reddit RSS feeds",
"fox");
}
@@ -34,6 +35,9 @@ class Af_RedditImgur extends Plugin {
$enable_readability = $this->host->get($this, "enable_readability");
$enable_content_dupcheck = $this->host->get($this, "enable_content_dupcheck");
$reddit_to_teddit = $this->host->get($this, "reddit_to_teddit");
+ $apply_nsfw_tags = $this->host->get_array($this, "apply_nsfw_tags");
+ $max_score = $this->host->get($this, "max_score", $this->default_max_score);
+ $import_score = $this->host->get($this, "import_score");
?>
<div dojoType="dijit.layout.AccordionPane"
@@ -54,6 +58,14 @@ class Af_RedditImgur extends Plugin {
</script>
<fieldset class='narrow'>
+ <label>
+ <?= __("Apply tags to NSFW posts (comma-separated list):") ?>
+ </label>
+ <input dojoType="dijit.form.TextBox" name="apply_nsfw_tags" size="20"
+ value="<?= htmlspecialchars(implode(", ", $apply_nsfw_tags)) ?>">
+ </fieldset>
+
+ <fieldset class='narrow'>
<label class='checkbox'>
<?= \Controls\checkbox_tag("enable_readability", $enable_readability) ?>
<?= __("Extract missing content using Readability (requires af_readability)") ?>
@@ -75,6 +87,15 @@ class Af_RedditImgur extends Plugin {
</label>
</fieldset>
+ <fieldset class='narrow'>
+ <label class='checkbox'>
+ <?= \Controls\checkbox_tag("import_score", $import_score) ?>
+ <?= __("Import score, limit maximum to:") ?>
+ <input dojoType="dijit.form.TextBox" name="max_score" size="20"
+ placeholder="<?= $this->default_max_score ?>" value="<?= $max_score ?>">
+ </label>
+ </fieldset>
+
<hr/>
<?= \Controls\submit_tag(__("Save")) ?>
</form>
@@ -87,10 +108,18 @@ class Af_RedditImgur extends Plugin {
$enable_readability = checkbox_to_sql_bool($_POST["enable_readability"] ?? "");
$enable_content_dupcheck = checkbox_to_sql_bool($_POST["enable_content_dupcheck"] ?? "");
$reddit_to_teddit = checkbox_to_sql_bool($_POST["reddit_to_teddit"] ?? "");
-
- $this->host->set($this, "enable_readability", $enable_readability, false);
- $this->host->set($this, "reddit_to_teddit", $reddit_to_teddit, false);
- $this->host->set($this, "enable_content_dupcheck", $enable_content_dupcheck);
+ $apply_nsfw_tags = FeedItem_Common::normalize_categories(explode(",", $_POST["apply_nsfw_tags"] ?? ""));
+ $import_score = checkbox_to_sql_bool($_POST["import_score"] ?? "");
+ $max_score = (int) $_POST['max_score'];
+
+ $this->host->set_array($this, [
+ "enable_readability" => $enable_readability,
+ "reddit_to_teddit" => $reddit_to_teddit,
+ "enable_content_dupcheck" => $enable_content_dupcheck,
+ "apply_nsfw_tags" => $apply_nsfw_tags,
+ "import_score" => $import_score,
+ "max_score" => $max_score
+ ]);
echo __("Configuration saved");
}
@@ -202,9 +231,20 @@ class Af_RedditImgur extends Plugin {
return $found;
}
- private function inline_stuff($article, &$doc, $xpath) {
+ /* function score_convert(int $value, int $from1, int $from2, int $to1, int $to2) {
+ return ($value - $from1) / ($from2 - $from1) * ($to2 - $to1) + $to1;
+ } */
+
+ private function inline_stuff(&$article, &$doc, $xpath) {
+
+ $max_score = (int) $this->host->get($this, "max_score", $this->default_max_score);
+ $import_score = (bool) $this->host->get($this, "import_score", $this->default_max_score);
$found = false;
+ $post_is_nsfw = false;
+ $num_comments = 0;
+ $score = 0;
+ $apply_nsfw_tags = FeedItem_Common::normalize_categories($this->host->get_array($this, "apply_nsfw_tags", []));
// embed before reddit <table> post layout
$anchor = $xpath->query('//body/*')->item(0);
@@ -230,6 +270,15 @@ class Af_RedditImgur extends Plugin {
foreach ($listing["data"]["children"] as $child) {
$data = $child["data"];
+ $over_18 = $data["over_18"] ?? 0 == 1;
+
+ $score += $data['score'] ?? 0;
+ $num_comments += $data["num_comments"] ?? 0;
+
+ if ($over_18) {
+ Debug::log("JSON: post is NSFW", Debug::$LOG_EXTENDED);
+ $post_is_nsfw = true;
+ }
if (isset($data["crosspost_parent_list"])) {
Debug::log("JSON: processing child crosspost_parent_list", Debug::$LOG_EXTENDED);
@@ -257,14 +306,22 @@ class Af_RedditImgur extends Plugin {
}
} else {
if (!$tmp) {
- global $fetch_last_error;
- Debug::log("JSON: failed to fetch post:" . $fetch_last_error, Debug::$LOG_EXTENDED);
+ Debug::log("JSON: failed to fetch post:" . UrlHelper::$fetch_last_error, Debug::$LOG_EXTENDED);
}
}
} else if (!$anchor) {
Debug::log("JSON: anchor element not found, unable to embed", Debug::$LOG_EXTENDED);
}
+ if ($post_is_nsfw && count($apply_nsfw_tags) > 0) {
+ $article["tags"] = array_merge($article["tags"], $apply_nsfw_tags);
+ }
+
+ $article["num_comments"] = $num_comments;
+
+ if ($import_score && $score > 0)
+ $article["score_modifier"] = ($article["score_modifier"] ?? 0) + ($score > $max_score ? $max_score : $score);
+
if ($found) {
Debug::log("JSON: found media data, skipping further processing of content", Debug::$LOG_VERBOSE);
$this->remove_post_thumbnail($doc, $xpath);
@@ -680,7 +737,9 @@ class Af_RedditImgur extends Plugin {
@$doc->loadHTML("<html><body><table><tr><td><a href=\"$url\">[link]</a></td></tr></table></body>");
$xpath = new DOMXPath($doc);
- $found = $this->inline_stuff(["link" => $article_url], $doc, $xpath);
+ $article = ["link" => $article_url, "tags" => []];
+
+ $found = $this->inline_stuff($article, $doc, $xpath);
Debug::log("Inline result: $found", Debug::$LOG_VERBOSE);