Meido/bot/bot.py

135 lines
5.1 KiB
Python
Raw Normal View History

2022-01-07 03:24:12 +05:30
import logging
import json
from datetime import datetime
from turtle import up
2022-01-07 03:24:12 +05:30
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram.update import Update
from botUtils import showhelp,parse_search_query,getalltsfiles
from database import getData, postData, updateData
import subprocess
2022-01-15 19:56:53 +05:30
import datetime
2022-01-07 03:24:12 +05:30
2022-01-09 04:16:12 +05:30
#enabling Logging
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(name)s - %(message)s', level=logging.INFO)
2022-01-07 03:24:12 +05:30
logger = logging.getLogger(__name__)
with open('bot/config/botConfig.json', 'r') as config:
2022-01-07 03:24:12 +05:30
configdata = json.load(config)
API_TOKEN = configdata.get("bot_token")
2022-01-09 04:16:12 +05:30
def start(update,context):
2022-01-15 19:56:53 +05:30
update.message.reply_text("Thanks for using Araon Bot(v:0.01)\nThis is a alpha built so expect delayed response and many bugs\nIf you spot any issue feel free to reach out")
2022-01-07 03:24:12 +05:30
2022-01-09 04:16:12 +05:30
def help(update,context):
text = showhelp()
update.message.reply_text(text)
2022-01-15 19:56:53 +05:30
def getanime(update, context):
2022-01-09 04:16:12 +05:30
logger.info('download function is called!')
chat_id = update.message.chat_id
rawUserInput = update.message.text
userInput = rawUserInput[10:]
2022-01-09 04:16:12 +05:30
if userInput and not userInput == " ":
userdata = parse_search_query(userInput)
2022-02-20 13:52:33 +05:30
update.message.reply_text(f"Checking Internal Db\nAnime: {userdata.get('series_name')}\nSeason: {userdata.get('season_id')}\nEpisode: {userdata.get('episode_id')}")
logger.info('search_in_mongodb:"%s"', userdata)
search_in_mongodb = userdata.pop('season_id')
anime_name = getData(search_in_mongodb)
2022-02-20 13:52:33 +05:30
if not anime_name:
logger.info('Got data from mongoDB')
logger.info(anime_name)
update_times_queried = updateData(anime_name)
2022-02-20 13:52:33 +05:30
download_status = subprocess.check_call("python downloaderService/main.py "+'"'+userdata.get('series_name')+'"'+' '+ userdata.get('episode_id') , shell=True)
else:
try:
context.bot.send_video(chat_id,anime_name.get("file_id"),supports_streaming=True,timeout=120)
except:
update.message.reply_text("anime_name is empty and anime_name has no file_id")
2022-01-09 04:16:12 +05:30
else:
update.message.reply_text("Please refer to /help")
2022-02-17 21:33:33 +05:30
2022-02-20 13:52:33 +05:30
update.message.reply_text(f"{userdata.get('series_name')} - {userdata.get('episode_id')} almost done downloading on the server side!")
2022-02-17 21:33:33 +05:30
filepath = getalltsfiles()
upload_status = subprocess.check_call("python uploaderService/main.py " +'"'+filepath+'"'+ ' ' + str(chat_id) + ' ' + (userdata.get('series_name')+'-'+str(userdata.get('episode_id'))), shell=True)
2022-01-07 03:24:12 +05:30
2022-01-07 03:24:12 +05:30
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s" and "%s"', update, context.error, context )
2022-02-17 21:33:33 +05:30
update.message.reply_text("Something has went wrong!, Please retry :)")
2022-01-07 03:24:12 +05:30
2022-01-09 04:16:12 +05:30
def check_document(update, context):
'''
This function is important as this checks for all the files uploaded to the telegram server
and returns a file id
'''
logger.info('check_document function is called!')
user_id = update.message.from_user.id
2022-01-09 04:16:12 +05:30
if user_id == configdata.get('agent_user_id'):
file_id = update.message.video.file_id
caption = update.message.caption
object_id = caption.split(":")[1]
end_user_chat_id = caption.split(":")[0]
series_name = object_id.split("-")[0]
episode_id = object_id.split("-")[1]
data2post = {
"series_name":series_name,
"episode_id":episode_id,
"file_id":file_id,
"times_queried":0,
"date_added":datetime.now()
}
logger.info('Got Posting data to mongoDB')
logger.info(data2post)
post_data_to_mongo = postData(data2post)
logger.info(post_data_to_mongo)
#Keep in mind here i have to parse the chat_id from the caption above
2022-01-15 19:56:53 +05:30
context.bot.send_video(end_user_chat_id,file_id,supports_streaming=True,timeout=120)
2022-01-09 04:16:12 +05:30
def debug_message(update, context):
logger.info('debug_message function is called!')
user_id = update.message.from_user.id
2022-02-17 21:33:33 +05:30
#update.message.reply_text("Invalid command")
2022-01-15 19:56:53 +05:30
def callback_minute(context):
context.bot.send_message(chat_id = configdata.get('agent_user_id'), text = 'Heart_beat <3')
2022-01-15 19:56:53 +05:30
# def check_for_update():
# logger.info('Checking Update for Animdl')
# subprocess.check_call("python -m pip install git+https://www.github.com/justfoolingaround/animdl")
2022-01-09 04:16:12 +05:30
def main():
2022-01-15 19:56:53 +05:30
updater = Updater(token=API_TOKEN, use_context=True, request_kwargs={'read_timeout': 10, 'connect_timeout': 10})
job = updater.job_queue
2022-02-20 13:52:33 +05:30
2022-01-07 03:24:12 +05:30
# Get the dispatcher to register handlers
dp = updater.dispatcher
2022-02-17 21:33:33 +05:30
job.run_repeating(callback_minute, interval=120,first=10)
2022-01-15 19:56:53 +05:30
2022-02-17 21:33:33 +05:30
# added handlers
2022-01-07 03:24:12 +05:30
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("getanime", getanime))
2022-01-09 04:16:12 +05:30
dp.add_handler(MessageHandler(Filters.text, debug_message))
dp.add_handler(MessageHandler(Filters.video, check_document))
2022-01-07 03:24:12 +05:30
dp.add_error_handler(error)
2022-01-15 19:56:53 +05:30
updater.start_polling(timeout=120)
2022-01-07 03:24:12 +05:30
updater.idle()
2022-01-09 04:16:12 +05:30
2022-01-07 03:24:12 +05:30
if __name__ == '__main__':
main()