Major project structure rebase

Signed-off-by: lzzy12 <jhashivam2020@gmail.com>
This commit is contained in:
lzzy12 2019-09-17 19:10:29 +05:30
parent 44fff43553
commit 21dacbc8fb
14 changed files with 100 additions and 92 deletions

7
.gitignore vendored
View File

@ -1 +1,6 @@
*config.env
*.env
*.ini
*auth_token.txt
*.pyc
downloads/*
data*

1
aria.bat Normal file
View File

@ -0,0 +1 @@
aria2c --dir ./download/ --enable-rpc --rpc-listen-all=false --rpc-listen-port 6800 --max-connection-per-server=10 --rpc-max-request-size=1024M --seed-time=0.01 --min-split-size=10M --follow-torrent=mem --split=10 --daemon=true --allow-overwrite=true"

1
aria.sh Normal file
View File

@ -0,0 +1 @@
aria2c --dir ./download/ --enable-rpc --rpc-listen-all=false --rpc-listen-port 6800 --max-connection-per-server=10 --rpc-max-request-size=1024M --seed-time=0.01 --min-split-size=10M --follow-torrent=mem --split=10 --daemon=true --allow-overwrite=true

57
bot.py
View File

@ -1,57 +0,0 @@
from telegram.ext import Updater,CommandHandler,run_async
import ariaTools
import logging
import gdriveTools
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('config.env')
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
if (getConfig('_____REMOVE_THIS_LINE_____')):
logging.ERROR('The README.md file there to be read! Exiting now!')
def getConfig(var: str):
return config.get('BOT', var)
@run_async
def start(update, context):
print(update)
context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
@run_async
def mirror(update,context):
message = update.message.text
link = message.replace('/mirror','')[1:]
reply_msg = context.bot.send_message(chat_id=update.message.chat_id, reply_to_message_id=update.message.message_id, text="Starting Download")
download = ariaTools.add_download(link,[reply_msg,update])
ariaTools.progress_status(context,reply_msg,previous=None)
with open('data','r') as f:
file_name = f.read()
print("File-Name: "+file_name)
link = gdriveTools.upload(file_name)
msg = '<a href="{}">{}</a>'.format(link,file_name)
context.bot.edit_message_text(text=msg,message_id=reply_msg.message_id,chat_id=reply_msg.chat.id,parse_mode='HTMl')
def main():
BOT_TOKEN = getConfig('BOT_TOKEN')
updater = Updater(token=BOT_TOKEN, use_context=True)
start_handler = CommandHandler('start', start)
mirror_handler = CommandHandler('mirror',mirror)
dispatcher = updater.dispatcher
dispatcher.add_handler(start_handler)
dispatcher.add_handler(mirror_handler)
logging.info("Bot Started")
updater.start_polling()
main()

27
bot/__init__.py Normal file
View File

@ -0,0 +1,27 @@
import logging
import configparser
from telegram.ext import Updater
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
config = configparser.ConfigParser()
config.read('bot/config.ini')
def getConfig(name: str):
return config['DEFAULT'][name]
LOGGER = logging.getLogger(__name__)
try:
if bool(config['DEFAULT']['_____REMOVE_THIS_LINE_____']):
logging.ERROR('The README.md file there to be read! Exiting now!')
exit()
except KeyError:
pass
BOT_TOKEN = getConfig('BOT_TOKEN')
updater = Updater(token=BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher

17
bot/__main__.py Normal file
View File

@ -0,0 +1,17 @@
from telegram.ext import CommandHandler, run_async
from bot import dispatcher, LOGGER, updater
import bot.mirror
@run_async
def start(update, context):
print(update)
context.bot.send_message(chat_id=update.message.chat_id, text="I'm a bot, please talk to me!")
LOGGER.info('__main__.py')
def main():
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
LOGGER.info("Bot Started!")
updater.start_polling()
main()

3
bot/config.py Normal file
View File

@ -0,0 +1,3 @@
class Config(object):
"""docstring for Config"""

View File

@ -1,6 +1,9 @@
#Remove this line before deploying
_____REMOVE_THIS_LINE_____=True
[BOT]
[DEFAULT]
# ENTER BOT TOKEN( Get it from https://my.telegram.org)
BOT_TOKEN=""
BOT_TOKEN =
G_DRIVE_CLIENT_ID =
G_DRIVE_CLIENT_SECRET =
GDRIVE_FOLDER_ID =

0
bot/helper/__init__.py Normal file
View File

View File

@ -1,15 +1,10 @@
import aria2p
import os
from time import sleep
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
from bot import LOGGER
cmd = "aria2c --enable-rpc --rpc-listen-all=false --rpc-listen-port 6800 --max-connection-per-server=10 --rpc-max-request-size=1024M --seed-time=0.01 --min-split-size=10M --follow-torrent=mem --split=10 --daemon=true --allow-overwrite=true"
EDIT_SLEEP_TIME_OUT = 5
aria2_is_running = os.system(cmd)
aria2 = aria2p.API(
aria2p.Client(
@ -26,7 +21,7 @@ def check_metadata(gid):
file = aria2.get_download(gid)
if file.followed_by_ids[0] != None:
new_gid = file.followed_by_ids[0]
logging.info("Changing GID "+gid+" to "+new_gid)
LOGGER.info("Changing GID "+gid+" to "+new_gid)
return new_gid
else:
return False
@ -36,12 +31,12 @@ def add_download(link,message):
if "magnet" in link:
download = aria2.add_magnet(link)
allDls[message[0]] = [download,message[1]]
logging.info("Adding: "+link)
LOGGER.info("Adding: "+link)
return download
else:
download = aria2.add_uris([link])
allDls[message[0]] = [download,message[1]]
logging.info("Adding: "+link)
LOGGER.info("Adding: "+link)
return download
@ -77,7 +72,7 @@ def progress_status(context,update,previous):
sleep(5)
progress_status(context,update,previous)
else:
logging.error(file.error_message)
LOGGER.error(file.error_message)
return
else:
try:
@ -90,7 +85,7 @@ def progress_status(context,update,previous):
allDls[update][0] = download
progress_status(context,update,previous=None)
else:
logging.info(file.name+" Completed.")
LOGGER.info(file.name+" Completed.")
msg = "<i>"+str(file.name) +"</i>:- Uploading."
context.bot.edit_message_text(text=msg,message_id=update.message_id,chat_id=update.chat.id,parse_mode='HTMl')
with open('data','w') as f:

View File

@ -7,26 +7,22 @@ from oauth2client import file, client, tools
from mimetypes import guess_type
import httplib2
import os
from config import Config
import logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
from bot import LOGGER, getConfig
G_DRIVE_TOKEN_FILE = "auth_token.txt"
# Copy your credentials from the APIs Console
CLIENT_ID = Config.G_DRIVE_CLIENT_ID
CLIENT_SECRET = Config.G_DRIVE_CLIENT_SECRET
CLIENT_ID = getConfig('G_DRIVE_CLIENT_ID')
CLIENT_SECRET = getConfig('G_DRIVE_CLIENT_SECRET')
# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = "https://www.googleapis.com/auth/drive.file"
# Redirect URI for installed apps, can be left as is
REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob"
parent_id = Config.GDRIVE_FOLDER_ID
parent_id = getConfig('GDRIVE_FOLDER_ID')
G_DRIVE_DIR_MIME_TYPE = "application/vnd.google-apps.folder"
if CLIENT_ID is None or CLIENT_SECRET is None or parent_id is None:
logging.error("Please Setup Config Properly.")
LOGGER.error("Please Setup Config Properly.")
@ -46,10 +42,10 @@ def upload(fileName):
file_name, mime_type = file_ops(fileName)
try:
g_drive_link = upload_file(http, file_name,file_name, mime_type,parent_id)
logging.info("Uploaded To G-Drive: "+fileName)
LOGGER.info("Uploaded To G-Drive: "+fileName)
link = g_drive_link
except Exception as e:
logging.error(str(e))
LOGGER.error(str(e))
pass
else:
http = authorize(G_DRIVE_TOKEN_FILE, None)
@ -57,11 +53,11 @@ def upload(fileName):
try:
dir_id = create_directory(http, os.path.basename(os.path.abspath(fileName)), parent_id)
DoTeskWithDir(http,fileName, dir_id)
logging.info("Uploaded To G-Drive: "+fileName)
LOGGER.info("Uploaded To G-Drive: "+fileName)
dir_link = "https://drive.google.com/folderview?id={}".format(dir_id)
link = dir_link
except Exception as e:
logging.error(str(e))
LOGGER.error(str(e))
pass
return link
# with open('data','w') as f:
@ -88,7 +84,7 @@ def create_directory(http, directory_name, parent_id):
file = drive_service.files().insert(body=file_metadata).execute()
file_id = file.get("id")
drive_service.permissions().insert(fileId=file_id, body=permissions).execute()
logging.info("Created Gdrive Folder:\nName: {}\nID: {} ".format(file.get("title"), file_id))
LOGGER.info("Created Gdrive Folder:\nName: {}\nID: {} ".format(file.get("title"), file_id))
return file_id

23
bot/mirror.py Normal file
View File

@ -0,0 +1,23 @@
from telegram.ext import CommandHandler, run_async
from bot.helper import ariaTools, gdriveTools
import configparser
from bot import config, LOGGER, dispatcher
LOGGER.info('mirror.py')
@run_async
def mirror(update,context):
message = update.message.text
link = message.replace('/mirror','')[1:]
reply_msg = context.bot.send_message(chat_id=update.message.chat_id, reply_to_message_id=update.message.message_id, text="Starting Download")
download = ariaTools.add_download(link,[reply_msg,update])
ariaTools.progress_status(context,reply_msg,previous=None)
with open('data','r') as f:
file_name = f.read()
print("File-Name: "+file_name)
link = gdriveTools.upload(file_name)
msg = '<a href="{}">{}</a>'.format(link,file_name)
context.bot.edit_message_text(text=msg,message_id=reply_msg.message_id,chat_id=reply_msg.chat.id,parse_mode='HTMl')
mirror_handler = CommandHandler('mirror', mirror)
dispatcher.add_handler(mirror_handler)

View File

@ -1,5 +0,0 @@
class Config(object):
"""docstring for Config"""
G_DRIVE_CLIENT_ID = ""
G_DRIVE_CLIENT_SECRET = ""
GDRIVE_FOLDER_ID = ""

View File

@ -1,3 +1,4 @@
python-telegram-bot
google-api-python-client>=1.7.3
google-auth>=1.5.0
google-auth-httplib2<=0.0.3
@ -7,7 +8,5 @@ ndg-httpsclient>=0.4.0
oauth>=1.0.1
pyasn1>=0.4.3
pyasn1-modules>=0.2.1
pycurl>=7.43.0
uritemplate>=3.0.0
urllib3>=1.23