add video file checking on bot

This commit is contained in:
Ara0n 2022-01-09 04:16:12 +05:30
parent a6f7f86991
commit 6da5955991
3 changed files with 113 additions and 19 deletions

View File

@ -2,10 +2,11 @@ import logging
import json import json
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
from telegram.update import Update from telegram.update import Update
from botUtils import showhelp,parse_search_query
#enabling Logging #enabling Logging
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(name)s - %(message)s', level=logging.INFO)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
with open('config/botConfig.json', 'r') as config: with open('config/botConfig.json', 'r') as config:
@ -13,38 +14,77 @@ with open('config/botConfig.json', 'r') as config:
API_TOKEN = configdata.get("bot_token") API_TOKEN = configdata.get("bot_token")
def start(update):
"""Send a message when the command /start is issued.""" def start(update,context):
update.message.reply_text('Hi!') update.message.reply_text(str(update.message.chat_id) + ": " + update.message.text)
def help(update): def help(update,context):
"""Send a message when the command /help is issued.""" text = showhelp()
update.message.reply_text('Help!') update.message.reply_text(text)
def echo(update, context):
"""Echo the user message."""
update.message.reply_text((update.message.text).upper())
def search(update, context): def search(update, context):
logger.info('Search function is called!') logger.info('Search function is called!')
update.message.reply_text('Searching....') update.message.reply_text('Searching....')
def download(update, context): def get(update, context):
logger.info('download function is called!') logger.info('download function is called!')
update.message.reply_text('Downloading!') chat_id = update.message.chat_id
rawUserInput = update.message.text
userInput = rawUserInput[5:]
if userInput and not userInput == " ":
userdata = parse_search_query(userInput)
update.message.reply_text(f"Checking Internal Db\nAnime: {userdata.get('series')}\nSeason: {userdata.get('season_id')}\nEpisode: {userdata.get('episode_id')}")
else:
update.message.reply_text("Please refer to /help")
def getall(update, context):
logger.info('download function is called!')
chat_id = update.message.chat_id
rawUserInput = update.message.text
userInput = rawUserInput[7:]
if userInput and not userInput == " ":
userdata = parse_search_query(userInput)
update.message.reply_text(f"Checking Internal Db\nAnime: {userdata.get('series')}\nSeason: {userdata.get('season_id')}")
else:
update.message.reply_text("Please refer to /help")
def error(update, context): def error(update, context):
"""Log Errors caused by Updates.""" """Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error) logger.warning('Update "%s" caused error "%s"', update, context.error)
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!')
# these are from the file sent from the agent
file_id = update.message.video.file_id
caption = update.message.caption
end_user_chat_id = caption.split(":")[1]
#Keep in mind here i have to parse the chat_id from the caption above
update.message.send_document(end_user_chat_id,file_id)
def debug_message(update, context):
logger.info('debug_message function is called!')
update.message.reply_text('up')
def main(): def main():
updater = Updater(token=API_TOKEN, use_context=True) updater = Updater(token=API_TOKEN, use_context=True)
# Get the dispatcher to register handlers # Get the dispatcher to register handlers
dp = updater.dispatcher dp = updater.dispatcher
@ -52,9 +92,12 @@ def main():
dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("search", search)) dp.add_handler(CommandHandler("search", search))
dp.add_handler(CommandHandler("download", download)) dp.add_handler(CommandHandler("get", get))
dp.add_handler(CommandHandler("getall", getall))
dp.add_handler(MessageHandler(Filters.text, echo))
dp.add_handler(MessageHandler(Filters.text, debug_message))
dp.add_handler(MessageHandler(Filters.video, check_document))
dp.add_error_handler(error) dp.add_error_handler(error)
@ -62,5 +105,6 @@ def main():
updater.idle() updater.idle()
if __name__ == '__main__': if __name__ == '__main__':
main() main()

37
bot/botUtils.py Normal file
View File

@ -0,0 +1,37 @@
'''
Here are the following bot commands
/get - will download the anime episode you wanted
example - /get Death note, s1, ep3
/getadd - will provide all the episode of an anime in a given season
example - /getall Death Note, s1
/search - will provide deatails about an anime
example - /search Death Note
'''
def showhelp():
helpText = "Here are the following bot commands\n\n/get - will download the anime episode you wanted\nexample - /get Death note, s1, ep3\n\n/getadd - will provide all the episode of an anime in a given season\nexample - /getall Death Note, s1\n\n/search - will provide deatails about an anime\nexample - /search Death Note"
return helpText
def parse_search_query(raw_input):
text = raw_input.split(',')
series_name = text[0]
try:
season_id = ''.join([n for n in text[1] if n.isdigit()])
except:
season_id = -1
try:
episode_id = ''.join([n for n in text[2] if n.isdigit()])
except:
episode_id = -1
query_obj = {
"series" : series_name,
"season_id" : season_id,
"episode_id": episode_id
}
return query_obj

13
bot/videoFetcher.py Normal file
View File

@ -0,0 +1,13 @@
def check_if_present_in_DB():
pass
def download_video():
pass
def upload_video():
pass