From 3ee87bc75472706f24a90c208b384deef1a89cea Mon Sep 17 00:00:00 2001 From: sijanec Date: Sun, 18 Oct 2020 02:25:07 +0200 Subject: added bot --- bot/banned_subs.txt | 0 bot/banned_users.txt | 0 bot/mentions.php | 54 +++++++++++++++++++++++++++++++++++++++++++ bot/processed.txt | 1 + main.php | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++- mentions.php | 21 +++++++++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 bot/banned_subs.txt create mode 100644 bot/banned_users.txt create mode 100755 bot/mentions.php create mode 100644 bot/processed.txt create mode 100644 mentions.php diff --git a/bot/banned_subs.txt b/bot/banned_subs.txt new file mode 100644 index 0000000..e69de29 diff --git a/bot/banned_users.txt b/bot/banned_users.txt new file mode 100644 index 0000000..e69de29 diff --git a/bot/mentions.php b/bot/mentions.php new file mode 100755 index 0000000..0b4345e --- /dev/null +++ b/bot/mentions.php @@ -0,0 +1,54 @@ +#!/usr/bin/env php +setusername(getenv("orC_user")); + $o->setpassword(getenv("orC_pass")); + $botsignature = "\n\n-- [/u/ImgLinkBot](/u/ImgLinkBot) | [ImgLinkBot.ž.ga](https://imglinkbot.xn--jha.ga/)"; + while (true) { + $mentions = $o->fetch("mentions"); + if ($mentions < 0) { + file_put_contents("log.txt", '[mentions.php] ERROR in $mentions'."\n", FILE_APPEND); + continue; + } + foreach($mentions["data"]["children"] as $mention) { + $processed = file_get_contents("processed.txt"); + if ($processed === false) { + file_put_contents("log.txt", "[mentions.php] ERROR: can't read processed.txt\n", FILE_APPEND); + continue; + } + if (in_array($mention["data"]["id"], explode("\n", $processed))) { + continue; + } + if (in_array($mention["data"]["subreddit"], explode("\n", file_get_contents("banned_subs.txt")))) { + continue; + } + if (in_array($mention["data"]["author"], explode("\n", file_get_contents("banned_users.txt")))) { + continue; + } + $post = explode("/", $mention["data"]["context"])[4]; + $post = $o->fetch("post", $post); + var_dump($post); + if ($post < 0) { + file_put_contents("log.txt", '[mentions.php] ERROR in $post'."\n", FILE_APPEND); + continue; + } + $post = $post[0]["data"]["children"][0]; + $parent = "t1_".$mention["data"]["id"]; + if (!file_put_contents("processed.txt", $mention["data"]["id"]."\n", FILE_APPEND)) { + file_put_contents("log.txt", "[mentions.php] ERROR mentions_processed.txt PUT\n", FILE_APPEND); + continue; + } + if (!in_array("media_metadata", array_keys($post["data"]))) { + $o->comment("Error: This post does not appear to be a gallery.$botsignature", $parent); + } else { + $imgs = ""; + foreach ($post["data"]["media_metadata"] as $metadata) { + $imgs .= get_string_between($metadata["s"]["u"], "dd.it/", "?width="); + } + $o->comment("[https://imglinkbot.ž.ga/?i=$imgs](https://imglinkbot.xn--jha.ga/?i=$imgs)$botsignature", $parent); + } + } + sleep(10); + } +?> diff --git a/bot/processed.txt b/bot/processed.txt new file mode 100644 index 0000000..4fde78b --- /dev/null +++ b/bot/processed.txt @@ -0,0 +1 @@ +g95m3jm diff --git a/main.php b/main.php index 0b869e6..31dee80 100644 --- a/main.php +++ b/main.php @@ -59,7 +59,7 @@ Errors: -2 you are not logged in (aj blin trms of srvis bi neseseri) -3 bad username and/or password (diplomatski odgovor ;-) -4 not written yet (suka bljet) - -5 + -5 value from argument not supported -6 error in fetched data 0 bad 1 good @@ -69,8 +69,10 @@ Errors: private $password; public $version = array(0, 0, 1); private $programname = "oldredditclient"; + private $adminemail = "a"."n"."t"."o"."n"."@"."s"."i"."j"."a"."n"."ec.eu"; private $programdomain = 'oldredditclient.sijanec.eu'; private $cookiedir; // set at runtime, ker je get_curerent_user, v login() + private $redditurl = "https://old.reddit.com/"; private $loginurl = "https://old.reddit.com/api/login/"; private $setflairurl = "https://old.reddit.com/api/selectflair"; private $modflairurl = "https://old.reddit.com/api/flair"; @@ -199,5 +201,66 @@ Errors: } return true; } + public function fetch($what = "mentions", $value = "jd2fm5", $limit = 100) { + if(!curl_getinfo($this->ch)) { + if (!$this->login()) { + return -2; + } + } + if ($limit>100) { + echo "\n[oldredditclient] WARN get(): multipage not implemented (limit>100)!\n"; + } + $ch = $this->ch; + switch($what) { + case "mentions": + curl_setopt($ch, CURLOPT_URL, $this->redditurl."message/mentions.json?limit=".$limit); + curl_setopt($ch, CURLOPT_POST, 0); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'X-Modhash: '.$this->modhash, + 'X-ContactBotDeveloper: '.$this->adminemail + )); + $response = curl_exec($ch); + if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { + return false; + } + $arr = json_decode($response, true, 512); + return $arr; + break; + case "post": + curl_setopt($ch, CURLOPT_URL, $this->redditurl.$value.".json"); + curl_setopt($ch, CURLOPT_POST, 0); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'X-Modhash: '.$this->modhash, + 'X-ContactBotDeveloper: '.$this->adminemail + )); + $response = curl_exec($ch); + if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) { + return false; + } + $arr = json_decode($response, true, 512); + return $arr; + default: + return -5; + } + } + public function comment($text = "oldredditClient", $in_reply_to = "jd2fm5") { + if (!curl_getinfo($this->ch)) { + if (!$this->login()) { + return -2; + } + } + $ch = $this->ch; + curl_setopt($ch, CURLOPT_URL, $this->redditurl."api/comment"); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'X-Modhash: '.$this->modhash, + 'X-ContactBotDeveloper: '.$this->adminemail + )); + curl_setopt($ch, CURLOPT_POSTFIELDS, "thing_id=$in_reply_to&text=".urlencode($text)); + $response = curl_exec($ch); + if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) + return false; + return true; + } } ?> diff --git a/mentions.php b/mentions.php new file mode 100644 index 0000000..66d35e1 --- /dev/null +++ b/mentions.php @@ -0,0 +1,21 @@ +setusername($_ENV["orc_user"]); + $o->setpasword($_ENV["orc_pass"]); + while (true) { + $mentions = $o->get("mentions"); + if ($mentions < 0) { + echo "\n".'[mentions.php] ERROR in $mentions'; + continue; + } + foreach($mentions["data"]["children"] as $mention) { + if (in_array(explode("\n", file_get_contents("banned_subs.txt")), $mention["subreddit"])) { + continue; + } + if (in_array(explode("\n", file_get_contents("banned_authors.txt")), $mention["author"])) { + continue; + } + } + } +?> -- cgit v1.2.3