def index(request): downbox = Download_Settings.objects.latest('id').download_box quickdown_value = Quick_Download.objects.latest('id').priority contain=[] if Quick_Download_Contains.objects.all(): for x in Quick_Download_Contains.objects.all(): contains={} contains['id'] = x.id contains['text'] = x.contains contain.append(contains) exclude=[] if Quick_Download_Excludes.objects.all(): for x in Quick_Download_Excludes.objects.all(): excludes = {} excludes['id'] = x.id excludes['text'] = x.excludes exclude.append(excludes) try: pathinstance = Download_Path.objects.latest('id') pathform = PathForm(instance=pathinstance) except: pathform = PathForm() context = { 'directory' : directory(), 'free_space' : get_free_space_mb(), 'quickdown_value' : quickdown_value, 'contains' : contain, 'excludes' : exclude, 'pathform' : pathform, 'downbox' : downbox, } return render(request, 'search.html', context)
def process(self, c, e, resume=False): #"When receiving a file with DCC, accept it" downloads_dir = directory() bot = e.source.nick try: connection = self.dcc_connect(self.ip, self.port, "raw") except irc.client.DCCConnectionError as e: log('Couldn\'t connect to DCC peer: %s' % e).write() self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Peer connection error", utcnow() self.dict[bot]["hist"].save() views.manage_queue(self.dict[bot]["down"]) return "" if resume: self.dict[bot]["file"] = open(os.path.join(downloads_dir, self.dict[bot]["filename"]), 'ab') self.dict[bot]["down"].filename, self.dict[bot]["down"].status = ( self.dict[bot]["filename"], "Resuming download...") log('Resuming download of %s' % self.dict[bot]["filename"]).write() else: self.dict[bot]["file"] = open(os.path.join(downloads_dir, self.dict[bot]["filename"]), 'wb') self.dict[bot]["down"].filename, self.dict[bot]["down"].status, self.dict[bot]["down"].sizeraw = ( self.dict[bot]["filename"], "Starting download...", self.dict[bot]["size"]) log('Starting download of %s' % self.dict[bot]["filename"]).write() self.DCCconnections_dict[bot] = connection self.dict[bot]["hist"].time = utcnow() self.dict[bot]["down"].save() t = threading.Timer(0, monitoring, [self, connection, bot, int(self.dict[bot]["position"])]) t.start() s=threading.Timer(0, self.retry_pack, [connection, bot]) s.daemon=True s.start()
def on_dcc_disconnect(self, connection, event, bot=""): for key, value in self.DCCconnections_dict.items(): if value == connection: bot = key break try: self.dict[bot]["file"].close() except: bot = bot if self.dict[bot]["stop"]: return "" if self.dict[bot]["cancel"] == False: (self.dict[bot]["down"].status, self.dict[bot]["down"].progress, self.dict[bot]["down"].speed, self.dict[bot]["down"].progress, self.dict[bot]["down"].eta) = "Extracting...", None, None, None, None self.dict[bot]["down"].save() tot = self.dict[bot]["received_bytes"]+self.dict[bot]["position"] log("Received file (%d bytes)." % tot ).write() log("Size (%d bytes)." % int(self.dict[bot]["size"]) ).write() percdone = tot/int(self.dict[bot]["size"]) duration = (utcnow() - self.dict[bot]["hist"].time) average = int(self.dict[bot]["size"])/1024/duration.total_seconds() (self.dict[bot]["hist"].filename, self.dict[bot]["hist"].status, self.dict[bot]["hist"].average, self.dict[bot]["hist"].duration, self.dict[bot]["hist"].time, self.dict[bot]["hist"].sizeraw) = (self.dict[bot]["filename"], "Downloaded", round(average, 0), duration, utcnow(), self.dict[bot]["size"]) self.dict[bot]["hist"].save() if os.path.exists(os.path.join(directory(), self.dict[bot]["filename"])): #added to prevent extracting incomplete files (internet connection interrupted) if percdone > 99/100: untar(os.path.join(directory(), self.dict[bot]["filename"]), self.dict[bot]["filename"], self.dict[bot]["down"], self.dict[bot]["hist"]) return "" self.dict[bot]["down"].status = "Error during file transfer" self.dict[bot]["down"].save() self.dict[bot]["hist"].status, self.dict[bot]["hist"].time = "Error during file transfer", utcnow() self.dict[bot]["hist"].save() log("Error during file transfer. Completed percentage: %d" % int(percdone*100) ).write() print ("2") views.manage_queue(self.dict[bot]["down"]) return "" else: self.dict[bot]["cancel"] = False views.manage_queue(self.dict[bot]["down"]) return ""
def opendir(request): pk, myclass = int(request.GET['pk']), request.GET['myclass'] if directory(): basefolder = directory() foldername = basefolder if myclass == "history": fileobject = Download_History.objects.get(pk=pk) if "extracted" in fileobject.status and ".tar" in splitext(fileobject.filename)[1]: foldername = os.path.join(basefolder, splitext(fileobject.filename)[0]) else: fileobject = Download_Ongoing.objects.get(pk=pk) if "Extracting" in fileobject.status and ".tar" in splitext(fileobject.filename)[1]: foldername = os.path.join(basefolder, '_UNPACK_' + splitext(fileobject.filename)[0]) elif "extracted" in fileobject.status and ".tar" in splitext(fileobject.filename)[1]: foldername = os.path.join(basefolder, splitext(fileobject.filename)[0]) if sys.platform == "win32": os.startfile(foldername) else: opener ="open" if sys.platform == "darwin" else "xdg-open" subprocess.call([opener, foldername]) return HttpResponse("succefully opened directory") else: return HttpResponse("No directory set to open dir")
def get_free_space_mb(): """ Return folder/drive free space (in megabytes). internal purpose only, not a view """ dirname = directory() if os.path.exists(dirname): if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(dirname), None, None, ctypes.pointer(free_bytes)) return round(free_bytes.value / (1024**3), 2) else: st = os.statvfs(dirname) return round(st.f_bavail * st.f_frsize / (1024**3), 2) else: return -1
def search(request): q = request.GET['q'] try: pn = int(request.GET['pn']) except: pn = 0 try: noquick = request.GET['noquick'] except: noquick = False result = getinfo(q, pn) contain=[] if Quick_Download_Contains.objects.all(): for x in Quick_Download_Contains.objects.all(): contains={} contains['id'] = x.id contains['text'] = x.contains contain.append(contains) exclude=[] if Quick_Download_Excludes.objects.all(): for x in Quick_Download_Excludes.objects.all(): excludes = {} excludes['id'] = x.id excludes['text'] = x.excludes exclude.append(excludes) downbox = Download_Settings.objects.latest('id').download_box pathinstance = Download_Path.objects.latest('id') pathform = PathForm(instance=pathinstance) context = { 'directory' : directory(), 'free_space' : get_free_space_mb(), 'pathform' : pathform, 'q' : q, 'noquick' : noquick, 'contains' : contain, 'excludes' : exclude, 'downbox' : downbox } try: context['results'] = result["results"] context['num'] = result["num"] context['pn'] = result["pn"] except: context['message'] = result return render(request, 'results.html', context)
def on_dccsend(self, c, e): bot = e.source.nick if self.direct: self.dict[bot] = self.dict.pop(self.bot) self.bot = bot #if the user cancels the download right away we're getting the DCC Send message, we'll still connect, which is bad, let's catch cancel if not self.dict[bot]["cancel"]: downloads_dir = directory() if e.arguments[1].split(" ", 1)[0] == "SEND": proceed = True if self.direct: dw = self.dict[bot]["down"] main.queuedict[dw.server+self.bot] = main.queuedict.pop(dw.server+dw.username) self.dict[bot]["down"].username = self.bot self.dict[bot]["down"].save() if e.arguments[1].split(" ")[5] == self.dict[bot]["pw"]: print("password correct") else: #invalid password print("bad password") c.ctcp("CHAT", e.source.nick, "wrong password") proceed = False if proceed: if '"' in e.arguments[1]: #remove spaces from filename one = e.arguments[1].find('"') two = e.arguments[1].find('"', one + 1) new = e.arguments[1][one+1:two].replace(" ", ".") e.arguments[1] = e.arguments[1][:one] + new + e.arguments[1][two+1:] self.dict[bot]["filename"] = e.arguments[1].split(" ")[1] self.port = int(e.arguments[1].split(" ")[3]) self.ip = irc.client.ip_numstr_to_quad(e.arguments[1].split(" ")[2]) try: #update size; sometimes the given size (on the search engine) isnt the same as the sent size self.dict[bot]["size"] = self.dict[bot]["down"].sizeraw = int(e.arguments[1].split(" ")[4]) except: pass if os.path.exists(os.path.join(downloads_dir, self.dict[bot]["filename"])): print ("resuming download") self.dict[bot]["position"] = os.path.getsize(os.path.join(downloads_dir, self.dict[bot]["filename"])) c.ctcp("DCC", e.source.nick, "RESUME %s %d %d" % (self.dict[bot]["filename"], self.port, self.dict[bot]["position"])) else: self.process(c, e) elif e.arguments[1].split(" ", 1)[0] == "ACCEPT": self.process(c, e, resume=True)
def untar(completefile, filename, down, hist): log("Extracting the file...").write() log(completefile + ' ' + filename).write() # tar file to extract theTarFile = completefile # tar file path to extract extractTarPath = os.path.join(directory(), '_UNPACK_' + splitext(filename)[0]) if tarfile.is_tarfile(theTarFile): # extract all contents try: tfile = tarfile.open(theTarFile) tfile.extractall(extractTarPath) tfile.close() os.remove(theTarFile) #extract rar files x=0 completefile = False #this test is added in case some txt is extracted with the folder while not os.path.isdir(os.path.join(extractTarPath, os.listdir(extractTarPath)[x])): #sometimes the file extracted is the final file, no futher rar extraction needed; in this case, test for it's size and finish if os.path.getsize(os.path.join(extractTarPath, os.listdir(extractTarPath)[x])) > 90/100*hist.sizeraw: completefile = True break x+=1 if not completefile: origin = os.path.join(extractTarPath, os.listdir(extractTarPath)[x]) log(origin).write() for fl in os.listdir(origin): shutil.move(os.path.join(origin, fl), extractTarPath) shutil.rmtree(origin) for archivefile in os.listdir(extractTarPath): if ".rar" in archivefile: arch_ref = rarfile.RarFile(os.path.join(extractTarPath, archivefile),'r') arch_ref.extractall(extractTarPath) #remove rest for fl in os.listdir(extractTarPath): ext = splitext(fl)[1].lower() pathToDelete = os.path.join(extractTarPath, fl) if os.path.isdir(pathToDelete): # Do not delete non-empty dirs, could contain useful files. try: os.rmdir(pathToDelete); except OSError as ex: if ex.errno == errno.ENOTEMPTY: log("Did not delete non-empty directory : %s" % pathToDelete).write() else: log("An error occurred deleting directory : %s" % pathToDelete).write() else: if ext[:2] == '.r' or ext in ['.sfv', '.nfo', '.png', '.jpg'] or 'sample' in fl: os.remove(os.path.join(extractTarPath, fl)) #remove UNPACK from name when done os.rename(extractTarPath, os.path.join(directory(), splitext(filename)[0])) except Exception as e: log("An error occured during file extraction : %s" % e).write() hist.status, hist.time = "Error during file extraction", utcnow() down.status, down.sizeraw, down.active = "Error during file extraction", None, False down.save() hist.save() print ("4") views.manage_queue(down) return "" else: log(theTarFile + " is not a tarfile.").write() down.status, down.sizeraw, down.active = "Downloaded and extracted properly", None, False hist.status, hist.time = "Downloaded and extracted properly", utcnow() hist.save() down.save() print ("5") views.manage_queue(down) return ""