scroll

- irc bot to play ascii art
git clone git://git.acid.vegas/scroll.git
Log | Files | Refs | Archive | README | LICENSE

commit 2d0600dd71040c773d3a9f1852d1dead33638d6f
parent be9cfd8aece404898bf34ada04659067d6e9f623
Author: acidvegas <acid.vegas@acid.vegas>
Date: Tue, 12 Sep 2023 15:03:04 -0400

Updated mirrors (fuck sourcehut), added .ascii random <query> feature, wrongly named variable is .ascii play fixed, etc

Diffstat:
MLICENSE | 2+-
MREADME.md | 40++++++++++++++--------------------------
Mscroll.py | 38++++++++++++++++++--------------------

3 files changed, 33 insertions(+), 47 deletions(-)

diff --git a/LICENSE b/LICENSE
@@ -1,6 +1,6 @@
 ISC License
 
-Copyright (c) 2021, acidvegas <acid.vegas@acid.vegas>
+Copyright (c) 2023, acidvegas <acid.vegas@acid.vegas>
 
 Permission to use, copy, modify, and/or distribute this software for any
 purpose with or without fee is hereby granted, provided that the above
diff --git a/README.md b/README.md
@@ -13,20 +13,19 @@ Designed to be portable, there is no API key needed, no local art files needed, 
 * [opencv-python](https://pypi.org/project/opencv-python/) *(`pip install opencv-python`)*
 
 ## Commands
-| Command                              | Description                                                |
-| ------------------------------------ | ---------------------------------------------------------- |
-| `@scroll`                            | information about scroll                                   |
-| `.ascii <name>`                      | play the \<name> art file                                  |
-| `.ascii dirs`                        | list of art directories                                    |
-| `.ascii img <ur>`                    | convert image \<ur> to art *(experimental, beta status)*   |
-| `.ascii list`                        | list of art filenames                                      |
-| `.ascii play <url>`                  | play the contents of \<url> *(must be a raw pastebin url)* |
-| `.ascii random [dir]`                | play random art, optionally from the [dir] directory only  |
-| `.ascii search <query>`              | search art files that match \<query>                       |
-| `.ascii settings`                    | view settings                                              |
-| `.ascii settings <setting> <option>` | change \<setting> to \<option>                             |
-| `.ascii stop`                        | stop playing art                                           |
-| `.ascii sync`                        | sync the ascii database to pump the newest art             |
+| Command                                | Description                                                |
+| -------------------------------------- | ---------------------------------------------------------- |
+| `@scroll`                              | information about scroll                                   |
+| `.ascii <name>`                        | play the \<name> art file                                  |
+| `.ascii dirs`                          | list of art directories                                    |
+| `.ascii img <ur>`                      | convert image \<ur> to art *(experimental, beta status)*   |
+| `.ascii list`                          | list of art filenames                                      |
+| `.ascii play <url>`                    | play the contents of \<url> *(must be a raw pastebin url)* |
+| `.ascii random [dir|query]`            | play random art, optionally from a [dir] or [query]        |
+| `.ascii search <query>`                | search art files that match \<query>                       |
+| `.ascii settings [<setting> <option>]` | view or change settings                                    |
+| `.ascii stop`                          | stop playing art                                           |
+| `.ascii sync`                          | sync the ascii database to pump the newest art             |
 
 **NOTE**: You can do `.ascii help` to play the [help.txt](https://github.com/ircart/ircart/blob/master/ircart/doc/help.txt) file in your channel.
 
@@ -53,18 +52,7 @@ Designed to be portable, there is no API key needed, no local art files needed, 
 
 Come pump with us in **#scroll** on [irc.supernets.org](ircs://irc.supernets.org)
 
-## Todo
-- git integration to `git clone` the [ircart](https://github.com/ircart/ircart) repository & `git pull` on `.ascii sync` *(Load art files into RAM for faster pumping)*
-- `.ascii scroll` command to loop playing random art files *(Stopped with `.ascii stop`)*
-- Add arguments to `.ascii img` for palette, width, & other options
-- Setting to auto convert any image link to IRC art *(Emulate link previews like on Discord LOL)*
-- `.ascii record` to record lines from the senders nick for uploads *(Stopped with `.ascii stop`)*
-- Improve randomness with `.ascii random`
-- Fall back to non-utf8 encoding on decoding failure *(Reference: 'bytes' object has no attribute 'encode' on `.ascii putfall`)*
-- Convert all art to PNG files for an online gallery & local previews directory
-- Possibly add art metadata *(author, tags, etc)* for credits & improved search
-
 ___
 
 ###### Mirrors
-[acid.vegas](https://git.acid.vegas/scroll) • [GitHub](https://github.com/ircart/scroll) • [GitLab](https://gitlab.com/ircart/scroll) • [SourceHut](https://git.sr.ht/~acidvegas/scroll) • [SuperNETs](https://git.supernets.org/ircart/scroll)
+[acid.vegas](https://git.acid.vegas/scroll) • [GitHub](https://github.com/ircart/scroll) • [GitLab](https://gitlab.com/ircart/scroll) • [SuperNETs](https://git.supernets.org/ircart/scroll)
diff --git a/scroll.py b/scroll.py
@@ -2,7 +2,6 @@
 # Scroll IRC Art Bot - Developed by acidvegas in Python (https://git.acid.vegas/scroll)
 
 import asyncio
-import io
 import json
 import random
 import re
@@ -12,7 +11,7 @@ import time
 import urllib.request
 
 class connection:
-	server  = 'irc.network.com'
+	server  = 'irc.server.com'
 	port    = 6697
 	ipv6    = False
 	ssl     = True
@@ -172,19 +171,14 @@ class Bot():
 				if img:
 					ascii = img2irc.convert(ascii.read(), img, int(self.settings['png_width']), self.settings['png_palette'], int(self.settings['png_quantize']))
 				else:
-					ascii = ascii.readlines()
-				if len(ascii) > int(self.settings['lines']) and chan != '#scroll':
+					ascii = ascii.read().decode(chardet.detect(ascii.read())['encoding'])
+				if len(ascii.splitlines()) > int(self.settings['lines']) and chan != '#scroll':
 					await self.irc_error(chan, 'file is too big', f'take those {len(ascii):,} lines to #scroll')
 				else:
 					if not img and not paste:
 						await self.action(chan, 'the ascii gods have chosen... ' + color(name, cyan))
-					for line in ascii:
-						if type(line) == bytes:
-							try:
-								line = line.decode()
-							except UnicodeError:
-								line = line.decode(chardet.detect(line)['encoding']).encode().decode() # TODO: Do we need to re-encode/decode in UTF-8?
-						line = line.replace('\n','').replace('\r','')
+					for line in ascii.splitlines():
+						line = line.replace('\n','').replace('\r','') # do we need this
 						await self.sendmsg(chan, line + reset)
 						await asyncio.sleep(self.settings['msg'])
 			else:
@@ -276,17 +270,22 @@ class Bot():
 									await self.sendmsg(chan, underline + color('https://raw.githubusercontent.com/ircart/ircart/master/ircart/.list', light_blue))
 								elif args[1] == 'random' and len(args) in (2,3):
 									if len(args) == 3:
-										dir = args[2]
+										query = args[2]
 									else:
-										random.seed(random.randrange(sys.maxsize))
-										dir = random.choice([item for item in self.db if item not in self.settings['ignore']])
-									if dir in self.db:
-										random.seed(random.randrange(sys.maxsize))
-										ascii = f'{dir}/{random.choice(self.db[dir])}'
+										query = random.choice([item for item in self.db if item not in self.settings['ignore']])
+									if query in self.db:
+										ascii = f'{query}/{random.choice(self.db[query])}'
 										self.playing = True
 										self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
 									else:
-										await self.irc_error(chan, 'invalid directory name', dir)
+										results = [{'name':ascii,'dir':dir} for dir in self.db for ascii in self.db[dir] if query in ascii]
+										if results:
+											ascii = random.choice(results)
+											ascii = f'{ascii["dir"]}/{ascii["name"]}'
+											self.playing = True
+											self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
+										else:
+											await self.irc_error(chan, 'invalid directory name or search query', query)
 								elif msg == '.ascii sync' and is_admin(ident):
 									await self.sync()
 									await self.sendmsg(chan, bold + color('database synced', light_green))
@@ -295,7 +294,7 @@ class Bot():
 									if url.startswith('https://pastebin.com/raw/') and len(url.split('raw/')) > 1:
 										self.loops[chan] = asyncio.create_task(self.play(chan, url, paste=True))
 									else:
-										await self.irc_error(chan, 'invalid pastebin url', paste)
+										await self.irc_error(chan, 'invalid pastebin url', url)
 								elif args[1] == 'search' and len(args) == 3:
 									query   = args[2]
 									results = [{'name':ascii,'dir':dir} for dir in self.db for ascii in self.db[dir] if query in ascii]
@@ -367,5 +366,4 @@ try:
 	import img2irc
 except ImportError:
 	raise SystemExit('missing required \'img2irc\' file (https://github.com/ircart/scroll/blob/master/img2irc.py)')
-	pass
 asyncio.run(Bot().connect())