From 6aa2dd969d89ba56dbe708f0bc369ad87dea53f6 Mon Sep 17 00:00:00 2001 From: Ara0n Date: Tue, 22 Feb 2022 02:21:09 +0530 Subject: [PATCH] added dummey config and added mongodb posting --- .gitignore | 2 +- bot/bot.py | 60 ++++++++++++------- bot/botUtils.py | 22 +++---- bot/config/exampleConfig.json | 4 ++ bot/database.py | 16 +++-- .../config/Example_agentConfig.json | 7 +++ 6 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 bot/config/exampleConfig.json create mode 100644 uploaderService/config/Example_agentConfig.json diff --git a/.gitignore b/.gitignore index 2f32dd0..bc9bba3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,4 @@ botConfig.json *.session agentConfig.json images -test.mp4 +*.mp4 diff --git a/bot/bot.py b/bot/bot.py index 4c7b811..18e2afb 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,16 +1,13 @@ -from cgitb import text import logging import json -from operator import truediv -from time import time +from datetime import datetime +from turtle import up from telegram.ext import Updater, CommandHandler, MessageHandler, Filters from telegram.update import Update -from botUtils import showhelp,parse_search_query,getalltsfiles,getAnimelink -from database import getData, postData +from botUtils import showhelp,parse_search_query,getalltsfiles +from database import getData, postData, updateData import subprocess import datetime -import pytz - #enabling Logging @@ -32,38 +29,41 @@ def help(update,context): update.message.reply_text(text) -def search(update, context): - logger.info('Search function is called!') - link = getAnimelink(update.message.text) - - - -def get(update, context): +def getanime(update, context): logger.info('download function is called!') chat_id = update.message.chat_id rawUserInput = update.message.text - userInput = rawUserInput[:] + userInput = rawUserInput[10:] + if userInput and not userInput == " ": userdata = parse_search_query(userInput) update.message.reply_text(f"Checking Internal Db\nAnime: {userdata.get('series_name')}\nSeason: {userdata.get('season_id')}\nEpisode: {userdata.get('episode_id')}") - anime_name = getData(userdata) + logger.info('search_in_mongodb:"%s"', userdata) + search_in_mongodb = userdata.pop('season_id') + anime_name = getData(search_in_mongodb) if not anime_name: + logger.info('Got data from mongoDB') + logger.info(anime_name) + update_times_queried = updateData(anime_name) download_status = subprocess.check_call("python downloaderService/main.py "+'"'+userdata.get('series_name')+'"'+' '+ userdata.get('episode_id') , shell=True) else: - context.bot.send_video(chat_id,anime_name.get("file_id"),supports_streaming=True,timeout=120) + 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") + else: update.message.reply_text("Please refer to /help") update.message.reply_text(f"{userdata.get('series_name')} - {userdata.get('episode_id')} almost done downloading on the server side!") 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) + upload_status = subprocess.check_call("python uploaderService/main.py " +'"'+filepath+'"'+ ' ' + str(chat_id) + ' ' + (userdata.get('series_name')+'-'+str(userdata.get('episode_id'))), shell=True) def error(update, context): """Log Errors caused by Updates.""" - logger.warning('Update "%s" caused error "%s"', update, context.error) + logger.warning('Update "%s" caused error "%s" and "%s"', update, context.error, context ) update.message.reply_text("Something has went wrong!, Please retry :)") - def check_document(update, context): ''' @@ -75,8 +75,23 @@ def check_document(update, context): if user_id == configdata.get('agent_user_id'): file_id = update.message.video.file_id - caption = update.message.caption + 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 context.bot.send_video(end_user_chat_id,file_id,supports_streaming=True,timeout=120) @@ -105,8 +120,7 @@ def main(): # added handlers dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) - dp.add_handler(CommandHandler("search", search)) - dp.add_handler(CommandHandler("get", get)) + dp.add_handler(CommandHandler("getanime", getanime)) dp.add_handler(MessageHandler(Filters.text, debug_message)) dp.add_handler(MessageHandler(Filters.video, check_document)) diff --git a/bot/botUtils.py b/bot/botUtils.py index 6f7ce4b..ecf47da 100644 --- a/bot/botUtils.py +++ b/bot/botUtils.py @@ -9,10 +9,13 @@ example - /search Death Note ''' import os -import subprocess -import re + + def showhelp(): - helpText = "Here are the following bot commands\n\n/get - will download the anime episode you wanted(make sure you seperate the name and the season and ep with comma)\nexample - /get Death note, 1, 3\n\n/search(still in development) - will provide deatails about an anime\nexample - /search Death Note" + helpText = "Here are the following bot commands\n \ + \n/getanime - will download the anime episode you wanted(make sure you seperate the name and the season and ep with comma) \ + \nexample - /getanime Death note, 1, 3\n\n/search(still in development) \ + - will provide deatails about an anime\nexample - /search Death Note" return helpText def parse_search_query(raw_input): @@ -40,21 +43,10 @@ def getalltsfiles(): for file in files: if (file.split(".")[-1].lower() == 'mp4'): mp4FilePath = os.path.join(root, os.path.splitext(file)[0] + ".mp4") - # seriously mate, i'm tired and this might be the worst piece of hack i've done in my life - # yet! - mp4FilePath = mp4FilePath.replace("\\","/") + mp4FilePath = mp4FilePath.replace("\\","/") # seriously mate, i'm tired and this might be the worst piece of hack i've done in my life, yet! return mp4FilePath -def getAnimelink(search_query): - userInput = search_query[8:] - if userInput and not userInput == " ": - userdata = parse_search_query(userInput) - - import pdb - pdb.set_trace() - #return raw_search_data - # python uploaderService/main.py ./Naruto/E04.mp4 5023977571 naruto4 diff --git a/bot/config/exampleConfig.json b/bot/config/exampleConfig.json new file mode 100644 index 0000000..068ae0f --- /dev/null +++ b/bot/config/exampleConfig.json @@ -0,0 +1,4 @@ +{ + "bot_token": "BOT_TOKEN", + "agent_user_id": 123 +} \ No newline at end of file diff --git a/bot/database.py b/bot/database.py index b7f5c6b..9ffd666 100644 --- a/bot/database.py +++ b/bot/database.py @@ -1,7 +1,4 @@ from pymongo import MongoClient -import datetime -import json -from botUtils import parse_search_query client = MongoClient('mongodb://localhost:27017/') @@ -10,7 +7,6 @@ db = client["animeDatabase"] #collection col = db["animeDatabase"] - ''' * DB Models * animeDatabase @@ -28,9 +24,17 @@ def postData(data): post_id = col.insert_one(data).inserted_id return post_id except: - return {} + return 0 def getData(data): - anime_data = col.find_one(filter=data) + anime_data = col.find_one(filter=data) #{"serise_name":"anime_name", "episode_id":2 } return anime_data +def updateData(data): + try: + update_id = col.update_one({"series_name":data.get('series_name') , "episode_id": data.get('episode_id')},{'$inc': {'times_queried':1}}) + return update_id + except: + return 0 + +#result = db.test.update_one({'x': 1}, {'$inc': {'x': 3}}) \ No newline at end of file diff --git a/uploaderService/config/Example_agentConfig.json b/uploaderService/config/Example_agentConfig.json new file mode 100644 index 0000000..a5bd357 --- /dev/null +++ b/uploaderService/config/Example_agentConfig.json @@ -0,0 +1,7 @@ +{ + "entity": "entity", + "api_id": "api_id", + "api_hash": "api_hash", + "bot_name": "bot_name" +} +