-
Notifications
You must be signed in to change notification settings - Fork 0
/
tachyon.py
executable file
·136 lines (109 loc) · 5.26 KB
/
tachyon.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
#!/usr/bin/env python3
import tools as tools
try:
import os, re, threading, math, requests, click, time
from tqdm import tqdm
except:
print("One of the modules has not been imported\nEnsure the following modules are installed:\nos, re, threading, math, requests, click, time")
class tachydownload():
global text
text = tools.textEffects()
global chunkSize
global threadNum
global downloadUrl
global fileName
global filePath
global fileSize
def __init__(self, downloadUrl, fileName, fileSize):
self.downloadUrl = downloadUrl
self.fileName = fileName
self.fileSize = fileSize
self.threadNum = 0
self.threadDivisor()
self.variableSetup()
tools.printText(text.formatText("BOLD", "Downloading file: \"" + self.fileName + "\""))
def consoleMsg(self, type, oldTime=time.time()):
if type=="header":
tools.printText("Tachyon", asciiArt=True)
tools.printText(text.formatText("BOLD", "\nThe faster than light media downloader"))
elif type=="completion":
tools.printText(text.formatText("BOLD", "\nSuccess! 🎉 🎉 🎉\nTime taken: " + str(time.time() - oldTime) + " seconds\n" + "File located at: ~/Desktop/Downloads by Tachyon/" + self.fileName))
elif type=="sysfail":
tools.printText(text.formatText("BOLD", "One of the modules has not been imported\nEnsure the following modules are installed:\nos, re, threading, math, requests, click, time"))
def threadDivisor(self):
'''for newThreadNum in range(1, 200):
chunkDivider = self.fileSize % newThreadNum
if chunkDivider == 0 and newThreadNum > self.threadNum:
'''
self.threadNum = 98
self.chunkSize = math.ceil(self.fileSize/self.threadNum)
self.threadNum += 1
print("Filesize: %s Chunksize: %s Threadnum: %s" % (self.fileSize, self.chunkSize, self.threadNum))
def variableSetup(self):
if tools.downloadDirectory() is not None:
self.filePath = os.path.join(tools.downloadDirectory(), self.fileName)
else:
self.filePath = os.path.join(os.path.join(os.path.expanduser("~"), "Desktop"), self.fileName)
def newThread(self, origin):
'''with tqdm(total=130, ascii=True, ncols=100) as progBar:
for i in range(130):
with open(self.filePath, mode='a') as outputFile:
outputFile.write("01")
progBar.update(1)
'''
range = {"Range": "bytes=%s-%s" % (origin, origin+self.chunkSize-1)}
fileReq = requests.get(self.downloadUrl, headers=range, stream=True)
file = fileReq.content
# Each thread creates a hidden dotfile within the tachyon folder
with open(os.path.join(tools.downloadDirectory(), "." + str(origin)), "wb") as hiddenFile:
hiddenFile.write(file)
def main(self):
oldTime = time.time()
threads = []
with tqdm(total=self.threadNum*2, ascii=True, ncols=100) as progBar:
for threadID in range(0, self.threadNum):
thread = threading.Thread(target = self.newThread, args=(threadID*self.chunkSize,))
thread.start()
threads.append(thread)
progBar.update(1)
for thread in threads:
thread.join()
progBar.update(1)
self.filePath = open(self.filePath, "wb")
for sections in range(0, self.threadNum):
readFile = open(os.path.join(tools.downloadDirectory(), "." + str(sections*self.chunkSize)), "rb")
self.filePath.write(readFile.read())
os.remove(os.path.join(tools.downloadDirectory(), "." + str(sections*self.chunkSize)))
self.filePath.close()
self.consoleMsg("completion", oldTime)
settings = dict(help_option_names=['--help', '-h'])
@click.command(context_settings=settings)
@click.option("--name", "-n", metavar="[TEXT]", help="The name which the downloaded file will be saved under.")
@click.argument("download_url", type=click.Path())
def executeDownload(name, download_url):
"""\b
######## ### ###### ## ## ## ## ####### ## ##
## ## ## ## ## ## ## ## ## ## ## ### ##
## ## ## ## ## ## #### ## ## #### ##
## ## ## ## ######### ## ## ## ## ## ##
## ######### ## ## ## ## ## ## ## ####
## ## ## ## ## ## ## ## ## ## ## ###
## ## ## ###### ## ## ## ####### ## ##
Tachyon — The faster than light media downloader.
(c) 2018 Akshat Bisht under the MIT license."""
global filename
global filesize
try:
urlInfo = requests.head(download_url)
filesize = int(urlInfo.headers["Content-Length"])
except:
print("Try another download URL or type \"tachyon.py --help\" for help.\n\nError: Input argument \"DOWNLOAD_URL\" appears to be corrupt.")
return
if name:
filename = name
else:
filename = download_url.split('/')[-1]
run = tachydownload(downloadUrl=download_url, fileName=filename, fileSize=filesize)
run.main()
if __name__ == '__main__':
executeDownload()