added dummey config and added mongodb posting

This commit is contained in:
Ara0n 2022-02-22 02:21:09 +05:30
parent d6f9051179
commit 6aa2dd969d
6 changed files with 66 additions and 45 deletions

2
.gitignore vendored
View File

@ -2,4 +2,4 @@ botConfig.json
*.session *.session
agentConfig.json agentConfig.json
images images
test.mp4 *.mp4

View File

@ -1,16 +1,13 @@
from cgitb import text
import logging import logging
import json import json
from operator import truediv from datetime import datetime
from time import time from turtle import up
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,getalltsfiles,getAnimelink from botUtils import showhelp,parse_search_query,getalltsfiles
from database import getData, postData from database import getData, postData, updateData
import subprocess import subprocess
import datetime import datetime
import pytz
#enabling Logging #enabling Logging
@ -32,38 +29,41 @@ def help(update,context):
update.message.reply_text(text) update.message.reply_text(text)
def search(update, context): def getanime(update, context):
logger.info('Search function is called!')
link = getAnimelink(update.message.text)
def get(update, context):
logger.info('download function is called!') logger.info('download function is called!')
chat_id = update.message.chat_id chat_id = update.message.chat_id
rawUserInput = update.message.text rawUserInput = update.message.text
userInput = rawUserInput[:] userInput = rawUserInput[10:]
if userInput and not userInput == " ": if userInput and not userInput == " ":
userdata = parse_search_query(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')}") 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: 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) download_status = subprocess.check_call("python downloaderService/main.py "+'"'+userdata.get('series_name')+'"'+' '+ userdata.get('episode_id') , shell=True)
else: 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: else:
update.message.reply_text("Please refer to /help") 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!") update.message.reply_text(f"{userdata.get('series_name')} - {userdata.get('episode_id')} almost done downloading on the server side!")
filepath = getalltsfiles() 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): 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" and "%s"', update, context.error, context )
update.message.reply_text("Something has went wrong!, Please retry :)") update.message.reply_text("Something has went wrong!, Please retry :)")
def check_document(update, context): def check_document(update, context):
''' '''
@ -75,8 +75,23 @@ def check_document(update, context):
if user_id == configdata.get('agent_user_id'): if user_id == configdata.get('agent_user_id'):
file_id = update.message.video.file_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] 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 #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) context.bot.send_video(end_user_chat_id,file_id,supports_streaming=True,timeout=120)
@ -105,8 +120,7 @@ def main():
# added handlers # added handlers
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("getanime", getanime))
dp.add_handler(CommandHandler("get", get))
dp.add_handler(MessageHandler(Filters.text, debug_message)) dp.add_handler(MessageHandler(Filters.text, debug_message))
dp.add_handler(MessageHandler(Filters.video, check_document)) dp.add_handler(MessageHandler(Filters.video, check_document))

View File

@ -9,10 +9,13 @@ example - /search Death Note
''' '''
import os import os
import subprocess
import re
def showhelp(): 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 return helpText
def parse_search_query(raw_input): def parse_search_query(raw_input):
@ -40,21 +43,10 @@ def getalltsfiles():
for file in files: for file in files:
if (file.split(".")[-1].lower() == 'mp4'): if (file.split(".")[-1].lower() == 'mp4'):
mp4FilePath = os.path.join(root, os.path.splitext(file)[0] + ".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 mp4FilePath = mp4FilePath.replace("\\","/") # seriously mate, i'm tired and this might be the worst piece of hack i've done in my life, yet!
# yet!
mp4FilePath = mp4FilePath.replace("\\","/")
return mp4FilePath 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 # python uploaderService/main.py ./Naruto/E04.mp4 5023977571 naruto4

View File

@ -0,0 +1,4 @@
{
"bot_token": "BOT_TOKEN",
"agent_user_id": 123
}

View File

@ -1,7 +1,4 @@
from pymongo import MongoClient from pymongo import MongoClient
import datetime
import json
from botUtils import parse_search_query
client = MongoClient('mongodb://localhost:27017/') client = MongoClient('mongodb://localhost:27017/')
@ -10,7 +7,6 @@ db = client["animeDatabase"]
#collection #collection
col = db["animeDatabase"] col = db["animeDatabase"]
''' '''
* DB Models * DB Models
* animeDatabase * animeDatabase
@ -28,9 +24,17 @@ def postData(data):
post_id = col.insert_one(data).inserted_id post_id = col.insert_one(data).inserted_id
return post_id return post_id
except: except:
return {} return 0
def getData(data): 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 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}})

View File

@ -0,0 +1,7 @@
{
"entity": "entity",
"api_id": "api_id",
"api_hash": "api_hash",
"bot_name": "bot_name"
}