2022-03-17 18:39:30 +05:30
#!/usr/bin/env python3
2022-01-07 03:24:12 +05:30
import logging
import json
2022-02-22 02:21:09 +05:30
from datetime import datetime
2022-01-07 03:24:12 +05:30
from telegram . ext import Updater , CommandHandler , MessageHandler , Filters
from telegram . update import Update
2022-02-22 02:21:09 +05:30
from botUtils import showhelp , parse_search_query , getalltsfiles
from database import getData , postData , updateData
2022-01-15 01:29:05 +05:30
import subprocess
2022-01-15 19:56:53 +05:30
import datetime
2022-01-07 03:24:12 +05:30
2022-03-09 22:01:36 +05:30
BOT_VERSION = 0.1
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__ )
2022-03-17 18:39:30 +05:30
#fetching config file
2022-03-09 22:01:36 +05:30
try :
with open ( ' bot/config/botConfig.json ' , ' w+ ' ) as config :
configdata = json . load ( config )
2022-03-17 18:39:30 +05:30
except Exception as e :
2022-03-09 22:01:36 +05:30
raise Exception ( ' CONFIG FILE NOT FOUND! ' )
2022-01-07 03:24:12 +05:30
API_TOKEN = configdata . get ( " bot_token " )
2022-01-09 04:16:12 +05:30
def start ( update , context ) :
2022-03-09 22:01:36 +05:30
update . message . reply_text ( f " Thanks for using Araon Bot( { BOT_VERSION } ) \n This is a alpha built so expect delayed response and many bugs \n If 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
2022-01-15 01:29:05 +05:30
2022-02-22 02:21:09 +05:30
def getanime ( update , context ) :
2022-03-09 22:01:36 +05:30
'''
Fetches the anime file on the mongo db and then if not found ,
downloads the anime using animeld
'''
2022-01-09 04:16:12 +05:30
logger . info ( ' download function is called! ' )
2022-03-09 22:01:36 +05:30
2022-01-09 04:16:12 +05:30
chat_id = update . message . chat_id
rawUserInput = update . message . text
2022-02-22 02:21:09 +05:30
userInput = rawUserInput [ 10 : ]
2022-01-09 04:16:12 +05:30
if userInput and not userInput == " " :
userdata = parse_search_query ( userInput )
2022-03-09 22:01:36 +05:30
2022-02-20 13:52:33 +05:30
update . message . reply_text ( f " Checking Internal Db \n Anime: { userdata . get ( ' series_name ' ) } \n Season: { userdata . get ( ' season_id ' ) } \n Episode: { userdata . get ( ' episode_id ' ) } " )
2022-02-22 02:21:09 +05:30
logger . info ( ' search_in_mongodb: " %s " ' , userdata )
2022-03-09 22:01:36 +05:30
2022-02-22 02:21:09 +05:30
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 :
2022-02-22 02:21:09 +05:30
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 :
2022-02-22 02:21:09 +05:30
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-03-09 22:01:36 +05:30
update . message . reply_text ( f " { userdata . get ( ' series_name ' ) } - { userdata . get ( ' episode_id ' ) } is done downloading on the server side! " )
2022-02-17 21:33:33 +05:30
filepath = getalltsfiles ( )
2022-02-22 02:21:09 +05:30
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-15 01:29:05 +05:30
2022-01-07 03:24:12 +05:30
def error ( update , context ) :
""" Log Errors caused by Updates. """
2022-02-22 02:21:09 +05:30
logger . warning ( ' Update " %s " caused error " %s " and " %s " ' , update , context . error , context )
2022-03-09 22:01:36 +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! ' )
2022-01-11 00:12:03 +05:30
user_id = update . message . from_user . id
2022-01-09 04:16:12 +05:30
2022-01-11 00:12:03 +05:30
if user_id == configdata . get ( ' agent_user_id ' ) :
file_id = update . message . video . file_id
2022-02-22 02:21:09 +05:30
caption = update . message . caption
object_id = caption . split ( " : " ) [ 1 ]
2022-01-11 00:12:03 +05:30
end_user_chat_id = caption . split ( " : " ) [ 0 ]
2022-02-22 02:21:09 +05:30
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 )
2022-03-09 22:01:36 +05:30
2022-01-11 00:12:03 +05:30
#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-11 00:12:03 +05:30
2022-01-09 04:16:12 +05:30
def debug_message ( update , context ) :
logger . info ( ' debug_message function is called! ' )
2022-01-11 00:12:03 +05:30
user_id = update . message . from_user . id
2022-02-17 21:33:33 +05:30
#update.message.reply_text("Invalid command")
2022-01-11 00:12:03 +05:30
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-11 00:12:03 +05:30
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-11 00:12:03 +05:30
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 ) )
2022-02-22 02:21:09 +05:30
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 ( )