1
mirror of git://git.acid.vegas/scroll.git synced 2024-11-22 16:16:40 +00:00

Improved database systen using github api, no more .list file needed

This commit is contained in:
Dionysus 2023-06-05 06:39:40 -04:00
parent 4725e6710b
commit cd4e9c8027
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 43 additions and 51 deletions

View File

@ -9,16 +9,16 @@ All of the IRC art is loaded directly from the [@ircart/ircart](https://github.c
* [chardet](https://pypi.org/project/chardet/) *(`pip install chardet`)* * [chardet](https://pypi.org/project/chardet/) *(`pip install chardet`)*
## Commands ## Commands
| Command | Description | | Command | Description |
| --------------------------------- | ---------------------------------------------------------- | | ------------------------ | --------------------------------------------------------- |
| `@scroll` | information about scroll | | `@scroll` | information about scroll |
| `.ascii <name>` | play the \<name> art file | | `.ascii <name>` | play the \<name> art file |
| `.ascii dirs` | list of ascii directories | | `.ascii dirs` | list of art directories |
| `.ascii list` | list of ascii filenames | | `.ascii list` | list of art filenames |
| `.ascii random [dir]` | play random art, optionally from the [dir] directory only | | `.ascii random [dir]` | play random art, optionally from the [dir] directory only |
| `.ascii search <query>` | search for art diles that match \<query> | | `.ascii search <query>` | search for art diles that match \<query> |
| `.ascii stop` | stop playing art | | `.ascii stop` | stop playing art |
| `.ascii sync` | sync the ascii database to pump the newest art | | `.ascii sync` | sync the ascii database to pump the newest art |
## Preview ## Preview

View File

@ -2,6 +2,7 @@
# Scroll IRC Art Bot - Developed by acidvegas in Python (https://git.acid.vegas/scroll) # Scroll IRC Art Bot - Developed by acidvegas in Python (https://git.acid.vegas/scroll)
import asyncio import asyncio
import json
import random import random
import ssl import ssl
import time import time
@ -18,16 +19,16 @@ class connection:
modes = None modes = None
class identity: class identity:
nickname = 'scroll' nickname = 'scr0ll'
username = 'scroll' username = 'scroll'
realname = 'git.acid.vegas/scroll' realname = 'git.acid.vegas/scroll'
nickserv = None nickserv = None
class throttle: class throttle:
flood = 3 # delay between each command flood = 2 # delay between each command
max_lines = 300 # maximum number of lines in art file to be played outside of #scroll max_lines = 300 # maximum number of lines in art file to be played outside of #scroll
message = 0.05 # delay between each line sent message = 0.03 # delay between each line sent
results = 10 # maximum number of results returned from search results = 25 # maximum number of results returned from search
# Formatting Control Characters / Color Codes # Formatting Control Characters / Color Codes
bold = '\x02' bold = '\x02'
@ -69,7 +70,7 @@ def ssl_ctx():
class Bot(): class Bot():
def __init__(self): def __init__(self):
self.db = dict() self.db = None
self.last = 0 self.last = 0
self.loops = dict() self.loops = dict()
self.playing = False self.playing = False
@ -117,26 +118,29 @@ class Bot():
async def sync(self): async def sync(self):
try: try:
cache = self.db cache = self.db
self.db = dict() self.db = {'root':list()}
ascii = urllib.request.urlopen('https://raw.githubusercontent.com/ircart/ircart/master/ircart/.list').readlines() sha = json.loads(urllib.request.urlopen('https://api.github.com/repos/ircart/ircart/contents').read().decode('utf-8'))[1]['sha']
for item in ascii: files = json.loads(urllib.request.urlopen(f'https://api.github.com/repos/ircart/ircart/git/trees/{sha}?recursive=true').read().decode('utf-8'))['tree']
item = item.decode(chardet.detect(item)['encoding']).replace('\n','').replace('\r','') for file in files:
if '/' in item: if file['type'] != 'tree':
dir = item.split('/')[0] file['path'] = file['path'][:-4]
name = item.split('/')[1] if '/' in file['path']:
self.db[dir] = self.db[dir]+[name,] if dir in self.db else [name,] dir = file['path'].split('/')[0]
else: name = file['path'].split('/')[1]
self.db['root'] = self.db['root']+[item,] if 'root' in self.db else [item,] self.db[dir] = self.db[dir]+[name,] if dir in self.db else [name,]
await self.sendmsg(connection.channel, bold + color('database synced', light_green)) else:
self.db['root'].append(file['path'])
except Exception as ex: except Exception as ex:
try: try:
await self.irc_error(connection.channel, 'failed to sync database', ex) await self.irc_error(connection.channel, 'failed to sync database', ex)
except: except:
error(connection.channel, 'failed to sync database', ex) error(connection.channel, 'failed to sync database', ex)
self.db = cache finally:
self.db = cache
async def play(self, chan, name): async def play(self, chan, name):
try: try:
print(name)
ascii = urllib.request.urlopen(f'https://raw.githubusercontent.com/ircart/ircart/master/ircart/{name}.txt', timeout=10) ascii = urllib.request.urlopen(f'https://raw.githubusercontent.com/ircart/ircart/master/ircart/{name}.txt', timeout=10)
if ascii.getcode() == 200: if ascii.getcode() == 200:
ascii = ascii.readlines() ascii = ascii.readlines()
@ -220,23 +224,22 @@ class Bot():
elif msg == '.ascii random': elif msg == '.ascii random':
self.playing = True self.playing = True
dir = random.choice(list(self.db)) dir = random.choice(list(self.db))
ascii = random.choice(self.db[dir]) if dir == 'root' else dir+'/'+random.choice(self.db[dir]) ascii = f'{dir}/{random.choice(self.db[dir])}'
self.loops[chan] = asyncio.create_task(self.play(chan, ascii)) self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
elif msg == '.ascii sync':
await self.sync()
await self.sendmsg(connection.channel, bold + color('database synced', light_green))
elif args[1] == 'random' and len(args) == 3: elif args[1] == 'random' and len(args) == 3:
dir = args[2] dir = args[2]
if dir in self.db: if dir in self.db:
self.playing = True self.playing = True
ascii = random.choice(self.db[dir]) ascii = f'{dir}/{random.choice(self.db[dir])}'
self.loops[chan] = asyncio.create_task(self.play(chan, dir+'/'+ascii)) self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
else: else:
await self.irc_error(chan, 'invalid directory name', dir) await self.irc_error(chan, 'invalid directory name', dir)
elif args[1] == 'search' and len(args) == 3: elif args[1] == 'search' and len(args) == 3:
query = args[2] query = args[2]
results = list() results = [{'name':ascii,'dir':dir} for dir in self.db for ascii in self.db[dir] if query in ascii]
for dir in self.db:
for ascii in self.db[dir]:
if query in ascii:
results.append({'dir':dir,'name':ascii})
if results: if results:
for item in results[:throttle.results]: for item in results[:throttle.results]:
if item['dir'] == 'root': if item['dir'] == 'root':
@ -246,25 +249,14 @@ class Bot():
await asyncio.sleep(throttle.message) await asyncio.sleep(throttle.message)
else: else:
await self.irc_error(chan, 'no results found', query) await self.irc_error(chan, 'no results found', query)
elif msg == '.ascii sync':
await self.sync()
elif len(args) == 2: elif len(args) == 2:
option = args[1] query = args[1]
if [x for x in ('..','?','%','\\') if x in option]: results = [dir+'/'+ascii for dir in self.db for ascii in self.db[dir] if query == ascii]
await self.irc_error(chan, 'nice try nerd') if results:
elif option == 'random':
self.playing = True self.playing = True
self.loops[chan] = asyncio.create_task(self.play(chan, random.choice(self.db))) self.loops[chan] = asyncio.create_task(self.play(chan, results[0]))
else: else:
ascii = [dir+'/'+option for dir in self.db if option in self.db[dir]] await self.irc_error(chan, 'no results found', query)
if ascii:
ascii = ascii[0]
if ascii.startswith('root/'):
ascii = ascii.split('/')[1]
self.playing = True
self.loops[chan] = asyncio.create_task(self.play(chan, ascii))
else:
await self.irc_error(chan, 'no results found', option)
except (UnicodeDecodeError, UnicodeEncodeError): except (UnicodeDecodeError, UnicodeEncodeError):
pass pass
except Exception as ex: except Exception as ex: