Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ tzdata==2023.3
urllib3==1.26.5
wrapt==1.15.0
yarl==1.9.2
aiogram==2.11.2
aiogram==2.11.2
schedule==1.2.1
55 changes: 55 additions & 0 deletions scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import time
import schedule
import subprocess

APP_FILE = "app.py"

def stop_bot():
"""
Stopping the bot
"""
# Остановка бота
try:
subprocess.run(["pkill", "-f", "python " + APP_FILE])
print("Bot has been stopped.")
except Exception as e:
print(f"Error stopping the bot: {e}")

def update_index():
"""
Check if new indexes have been added. If they are, stop the bot, add them and restart it
"""
# Открываем файл с url-ами и запоминаем его состояние
with open(r'data/urls_of_channel_videos.txt', 'r') as file:
old_content = file.read()

# Запускаем пайплайн. Если новые видео будут найдены, в файл добавятся их url-ы
subprocess.run(['python', r'data_pipelines/index_pipeline.py'])

# Открываем файл с url-ами после изменения
with open(r'data/urls_of_channel_videos.txt', 'r') as file:
new_content = file.read()

# Если файл с url-ами изменился, останавливаем бота, добавляем новые индексы, перезапускаем бота
if old_content == new_content:
print("No new videos found.")
else:
try:
stop_bot() # Останавливаем бот
subprocess.Popen(["python", APP_FILE]) # Перезапускаем бот
print("Bot has been updated and restarted.")
except Exception as e:
print(f"Error updating and restarting the bot: {e}")

# Планируем обновление индексов
schedule.every().monday.at("00:00").do(update_index)

if __name__ == "__main__":
# Запускаем бот
subprocess.Popen(["python", APP_FILE])

# Запускаем шедулер
while True:
schedule.run_pending()
time.sleep(1)
55 changes: 55 additions & 0 deletions scheduler/scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import os
import time
import schedule
import subprocess

APP_FILE = "app.py"

def stop_bot():
"""
Stopping the bot
"""
# Остановка бота
try:
subprocess.run(["pkill", "-f", "python " + APP_FILE])
print("Bot has been stopped.")
except Exception as e:
print(f"Error stopping the bot: {e}")

def update_index():
"""
Check if new indexes have been added. If they are, stop the bot, add them and restart it
"""
# Открываем файл с url-ами и запоминаем его состояние
with open(r'../data/urls_of_channel_videos.txt', 'r') as file:
old_content = file.read()

# Запускаем пайплайн. Если новые видео будут найдены, в файл добавятся их url-ы
subprocess.run(['python', r'../data_pipelines/index_pipeline.py'])

# Открываем файл с url-ами после изменения
with open(r'../data/urls_of_channel_videos.txt', 'r') as file:
new_content = file.read()

# Если файл с url-ами изменился, останавливаем бота, добавляем новые индексы, перезапускаем бота
if old_content == new_content:
print("No new videos found.")
else:
try:
stop_bot() # Останавливаем бот
subprocess.Popen(["python", APP_FILE]) # Перезапускаем бот
print("Bot has been updated and restarted.")
except Exception as e:
print(f"Error updating and restarting the bot: {e}")

# Планируем обновление индексов
schedule.every().monday.at("00:00").do(update_index)

if __name__ == "__main__":
# Запускаем бот
subprocess.Popen(["python", APP_FILE])

# Запускаем шедулер
while True:
schedule.run_pending()
time.sleep(1)