Fun function to find movies
authorCameron Ball <cameron@cameron1729.xyz>
Mon, 19 Nov 2018 13:33:01 +0000 (21:33 +0800)
committerCameron Ball <cameron@cameron1729.xyz>
Tue, 20 Nov 2018 03:05:37 +0000 (11:05 +0800)
purjolok.php

index 3e78f8d..1678cb2 100644 (file)
@@ -14,6 +14,31 @@ use function Common\{
 use Telegram\Bot\Actions;
 use Telegram\Bot\Commands\Command;
 
+
+getTelegram()->addCommand(
+    new class extends Command {
+        protected $name = 'searchmovies';
+        protected $description = 'Search through the movie collection';
+
+        public function handle($arguments) {
+            $results = array_map(
+                function($moviename) use ($arguments) {
+                    similar_text($arguments, substr($moviename, 0, -7) ?: '', $perc);
+                    return ['title' => $moviename, 'similarity' => $perc];
+            },
+                scandir('/mnt/media/Movies')
+            );
+
+            uasort($results, function($a, $b) {
+                return $b['similarity'] <=> $a['similarity'];
+            });
+
+            $results = array_column(array_slice(array_values($results), 0, 5), 'title');
+            $this->replyWithMessage(['text' => "Here are the most similar movies titles I could find...\n\n" . implode("\n", $results)]);
+        }
+    }
+);
+
 getTelegram()->addCommand(
     new class extends Command {
         protected $name = 'chatid';