Compare commits

..

21 Commits

Author SHA1 Message Date
gitea cfb26d95a4 Nowy lepszy trunk 2024-04-10 15:25:53 +02:00
gitea 5e14299fef Additional lines of code 2024-04-10 15:25:40 +02:00
gitea 126de9a4a4 Fix 2024-04-10 15:16:22 +02:00
gitea 0d132983fc Fix 2.0 2024-04-10 15:05:43 +02:00
gitea 67cae0e68e Fix 2024-04-10 15:05:43 +02:00
gitea eabd0e8e1e Nope 2024-04-10 15:05:43 +02:00
gitea f7c91e3874 Test chat gpt4 2024-04-10 15:05:43 +02:00
gitea c224b2ccc8 Hejo 2024-04-10 15:05:43 +02:00
gitea 3ca9f2c6f3 -1 2024-04-10 15:04:41 +02:00
gitea c52d55e0a2 +1 2024-04-10 15:04:41 +02:00
gitea e1fa15abb3 Async 2024-04-10 15:04:41 +02:00
gitea 74c76e639d Async fix 2024-04-10 15:04:41 +02:00
gitea 35a65e48e9 And even more async 2024-04-10 15:04:41 +02:00
gitea 5d5c136b41 More async 2024-04-10 15:04:40 +02:00
gitea b9fbc0f910 Async wyszukaj 2024-04-10 15:04:40 +02:00
gitea fd742c8f1c Parametryzacja "muzyki mojego ludu" 2024-04-10 15:04:40 +02:00
gitea 1c240342c5 Logging 2024-04-10 15:04:37 +02:00
gitea 7bd0c0aca4 Fix 2.6 2024-04-10 15:04:37 +02:00
gitea 7c8ad99743 Fix 2 2024-04-10 15:04:37 +02:00
gitea 90896fa9be Fix 1 2024-04-10 15:04:37 +02:00
gitea e3bec6bc11 Playlist based on chat history 2024-04-10 15:04:37 +02:00
3 changed files with 205 additions and 18 deletions
+4
View File
@@ -6,4 +6,8 @@
plugins
user_trunk.yaml
user.yaml
<<<<<<< HEAD
tmp
=======
tools
>>>>>>> c7dc465 (Parametryzacja "muzyki mojego ludu")
+37
View File
@@ -2,6 +2,7 @@
# To learn more about the format of this file, see https://docs.trunk.io/reference/trunk-yaml
version: 0.1
cli:
<<<<<<< HEAD
version: 1.20.1
# Trunk provides extensibility via plugins. (https://docs.trunk.io/plugins)
plugins:
@@ -10,10 +11,44 @@ plugins:
ref: v1.4.5
uri: https://github.com/trunk-io/plugins
# Many linters and tools depend on runtimes - configure them here. (https://docs.trunk.io/runtimes)
=======
version: 1.12.1
plugins:
sources:
- id: trunk
ref: v0.0.22
uri: https://github.com/trunk-io/plugins
lint:
enabled:
- actionlint@1.6.25
- markdownlint@0.35.0
- trivy@0.43.1
- trufflehog@3.44.0
- pylint
- codespell
- cspell
- git-diff-check
- eslint
- prettier
- semgrep
- autopep8
- bandit
- black
- flake8
- isort
- mypy
- pylint
- ruff
- semgrep
- yapf
- gitleaks
>>>>>>> c7dc465 (Parametryzacja "muzyki mojego ludu")
runtimes:
enabled:
- node@18.12.1
- python@3.10.8
<<<<<<< HEAD
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
@@ -31,6 +66,8 @@ lint:
- trivy@0.50.1
- trufflehog@3.71.0
- yamllint@1.35.1
=======
>>>>>>> c7dc465 (Parametryzacja "muzyki mojego ludu")
actions:
disabled:
- trunk-announce
+164 -18
View File
@@ -38,10 +38,10 @@ import spotipy
from discord.ext import commands
from spotipy.oauth2 import SpotifyClientCredentials
import yt_dlp
from spotify_dl import spotify
from spotify_dl import youtube as youtube_download
from spotify_dl import spotify
Music_Config = TypedDict(
"Music_Config",
{
@@ -68,6 +68,11 @@ MEMORY_FIVE_MUZYKA = ""
SETTINGS_FILE = ""
ENCODING = ""
GRAPHICS_PATH = ""
MUZYKA_MOJEGO_LUDU_HISTORIA = 1500
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = 30
MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30
# *=========================================== Platform Specific Predefines
if platform in ("linux", "linux2"):
@@ -187,7 +192,6 @@ async def on_ready():
logger.debug("%s has connected to Discord!", client.user)
await client.change_presence(activity=discord.Game(name="Axe Throwing Darts"))
await client.tree.sync()
# await tree.sync(guild=discord.Object(id=Your guild id))
await check()
@@ -270,22 +274,25 @@ async def on_message(message):
if "conjurer:" in message.content:
async with channel.typing():
logger.debug("Procedura chatu")
message.content = message.content.replace("conjurer: ", "")
prompt = message.content
if message.author.nick:
username = message.author.nick
else:
username = message.author.name
vykidailo = False
bartender = False
if kondziu_mentioned:
prompt = message.clean_content
else:
prompt = message.content
for role in message.author.roles:
if role.name == "Vykidailo":
vykidailo = True
if role.name == "Bartender":
bartender = True
global MESSAGE_TABLE # pylint: disable=global-statement
result, MESSAGE_TABLE = await handle_response(
prompt, vykidailo, bartender, MESSAGE_TABLE, username, False
)
@@ -446,6 +453,20 @@ async def play(ctx, zamawial=None, arg=None):
await ctx.send(result)
async def archive_channel(channel_no):
channel = client.get_channel(channel_no)
messages = [message async for message in channel.history(limit=None)]
path_newfile = (
f"{LOGSTORE}channeldump_{channel_no}_{datetime.now()}.json"
)
new_data = []
for message in messages:
pass
with open(path_newfile, "x", encoding=ENCODING) as new_file:
new_file.seek(0)
json.dump(new_data, new_file, indent=4)
async def check():
"""Funkcja sprawdzająca czy grać następny kawałek."""
while True:
@@ -564,14 +585,14 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
# * extend bo 20 ostatnich
if music:
history.append(message_table_muzyka[0])
history.extend(message_table_muzyka[-20:])
history.extend(message_table_muzyka[-15:])
else:
history.append(MESSAGE_TABLE[0])
history.extend(MESSAGE_TABLE[-20:])
history.extend(MESSAGE_TABLE[-15:])
logger.info("Historia wysłana:")
logger.debug(history)
response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo", messages=history
model="gpt-4", messages=history
)
await asyncio.sleep(10)
result = ""
@@ -756,10 +777,42 @@ async def async_iterator_generator(range_of_iterable):
"""
async def get_stats(ctx, history_limit):
"""
The `get_stats` function retrieves the message history of a specific channel and counts the
frequency of each word in the messages.
:param ctx: The `ctx` parameter is an object that represents the context of the command being
executed. It contains information such as the message, the author, the server, and other relevant
details
:param history_limit: The `history_limit` parameter is the maximum number of messages to retrieve
from the channel history. It determines how far back in time the statistics will be calculated
:return: The function `get_stats` returns a dictionary `stats` that contains the frequency count of
words found in the messages from the specified channel.
"""
channel_id = 1062047367337095268
async with ctx.typing():
stats = {}
channel = client.get_channel(channel_id)
messages = [message async for message in channel.history(limit=history_limit)]
# traverse the iterable of awaitables
for message in messages:
# await and get the result from the awaitable
result = message.content
words = result.split()
for word in words:
if word in stats:
stats[word] += 1
else:
stats[word] = 1
for name, how_many in stats.items():
yield name, how_many
# trunk-ignore(pylint/R0914)
# trunk-ignore(pylint/R0915)
# trunk-ignore(pylint/R0912)
async def wyszukaj(ctx, how_many=0):
async def wyszukaj(ctx, how_many=0, slowa_kluczowe=None):
"""
Take in a context object and an optional integer parameter "how_many" and perform search
operation on music library.
@@ -773,7 +826,10 @@ async def wyszukaj(ctx, how_many=0):
"""
# TODO: Potem dorobić wyszukiwania po kawałkach słów
# i sieć neuronową z możliwością wyrażenia opinii o dopasowaniu.
word_list = ctx.message.content.split()
if slowa_kluczowe:
word_list = slowa_kluczowe
else:
word_list = ctx.message.content.split()
logger.info("Wyszukuje")
search_weight = []
for _ in range(len(music_file_list)):
@@ -784,11 +840,11 @@ async def wyszukaj(ctx, how_many=0):
skip_start = 2
else:
skip_start = 1
for word in word_list[skip_start:]:
async for word in async_iterator_generator(word_list[skip_start:]):
token_weight = len(word)
logger.info("Słowo kluczowe: %s", word)
itr = 0
for file in music_file_list:
async for file in async_iterator_generator(music_file_list):
file = file.split(SEPARATOR_FILE_PATH)
char_remove = [
@@ -814,8 +870,8 @@ async def wyszukaj(ctx, how_many=0):
"mp3",
]
all_words = []
for f_iter in file:
for char in char_remove:
async for f_iter in async_iterator_generator(file):
async for char in async_iterator_generator(char_remove):
f_iter = f_iter.replace(char, "")
tmp = f_iter.split()
all_words.extend(tmp)
@@ -826,7 +882,7 @@ async def wyszukaj(ctx, how_many=0):
else:
skip = 4
matched_times = 1
for itm in all_words[skip:]:
async for itm in async_iterator_generator(all_words[skip:]):
pingu += 1
pattern = ".*" + word + ".*"
if re.match(pattern, itm, re.IGNORECASE):
@@ -1489,7 +1545,13 @@ async def get_image_stable_diffusion(ctx):
pass
# @client.hybrid_command()
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="zagraj_muzyke_mego_ludu",
description="Gra muzyke wyszukana na bazie tematow konwersacji na kanale NZ",
guild=discord.Object(id=664789470779932693),
)
# trunk-ignore(pylint/R0912)
async def zagraj_muzyke_mego_ludu(ctx):
"""
Play completeley random playlist based on messaging history.
@@ -1499,9 +1561,93 @@ async def zagraj_muzyke_mego_ludu(ctx):
channel, the author, and other relevant details. In this case, it is being passed as a parameter to
the `get_image
"""
stats = {}
if ctx:
stat_iter = get_stats(ctx, MUZYKA_MOJEGO_LUDU_HISTORIA)
async for name, how_many in stat_iter:
if len(name) > 3:
stats[name] = how_many
sorted_stats = sorted(stats.items(), key=lambda x: x[1])
key_words = []
for stat in sorted_stats:
key_words.append(stat[0])
logger.info(sorted_stats[:30])
logger.info(key_words[:30])
reply = "Wygenerowana plejlista:\n"
async with ctx.typing():
logger.info("Zaczynam szukać timestamp %s", datetime.now())
search_time_glob = datetime.now()
file = await wyszukaj(ctx=ctx, how_many=MUZYKA_MOJEGO_LUDU_PLAJLISTA, slowa_kluczowe=key_words[:MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE])
logger.info(
"Koniec szukania(timestamp %s zajęło %s",
datetime.now(),
datetime.now() - search_time_glob,
)
index = 1
if file:
for plik in file:
global MUZYKA # pylint: disable=global-variable-not-assigned
MUZYKA["queue"].insert(0, plik[1])
MUZYKA["requestor"].insert(0, ctx.author)
metadata = eyed3.load(plik[1])
if metadata and metadata.tag:
if metadata.tag.title:
reply += f"{index} {metadata.tag.title}"
else:
reply += f"{index}. {plik[1]} z prywatnej kolekcji Hammera."
if metadata.tag.artist:
reply += f" wykonawcy {metadata.tag.artist}"
if metadata.tag.album:
reply += f" z albumu {metadata.tag.album}"
if metadata.tag.title:
reply += " z prywatnej kolekcji Hammera i na Twoją specjalną rpośbę.\n"
else:
reply += f"{index}. {plik[1]} z prywatnej kolekcji Hammera."
index += 1
if len(reply) > 1800:
await ctx.send(reply)
reply = ""
else:
await ctx.send(
"Obawiam się że nie mogę nic znaleźć u siebie. Spróbuj komendy '$dej_co_ze_spotifaja' jeśli masz link"
)
await ctx.send(reply)
logger.info("Plejlista zrobiona")
# trunk-ignore(mypy/arg-type)
@client.hybrid_command(
name="parametry_muzyki_mego_ludu",
description="Konfiguruje komende zagraj muzyke mojego ludu",
guild=discord.Object(id=664789470779932693),
)
async def parametry_muzyki_mego_ludu(ctx, ile_historii=1500, ile_slow_kluczowych=30, jak_dluga_plejlista=30):
"""
The function `parametry_muzyki_mego_ludu` sets global variables for the number of history entries,
number of keywords, and length of playlist for a music application.
:param ctx: The `ctx` parameter is typically used in Discord.py, a Python library for creating
Discord bots. It represents the context of the command being executed, including information about
the message, the server, and the user who invoked the command
:param ile_historii: The parameter "ile_historii" represents the number of history items for the
music playlist, defaults to 1500 (optional)
:param ile_slow_kluczowych: The parameter "ile_slow_kluczowych" represents the number of keywords or
key phrases related to music that you want to include in your analysis or search, defaults to 30
(optional)
:param jak_dluga_plejlista: The parameter "jak_dluga_plejlista" determines the length of the
playlist. It specifies how many songs should be included in the playlist, defaults to 30 (optional)
"""
if ctx:
pass
global MUZYKA_MOJEGO_LUDU_HISTORIA # pylint: disable=global-statement
MUZYKA_MOJEGO_LUDU_HISTORIA = ile_historii
global MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE # pylint: disable=global-statement
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = ile_slow_kluczowych
global MUZYKA_MOJEGO_LUDU_PLAJLISTA # pylint: disable=global-statement
MUZYKA_MOJEGO_LUDU_PLAJLISTA = jak_dluga_plejlista
# *================================== Run