Finished audio list generator

This commit is contained in:
2022-09-01 15:15:30 +03:00
parent 5b134e6446
commit 3c0851102d
7 changed files with 139 additions and 67 deletions

View File

@@ -1,6 +1,78 @@
import sqlite3
class User:
def __init__(self, userid: str = None):
self.userid = userid
self.list: list = self._lister(self.userid)
self._current_index = 0
def __str__(self) -> str:
return self.name
def __repr__(self) -> str:
return (
f'<Audio file name={self.name} path={self.userid} type={self.type}>'
)
@property
def name(self) -> str:
return self.list[self._current_index]
@property
def type(self) -> str:
guess = mimetypes.guess_type(f'{self.path}/{self.name}')[0].split('/')[0]
return guess
@classmethod
def _lister(cls, path) -> list:
_dict: list = []
for _files in os.walk(path):
_dict.extend(_files)
break
return _dict[2]
def __iter__(self):
return _ListGenerationIter(self)
class _FileAttrs:
def __init__(self, name, path, type):
self.name = name
self.path = path
self.type = type
def __str__(self):
return 'Audio Dict Generator'
def __repr__(self):
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
class _ListGenerationIter:
def __init__(self, list_class):
self._list = list_class.list
self._name = list_class.name
self._type = list_class.type
self._path = list_class.path
self._size = len(self._list)
self._current_index = 0
def __iter__(self):
return self
def __next__(self):
if self._current_index < self._size:
_name = self._list[self._current_index]
_path = self._path
_type = self._type
self._current_index += 1
memb = _FileAttrs(_name, _path, _type)
return memb
raise StopIteration
async def prepare_db(guild: int):
try:
connect = sqlite3.connect('user.db')

View File

@@ -2,76 +2,66 @@ import mimetypes
import os
class FileAttrs:
def __iter__(self, name, path, type):
self.name = name
self.path = path
self.type = type
@self.name.setter
def name(self, value):
self.name = value
@self.type.setter
def type(self, value):
self.type = value
@self.path.setter
def path(self, value):
self.path = value
class ListGenerator:
def __init__(self, path=None):
self._path = path
self.list: list = self._lister(self._path)
def __init__(self, path: str = None):
self.path = path
self.list: list = self._lister(self.path)
self._current_index = 0
def __str__(self) -> str:
return self.name
if self.path:
return self.name
else:
return 'Audio iter generator'
def __repr__(self) -> str:
return (
f'<Audio file name={self.name} path={self.path} type={self.type} size={self._size}>'
)
if self.path:
return (
f'<Audio file name={self.name} path={self.path} type={self.type}>'
)
else:
return (
f'<Audio file name={None} path={None} type={None}>'
)
@property
def name(self) -> str:
return self.list[self._current_index]
@property
def path(self) -> str:
return str(self._path)
@property
def type(self) -> str:
guess = mimetypes.guess_type(f'{self._path}/{self.name}')[0].split('/')[0]
guess = mimetypes.guess_type(f'{self.path}/{self.name}')[0].split('/')[0]
return guess
@classmethod
def _lister(cls, path) -> list:
_dict: list = []
for _files in os.walk(path):
_dict.extend(_files)
break
return _dict[2]
try:
for _files in os.walk(path):
_dict.extend(_files)
break
return _dict[2]
except TypeError:
pass
def __iter__(self):
return ListGenerationIter(self)
@name.setter
def name(self, value):
self.name = value
@type.setter
def type(self, value):
self.type = value
@path.setter
def path(self, value):
self.path = value
return _ListGenerationIter(self)
class ListGenerationIter:
class _FileAttrs:
def __init__(self, name, path, type):
self.name = name
self.path = path
self.type = type
def __str__(self):
return self.name
def __repr__(self):
return f'<File attrs name={self.name} path={self.path} type={self.path}>'
class _ListGenerationIter:
def __init__(self, list_class):
self._list = list_class.list
self._name = list_class.name
@@ -86,11 +76,10 @@ class ListGenerationIter:
def __next__(self):
if self._current_index < self._size:
_name = self._list[self._current_index]
_path = self._path
_type = self._type
self._current_index += 1
memb = FileAttrs(_name, _path, _type)
memb = _FileAttrs(_name, _path, _type)
return memb
raise StopIteration

View File

@@ -1,7 +1,7 @@
import logging
class CustomFormatter(logging.Formatter):
class _CustomFormatter(logging.Formatter):
grey = "\x1b[38;20m"
yellow = "\x1b[33;20m"
@@ -29,5 +29,5 @@ logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(CustomFormatter())
ch.setFormatter(_CustomFormatter())
logger.addHandler(ch)

View File

@@ -10,7 +10,7 @@ async def play_audio(audio, bot, vc):
await sleep(1)
vp = await vc.connect()
if not vp.is_playing():
vp.play(FFmpegPCMAudio(f'audio/{audio}'), after=None)
vp.play(FFmpegPCMAudio(f'{audio}'), after=None)
while vp.is_playing():
await sleep(0.5)
await sleep(1)