-
Notifications
You must be signed in to change notification settings - Fork 20
/
ds.py
executable file
·151 lines (142 loc) · 6.57 KB
/
ds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from discordstorage import core
import threading,json,asyncio,random,sys,argparse,os,time
TOKEN_SECRET = "" #bot's secret token
ROOM_ID = "" #channel text ID
BOT_INFO = None
FILES = None
#Generates a file code from 0-4097
def genCode():
code = str(random.randint(0,4098))
if FILES == None:
return code
while code in FILES.keys():
code = str(random.randint(0,4098))
return code
#returns if the config file is configured or not.
def isConfigured():
return os.path.isfile('config.discord')
#invokes file uploading, to be used on a thread that's not in main thread.
#writes to config file accordingly.
def tellupload(line1,line2,cmd,code,client):
while not (client.isready()):
time.sleep(0.5)
if not os.path.isfile(cmd):
print('[ERROR] File does not exist.')
client.logout()
return
flcode = client.upload(cmd,code)
if flcode == -1:
print('[ERROR] File upload fail')
else:
jobject = json.loads(line2)
jobject[code] = flcode
f = open('config.discord','w')
f.write(line1)
f.write(json.dumps(jobject))
f.close()
print('[DONE] File upload complete')
client.logout()
def GetHumanReadable(size,precision=2):
suffixes=['B','KB','MB','GB','TB']
suffixIndex = 0
while size > 1024 and suffixIndex < 4:
suffixIndex += 1 #increment the index of the suffix
size = size/1024.0 #apply the division
return "%.*f%s"%(precision,size,suffixes[suffixIndex])
#invokes file downloading, to be used on a thread that's not in main thread
def telldownload(client,inp):
while not (client.isready()):
time.sleep(0.5)
client.download(inp)
client.logout()
#parses cmd line arguments
def parseArgs(inp):
commands = ['-h','-help','-l','-list','-d','-download','-u','-upload']
if(len(inp) == 1):
print('----------------------\n|DiscordStorage v0.1 |')
print('|github.com/nigelchen|\n----------------------')
print('\nUsage: python ds.py [command] (target)\n')
print('COMMANDS:')
print('[-h, -help] :: Show the current message')
print('[-l, -list] :: Lists all the file informations that has been uploaded to the server.')
print('[-d, -download] (FILE CODE) :: Downloads a file from the server. A filecode is taken in as the file identifier.')
print('[-u, -upload] (FILE DIRECTORY) :: Uploads a file to the server. The full file directory is taken in for the argument.\n')
elif isConfigured():
f = open('config.discord','r')
first = f.readline()
second = f.readline()
f.close()
TOKEN_SECRET = json.loads(first.replace("\\n",""))['TOKEN']
for el in inp:
if '-d' == el or '-download' == el:
if not ((not(FILES == None)) and (inp[inp.index(el)+1] in FILES.keys())):
print('\n[ERROR] File code not found\n')
else:
obj = json.loads(second)[inp[inp.index(el)+1]]
print('DOWNLOADING: ' + obj[0] )
print('SIZE: ' + GetHumanReadable(obj[1]))
client = core.Core(os.getcwd() + "/",TOKEN_SECRET,ROOM_ID)
threading.Thread(target=telldownload,args=(client,obj,)).start()
client.start()
break
elif '-u' == el or '-upload' == el:
print('UPLOADING: ' + inp[inp.index(el)+1])
client = core.Core(os.getcwd() + "/",TOKEN_SECRET,ROOM_ID)
threading.Thread(target=tellupload,args=(first,second,inp[inp.index(el)+1],genCode(),client,)).start()
client.start()
break
elif '-list' == el or '-l' == el:
if not (FILES == None):
print('\nFILES UPLOADED TO DISCORD:\n')
for key in FILES.keys():
if FILES[key] == None:
#correct nullfied attribute
print(' [CONSOLE] Removed incorrect file with filecode ' + str(key))
f = open('config.discord','w')
f.write(first)
jobject = json.loads(second)
del jobject[key]
f.write(json.dumps(jobject))
f.close()
else:
print('name: ' + str(FILES[key][0]) + ' | code: ' + str(key) + ' | size: ' + GetHumanReadable(FILES[key][1]) +'')
print('\n')
break
elif '-help' == el or '-h' == el:
print('----------------------\n|DiscordStorage|')
print('|github.com/nigel|\n----------------------')
print('\nUsage: python ds.py [command] (target)\n')
print('COMMANDS:')
print('[-h, -help] :: Show the current message')
print('[-l, -list] :: Lists all the file informations that has been uploaded to the server.')
print('[-d, -download] (FILE CODE) :: Downloads a file from the server. A filecode is taken in as the file identifier.')
print('[-u, -upload] (FILE DIRECTORY) :: Uploads a file to the server. The full file directory is taken in for the argument.\n')
if not isConfigured():
print('Welcome to DiscordStorage.')
print('Go to http://github.com/nigelchen/DiscordStorage for instructions.')
TOKEN_SECRET = input('Bot token ID (Will be stored in plaintext in config file):')
ROOM_ID = input ('Enter channel ID to store files in:')
if len(ROOM_ID) <=0:
ROOM_ID = None
f = open('config.discord','w')
f.write(str(json.dumps({'TOKEN':TOKEN_SECRET,'ROOM_ID':ROOM_ID})) + "\n")
f.write(str(json.dumps({})))
f.close()
else:
f = open('config.discord','r')
first = f.readline()
second = f.readline()
BOT_INFO = json.loads(first)
FILES = json.loads(second)
TOKEN_SECRET = json.loads(first.replace("\\n",""))['TOKEN']
ROOM_ID = json.loads(first.replace("\\n",""))['ROOM_ID']
f.close()
try:
parseArgs(sys.argv)
except IndexError:
print('\nUsage: python ds.py [command] (target)\n')
print('COMMANDS:')
print('[-h, -help] :: Show the help message')
print('[-l, -list] :: Lists all the file informations that has been uploaded to the server.')
print('[-d, -download] (FILE CODE) :: Downloads a file from the server. A filecode is taken in as the file identifier.')
print('[-u, -upload] (FILE DIRECTORY) :: Uploads a file to the server. The full file directory is taken in for the argument.\n')