def add_torrent(request): # TODO: torrent_file is higher priority than url method, is it okay? if request.FILES.has_key('torrent_file') is True: torrent_file = request.FILES['torrent_file'] data = torrent_file.read() elif request.POST.has_key('torrent_url') is True: url = request.POST['torrent_url'] response = requests.get(url) data = response.content else: #TODO: notificate to user, error! print "DEBUG:: Not supported yet..." return redirect("/") e = lt.bdecode(data) info = lt.torrent_info(e) torrent_hash = str(info.info_hash()) # get current user information u = User.objects.get(username=request.user.username) account = Account.objects.get(user=u) # check uniqueness torrent_entry = TorrentEntries.objects.filter( hash_value=torrent_hash).first() if torrent_entry and torrent_entry.progress == 100.0: new_entry = TorrentEntries( name=torrent_entry.name, hash_value=torrent_hash, progress=torrent_entry.progress, download_rate=torrent_entry.download_rate, owner=account, file_size=torrent_entry.file_size, downloaded_size=torrent_entry.downloaded_size, peers=torrent_entry.peers, status=torrent_entry.status, worker_pid=os.getpid()) new_entry.save() return redirect("/") # Background torrent download tasks.TorrentDownload.delay(account, data) return redirect("/")
def add_torrent(request): # TODO: torrent_file is higher priority than url method, is it okay? if request.FILES.has_key('torrent_file') is True: torrent_file = request.FILES['torrent_file'] data = torrent_file.read() elif request.POST.has_key('torrent_url') is True: url = request.POST['torrent_url'] response = requests.get(url) data = response.content else: #TODO: notificate to user, error! print "DEBUG:: Not supported yet..." return redirect("/") e = lt.bdecode(data) info = lt.torrent_info(e) torrent_hash = str(info.info_hash()) # get current user information u = User.objects.get(username = request.user.username) account = Account.objects.get(user=u) # check uniqueness torrent_entry = TorrentEntries.objects.filter(hash_value=torrent_hash).first() if torrent_entry and torrent_entry.progress == 100.0: new_entry=TorrentEntries(name=torrent_entry.name, hash_value=torrent_hash, progress=torrent_entry.progress, download_rate=torrent_entry.download_rate, owner=account, file_size=torrent_entry.file_size, downloaded_size=torrent_entry.downloaded_size, peers=torrent_entry.peers, status=torrent_entry.status, worker_pid=os.getpid()) new_entry.save() return redirect("/") # Background torrent download tasks.TorrentDownload.delay(account, data) return redirect("/")
def TorrentDownload(account, data): import libtorrent as lt import urllib2 import time #TODO: We have to support Three mode, URL, magnet, File # register signal signal.signal(signal.SIGINT, receive_signal) global download_flag download_flag = True ses = lt.session() ses.listen_on(6881, 6891) e = lt.bdecode(data) info = lt.torrent_info(e) info_hash = info.info_hash() #TODO: if torrent_entry progress is not 100.0 ?... # instead duplicated download, we have to provide mirror of one # actually download only one torrent, others just get information of it not download it # Add torrent h = ses.add_torrent({'ti': info, 'save_path': TorrentStorage_PATH}) # Torrent validation check if h.is_valid() is not True: print "[!] Not valid torrent!" return # Save progress and torrent download status into DB new_entry = TorrentEntries(name=str(h.name()), hash_value=str(info_hash), progress=0, download_rate=0, owner=account, file_size=int(h.status().total_wanted), downloaded_size=0, peers=0, status="download", worker_pid=os.getpid()) try: new_entry.save() except: print "[!] Database insert Error" return while (not h.is_seed() and download_flag is True): s = h.status() new_entry.progress = float("%.2f" % (s.progress * 100)) new_entry.download_rate = s.download_rate new_entry.downloaded_size = s.total_done new_entry.peers = s.num_peers new_entry.save() time.sleep(1) filename = TorrentStorage_PATH + "/" + new_entry.name # User cancel torrent download during downloading... if download_flag is False: new_entry.delete() else: if os.path.isdir(filename): # make folder to zip file new_entry.status = "compressing" new_entry.save() compress_data(TorrentStorage_PATH, new_entry.name) # remove original directory shutil.rmtree(filename) new_entry.name = new_entry.name + ".zip" print "[+] Done: " + str(h.name()) new_entry.progress = 100 new_entry.downloaded_size = new_entry.file_size new_entry.download_rate = 0 new_entry.peers = 0 new_entry.status = "finished" new_entry.save()
def add_torrent(request): # TODO: Support magnet if request.FILES.has_key('torrent_file') is True: torrent_file = request.FILES['torrent_file'] data = torrent_file.read() elif request.POST.has_key('torrent_url') is True: url = request.POST['torrent_url'] val = URLValidator() try: val(url) except: messages.error(request, "Invalid torrent URL! Please check your torrent URL") return redirect("/") # TODO: SSL error occur, we have to fix it. response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}) data = response.content else: messages.error(request, "Please add torrent file by URL or file") return redirect("/") try: e = lt.bdecode(data) except: messages.error(request, "Broken or invalid torrent file! Please check your torrent URL or file") return redirect("/") info = lt.torrent_info(e) torrent_hash = str(info.info_hash()) # get current user information u = User.objects.get(username = request.user.username) account = Account.objects.get(user=u) # If current user have this torrent entry, then nothing todo. redirect to root. torrent_entry = TorrentEntries.objects.filter(owner=account, hash_value=torrent_hash) if torrent_entry: messages.error(request, "You already have this torrent") return redirect("/") #TODO: Try download same torrent more than two user at the same time # instead of duplicated downloading, we have to provide mirror of one # actually download only one torrent, others just get information of it. Not download it # Duplication check, if current user doesn't have this torrent entry but torrent file exist in server # then just update current user's torrent entry and redirect to root. (this save worker thread) # Since we don't have to re-download same file (file is already exist in server) torrent_entry = TorrentGlobalEntries.objects.filter(hash_value=torrent_hash, status="finished").first() if torrent_entry: new_entry=TorrentEntries(name = torrent_entry.name, hash_value = torrent_entry.hash_value, progress = torrent_entry.progress, download_rate = 0, owner = account, file_size = torrent_entry.file_size, downloaded_size = torrent_entry.downloaded_size, peers = torrent_entry.peers, status = torrent_entry.status) new_entry.save() return redirect("/") # Calculate the number of waiting torrents in queue waitings=TorrentEntries.objects.filter(status="queued").count() waitings+=1 # Initialize new user torrent entry. status must be initialized with queued. new_entry=TorrentEntries(name = str(info.name()), hash_value = str(torrent_hash), progress = 0, download_rate = 0, owner = account, file_size = int(info.total_size()), downloaded_size = 0, peers = 0, status = "queued", priority = waitings) try: new_entry.save() except: messages.error(request, "Database insert error! Please notice this to admin") return redirect("/") # Background torrent download TorrentDownload.delay(account, data) return redirect("/")
def add_torrent(request): # TODO: Support magnet if request.FILES.has_key('torrent_file') is True: torrent_file = request.FILES['torrent_file'] data = torrent_file.read() elif request.POST.has_key('torrent_url') is True: url = request.POST['torrent_url'] val = URLValidator() try: val(url) except: messages.error( request, "Invalid torrent URL! Please check your torrent URL") return redirect("/") # TODO: SSL error occur, we have to fix it. response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'}) data = response.content else: messages.error(request, "Please add torrent file by URL or file") return redirect("/") try: e = lt.bdecode(data) except: messages.error( request, "Broken or invalid torrent file! Please check your torrent URL or file" ) return redirect("/") info = lt.torrent_info(e) torrent_hash = str(info.info_hash()) # get current user information u = User.objects.get(username=request.user.username) account = Account.objects.get(user=u) # If current user have this torrent entry, then nothing todo. redirect to root. torrent_entry = TorrentEntries.objects.filter(owner=account, hash_value=torrent_hash) if torrent_entry: messages.error(request, "You already have this torrent") return redirect("/") #TODO: Try download same torrent more than two user at the same time # instead of duplicated downloading, we have to provide mirror of one # actually download only one torrent, others just get information of it. Not download it # Duplication check, if current user doesn't have this torrent entry but torrent file exist in server # then just update current user's torrent entry and redirect to root. (this save worker thread) # Since we don't have to re-download same file (file is already exist in server) torrent_entry = TorrentGlobalEntries.objects.filter( hash_value=torrent_hash, status="finished").first() if torrent_entry: new_entry = TorrentEntries( name=torrent_entry.name, hash_value=torrent_entry.hash_value, progress=torrent_entry.progress, download_rate=0, owner=account, file_size=torrent_entry.file_size, downloaded_size=torrent_entry.downloaded_size, peers=torrent_entry.peers, status=torrent_entry.status) new_entry.save() return redirect("/") # Calculate the number of waiting torrents in queue waitings = TorrentEntries.objects.filter(status="queued").count() waitings += 1 # Initialize new user torrent entry. status must be initialized with queued. new_entry = TorrentEntries(name=str(info.name()), hash_value=str(torrent_hash), progress=0, download_rate=0, owner=account, file_size=int(info.total_size()), downloaded_size=0, peers=0, status="queued", priority=waitings) try: new_entry.save() except: messages.error(request, "Database insert error! Please notice this to admin") return redirect("/") # Background torrent download TorrentDownload.delay(account, data) return redirect("/")
def TorrentDownload(account, data): import libtorrent as lt import urllib2 import time #TODO: We have to support Three mode, URL, magnet, File # register signal signal.signal(signal.SIGINT, receive_signal) global download_flag download_flag=True ses = lt.session() ses.listen_on(6881, 6891) e = lt.bdecode(data) info = lt.torrent_info(e) info_hash = info.info_hash() #TODO: if torrent_entry progress is not 100.0 ?... # instead duplicated download, we have to provide mirror of one # actually download only one torrent, others just get information of it not download it # Add torrent h = ses.add_torrent({'ti': info, 'save_path': TorrentStorage_PATH}) # Torrent validation check if h.is_valid() is not True: print "[!] Not valid torrent!" return # Save progress and torrent download status into DB new_entry=TorrentEntries(name=str(h.name()), hash_value=str(info_hash), progress=0, download_rate=0, owner=account, file_size=int(h.status().total_wanted), downloaded_size=0, peers=0, status="initializing", worker_pid=os.getpid()) try: new_entry.save() except: print "[!] Database insert Error" return while (not h.is_seed() and download_flag is True): s = h.status() new_entry.progress=int("%d" % (s.progress * 100)) new_entry.download_rate=s.download_rate new_entry.downloaded_size=s.total_done new_entry.peers=s.num_peers new_entry.status="downloading" new_entry.save() time.sleep(1); filePath = os.path.join(TorrentStorage_PATH,new_entry.name) # User cancel torrent download during downloading... if download_flag is False: new_entry.delete() else: if os.path.isdir(filePath): # make folder to zip file new_entry.status="compressing" new_entry.save() compress_data(TorrentStorage_PATH, new_entry.name) # remove original directory shutil.rmtree(filePath) new_entry.name = new_entry.name + ".zip" print "[+] Done: " + str(h.name()) new_entry.progress=100 new_entry.downloaded_size = new_entry.file_size new_entry.download_rate = 0 new_entry.peers = 0 new_entry.status="finished" new_entry.save()