Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concurrency using asyncio #12

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions spotifilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import spotipy
import requests
import asyncio
from typing import Final
from openai import OpenAI
from dotenv import load_dotenv
Expand All @@ -20,7 +21,7 @@
OPENAI_API_KEY: Final = os.getenv("OPENAI_API_KEY")
BOT_TOKEN: Final = os.getenv("TELEGRAM_TOKEN")
BOT_USERNAME: Final = os.getenv("TELEGRAM_USERNAME")
BOT_POLLING_INTERVAL: Final = int(os.getenv("POLLING_INTERVAL", 5))
BOT_POLLING_INTERVAL: Final = float(os.getenv("POLLING_INTERVAL", 0.0))


def get_playlist_info(playlist_id: str) -> str:
Expand Down Expand Up @@ -164,12 +165,12 @@ async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> N


async def filter_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
playlist_id = update.message.text.lstrip("/filter").strip()
if playlist_id == "":
if not context.args:
await update.message.reply_text("You need to provide a playlist ID after the /filter command.")
else:
await update.message.reply_text(get_playlist_info(playlist_id)[1])
report = logic(playlist_id)
await update.message.reply_text(get_playlist_info(context.args[0])[1])
loop = asyncio.get_event_loop()
report = await loop.run_in_executor(None, logic, context.args[0])
await update.message.reply_text(report)


Expand All @@ -178,18 +179,26 @@ async def report_command(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
"https://github.com/doronkg/spotifilter/issues")


async def handle_unknown(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("I don't know what it is.")


async def handle_error(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
print(f"An error occurred in {update}: {context.error}")


def main():
app = Application.builder().token(BOT_TOKEN).build()
app = Application.builder().token(BOT_TOKEN).concurrent_updates(True).build()

# Commands
app.add_handler(CommandHandler("start", start_command))
app.add_handler(CommandHandler("filter", filter_command))
app.add_handler(CommandHandler("report", report_command))

# Messages
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), handle_unknown))
app.add_handler(MessageHandler(filters.COMMAND, handle_unknown))

# Errors
app.add_error_handler(handle_error)

Expand Down