Initial
This commit is contained in:
commit
e826385571
|
|
@ -0,0 +1,97 @@
|
|||
import aria2p
|
||||
import os
|
||||
from time import sleep
|
||||
import logging
|
||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
level=logging.INFO)
|
||||
|
||||
|
||||
|
||||
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(
|
||||
host="http://localhost",
|
||||
port=6800,
|
||||
secret=""
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
allDls = {}
|
||||
|
||||
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)
|
||||
return new_gid
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def add_download(link,message):
|
||||
if "magnet" in link:
|
||||
download = aria2.add_magnet(link)
|
||||
allDls[message[0]] = [download,message[1]]
|
||||
logging.info("Adding: "+link)
|
||||
return download
|
||||
else:
|
||||
download = aria2.add_uris([link])
|
||||
allDls[message[0]] = [download,message[1]]
|
||||
logging.info("Adding: "+link)
|
||||
return download
|
||||
|
||||
|
||||
def remove_download(message):
|
||||
del allDls[message]
|
||||
aria2.remove_download(allDls[message].gid)
|
||||
return True
|
||||
|
||||
|
||||
|
||||
def get_download_by_message(message):
|
||||
if allDls[message] == None:
|
||||
return None
|
||||
else:
|
||||
download = allDls[message]
|
||||
return download[0]
|
||||
|
||||
|
||||
|
||||
def progress_status(context,update,previous):
|
||||
download = get_download_by_message(update)
|
||||
file = aria2.get_download(download.gid)
|
||||
if not file.is_complete:
|
||||
if not file.error_message:
|
||||
msg = "<i>"+str(file.name) +"</i>:- " +str(file.progress_string())+" of "+str(file.total_length_string())+" at "+str(file.download_speed_string())+" ,ETA: "+str(file.eta_string())
|
||||
if previous != msg:
|
||||
print("editing message")
|
||||
try:
|
||||
context.bot.edit_message_text(text=msg,message_id=update.message_id,chat_id=update.chat.id,parse_mode='HTMl')
|
||||
except:
|
||||
pass
|
||||
previous = msg
|
||||
sleep(5)
|
||||
progress_status(context,update,previous)
|
||||
else:
|
||||
logging.error(file.error_message)
|
||||
return
|
||||
else:
|
||||
try:
|
||||
new_gid = check_metadata(file.gid)
|
||||
except:
|
||||
new_gid = None
|
||||
pass
|
||||
if new_gid:
|
||||
download = aria2.get_download(new_gid)
|
||||
allDls[update][0] = download
|
||||
progress_status(context,update,previous=None)
|
||||
else:
|
||||
logging.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:
|
||||
f.write(file.name)
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
from telegram.ext import Updater,CommandHandler,run_async
|
||||
import ariaTools
|
||||
import logging
|
||||
import gdriveTools
|
||||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||||
level=logging.INFO)
|
||||
|
||||
|
||||
|
||||
|
||||
@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, 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)
|
||||
# with open('data','r') as f:
|
||||
# link = f.read()
|
||||
msg = "<i>"+str(file_name) +"</i>\n"+link
|
||||
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 = "976868081:AAEO--j0dqomyy0ZOYD0sSIGim4UHTYQg5E"
|
||||
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()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
class Config(object):
|
||||
"""docstring for Config"""
|
||||
G_DRIVE_CLIENT_ID = ""
|
||||
G_DRIVE_CLIENT_SECRET = ""
|
||||
GDRIVE_FOLDER_ID = ""
|
||||
|
|
@ -0,0 +1,182 @@
|
|||
from apiclient.discovery import build
|
||||
from apiclient.http import MediaFileUpload
|
||||
from apiclient.errors import ResumableUploadError
|
||||
from oauth2client.client import OAuth2WebServerFlow
|
||||
from oauth2client.file import Storage
|
||||
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)
|
||||
|
||||
|
||||
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
|
||||
# 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
|
||||
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.")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def upload(fileName):
|
||||
try:
|
||||
with open(G_DRIVE_TOKEN_FILE) as f:
|
||||
pass
|
||||
except IOError:
|
||||
storage = create_token_file(G_DRIVE_TOKEN_FILE)
|
||||
http = authorize(G_DRIVE_TOKEN_FILE, storage)
|
||||
print("Uploading File: "+fileName)
|
||||
if os.path.isfile(fileName):
|
||||
http = authorize(G_DRIVE_TOKEN_FILE, None)
|
||||
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)
|
||||
link = g_drive_link
|
||||
except Exception as e:
|
||||
logging.error(str(e))
|
||||
pass
|
||||
else:
|
||||
http = authorize(G_DRIVE_TOKEN_FILE, None)
|
||||
file_name, mime_type = file_ops(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)
|
||||
dir_link = "https://drive.google.com/folderview?id={}".format(dir_id)
|
||||
link = dir_link
|
||||
except Exception as e:
|
||||
logging.error(str(e))
|
||||
pass
|
||||
return link
|
||||
# with open('data','w') as f:
|
||||
# f.write(link)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def create_directory(http, directory_name, parent_id):
|
||||
drive_service = build("drive", "v2", http=http, cache_discovery=False)
|
||||
permissions = {
|
||||
"role": "reader",
|
||||
"type": "anyone",
|
||||
"value": None,
|
||||
"withLink": True
|
||||
}
|
||||
file_metadata = {
|
||||
"title": directory_name,
|
||||
"mimeType": G_DRIVE_DIR_MIME_TYPE
|
||||
}
|
||||
if parent_id is not None:
|
||||
file_metadata["parents"] = [{"id": 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))
|
||||
return file_id
|
||||
|
||||
|
||||
def DoTeskWithDir(http, input_directory, parent_id):
|
||||
list_dirs = os.listdir(input_directory)
|
||||
if len(list_dirs) == 0:
|
||||
return parent_id
|
||||
r_p_id = None
|
||||
for a_c_f_name in list_dirs:
|
||||
current_file_name = os.path.join(input_directory, a_c_f_name)
|
||||
if os.path.isdir(current_file_name):
|
||||
current_dir_id = create_directory(http, a_c_f_name, parent_id)
|
||||
r_p_id = DoTeskWithDir(http, current_file_name,current_dir_id)
|
||||
else:
|
||||
file_name, mime_type = file_ops(current_file_name)
|
||||
# current_file_name will have the full path
|
||||
g_drive_link = upload_file(http, current_file_name, file_name, mime_type, parent_id)
|
||||
r_p_id = parent_id
|
||||
# TODO: there is a #bug here :(
|
||||
return r_p_id
|
||||
|
||||
def file_ops(file_path):
|
||||
mime_type = guess_type(file_path)[0]
|
||||
mime_type = mime_type if mime_type else "text/plain"
|
||||
file_name = file_path.split("/")[-1]
|
||||
return file_name, mime_type
|
||||
|
||||
|
||||
def create_token_file(token_file):
|
||||
# Run through the OAuth flow and retrieve credentials
|
||||
flow = OAuth2WebServerFlow(
|
||||
CLIENT_ID,
|
||||
CLIENT_SECRET,
|
||||
OAUTH_SCOPE,
|
||||
redirect_uri=REDIRECT_URI
|
||||
)
|
||||
authorize_url = flow.step1_get_authorize_url()
|
||||
print('Go to the following link in your browser: ' + authorize_url)
|
||||
code = input('Enter verification code: ').strip()
|
||||
credentials = flow.step2_exchange(code)
|
||||
storage = Storage(token_file)
|
||||
storage.put(credentials)
|
||||
return storage
|
||||
|
||||
def authorize(token_file, storage):
|
||||
# Get credentials
|
||||
if storage is None:
|
||||
storage = Storage(token_file)
|
||||
credentials = storage.get()
|
||||
# Create an httplib2.Http object and authorize it with our credentials
|
||||
http = httplib2.Http()
|
||||
credentials.refresh(http)
|
||||
http = credentials.authorize(http)
|
||||
return http
|
||||
|
||||
|
||||
|
||||
|
||||
def upload_file(http, file_path,file_name, mime_type, parent_id):
|
||||
# Create Google Drive service instance
|
||||
drive_service = build('drive', 'v2', http=http, cache_discovery=False)
|
||||
# File body description
|
||||
media_body = MediaFileUpload(file_path,
|
||||
mimetype=mime_type,
|
||||
resumable=True)
|
||||
body = {
|
||||
'title': file_name,
|
||||
'description': 'backup',
|
||||
'mimeType': mime_type,
|
||||
}
|
||||
if parent_id is not None:
|
||||
body["parents"] = [{"id": parent_id}]
|
||||
# Permissions body description: anyone who has link can upload
|
||||
# Other permissions can be found at https://developers.google.com/drive/v2/reference/permissions
|
||||
permissions = {
|
||||
'role': 'reader',
|
||||
'type': 'anyone',
|
||||
'value': None,
|
||||
'withLink': True
|
||||
}
|
||||
# Insert a file
|
||||
file = drive_service.files().insert(body=body, media_body=media_body).execute()
|
||||
# Insert new permissions
|
||||
drive_service.permissions().insert(fileId=file['id'], body=permissions).execute()
|
||||
# Define file instance and get url for download
|
||||
file = drive_service.files().get(fileId=file['id']).execute()
|
||||
download_url = file.get('webContentLink')
|
||||
return download_url
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
google-api-python-client>=1.7.3
|
||||
google-auth>=1.5.0
|
||||
google-auth-httplib2<=0.0.3
|
||||
httplib2>=0.11.3
|
||||
more-itertools>=4.2.0
|
||||
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
|
||||
|
||||
Loading…
Reference in New Issue