def add_torrent(request): if not request.user.has_perm("home.add_whattorrent"): return {"success": False, "error": "You don't have permission to add torrents. Talk to the administrator."} try: if "dir" in request.POST: download_location = DownloadLocation.objects.get(zone=ReplicaSet.ZONE_WHAT, path=request.POST["dir"]) else: download_location = DownloadLocation.get_what_preferred() except DownloadLocation.DoesNotExist: return {"success": False, "error": u"Download location does not exist."} if download_location.free_space_percent < MIN_FREE_DISK_SPACE: LogEntry.add(request.user, u"error", u"Failed to add torrent. Not enough disk space.") return {"success": False, "error": u"Not enough free space on disk."} try: what_id = int(request.POST["id"]) except (ValueError, MultiValueDictKeyError): return {"success": False, "error": u"Invalid id"} instance = ReplicaSet.get_what_master().get_preferred_instance() try: if WhatTorrent.is_downloaded(request, what_id=what_id): m_torrent = TransTorrent.objects.filter(what_torrent_id=what_id)[0] raise TorrentAlreadyAddedException() m_torrent = manage_torrent.add_torrent(request, instance, download_location, what_id, True) m_torrent.what_torrent.added_by = request.user m_torrent.what_torrent.save() except TorrentAlreadyAddedException: LogEntry.add(request.user, u"info", u"Tried adding what_id={0}, already added.".format(what_id)) what_torrent = WhatTorrent.get_or_none(request, what_id=what_id) result = { "success": False, "error_code": u"already_added", "error": u"Already added.", "torrent_id": m_torrent.what_torrent_id, } if m_torrent.what_torrent.info_category_id == 1: result["artist"] = what_torrent.info_artist if what_torrent else "<<< Unable to find torrent >>>" result["title"] = what_torrent.info_title if what_torrent else "<<< Unable to find torrent >>>" return result except Exception as ex: tb = traceback.format_exc() LogEntry.add(request.user, u"error", u"Tried adding what_id={0}. Error: {1}".format(what_id, unicode(ex)), tb) return {"success": False, "error": unicode(ex), "traceback": tb} tags = request.POST.get("tags") if tags: m_torrent.what_torrent.tags = tags m_torrent.what_torrent.save() LogEntry.add(request.user, u"action", u"Added {0} to {1}".format(m_torrent, m_torrent.instance)) result = {"success": True} if m_torrent.what_torrent.info_category_id == 1: result["artist"] = (m_torrent.what_torrent.info_artist,) result["title"] = (m_torrent.what_torrent.info_title,) return result
def update_freeleech(request): start_time = time.time() added = 0 total_bytes = 0 total_torrents = 0 try: master = ReplicaSet.get_what_master() what_client = get_what_client(request) for what_id, what_group, what_torrent in what_client.get_free_torrent_ids(): total_bytes += what_torrent['size'] total_torrents += 1 if not WhatTorrent.is_downloaded(request, what_id=what_id): freeleech_add_torrent(request, master, what_id) added += 1 log_type = u'action' if added > 0 else u'info' if added >= FREELEECH_EMAIL_THRESHOLD and socket.gethostname() == FREELEECH_HOSTNAME: send_freeleech_email(u'Added {0} freeleech torrents'.format(added)) time_taken = time.time() - start_time LogEntry.add(request.user, log_type, u'Successfully updated freeleech in {0:.3f}s. ' u'{1} added. {2} / {3} torrents total.'.format( time_taken, added, filesizeformat(total_bytes), total_torrents)) except Exception as ex: tb = traceback.format_exc() LogEntry.add(request.user, u'error', u'Error updating freeleech: {0}({1})'.format(type(ex).__name__, unicode(ex)), tb) return { 'success': True, 'added': added }
def handle(self, *args, **options): if not self.check_args(args): print u'Pass the torrent data directory as a first argument, ' \ u'a path to the .torrent file as a second.' return self.data_path, self.torrent_path = [wm_unicode(i) for i in args] with open(wm_str(self.torrent_path), 'rb') as f: self.torrent_info = bencode.bdecode(f.read()) if options['base_dir']: self.data_path = os.path.join(self.data_path, wm_unicode(self.torrent_info['info']['name'])) print u'Checking to see if torrent is already loaded into WM..' masters = list(ReplicaSet.get_what_master().transinstance_set.all()) try: TransTorrent.objects.get(instance__in=masters, info_hash=self.info_hash) print u'Torrent already added to WM. Skipping...' return False except TransTorrent.DoesNotExist: pass self.what_torrent = WhatTorrent.get_or_create(self.pseudo_request, info_hash=self.info_hash) if not self.check_files(): return self.move_files() print 'Adding torrent to WM...' manage_torrent.add_torrent(self.pseudo_request, self.trans_instance, self.download_location, self.what_torrent.id) print 'Done!'
def add_torrent(request, instance, download_location, what_id, add_to_client=True): w_torrent = WhatTorrent.get_or_create(request, what_id=what_id) with LockModelTables(TransTorrent, LogEntry): try: TransTorrent.objects.get(instance=instance, info_hash=w_torrent.info_hash) raise TorrentAlreadyAddedException(u"Already added.") except TransTorrent.DoesNotExist: pass if add_to_client: manager = transaction.atomic else: manager = dummy_context_manager with manager(): if True: m_torrent = TransTorrent( instance=instance, location=download_location, what_torrent=w_torrent, info_hash=w_torrent.info_hash ) m_torrent.save() if add_to_client: download_dir = os.path.join(download_location.path, unicode(w_torrent.id)) t_torrent = instance.client.add_torrent( w_torrent.torrent_file, download_dir=download_dir, paused=False ) t_torrent = instance.client.get_torrent(t_torrent.id, arguments=TransTorrent.sync_t_arguments) norm_t_torrent(t_torrent) m_torrent.sync_t_torrent(t_torrent) m_torrent.sync_files() return m_torrent
def upload_to_what(request, book_upload): book_upload.full_clean() if not book_upload.what_torrent_file: raise Exception('what_torrent is no') if not book_upload.cover_url: raise Exception('cover_url is no') print 'Sending request for upload to what.cd' what = get_what_client(request) payload_files = dict() payload_files['file_input'] = ('torrent.torrent', book_upload.what_torrent_file) payload = dict() payload['submit'] = 'true' payload['auth'] = what.authkey payload['type'] = '2' payload['title'] = book_upload.author + ' - ' + book_upload.title payload['tags'] = get_what_tags(book_upload) payload['image'] = book_upload.cover_url payload['desc'] = get_what_desc(book_upload) old_content_type = what.session.headers['Content-type'] upload_exception = None try: del what.session.headers['Content-type'] response = what.session.post(WHAT_UPLOAD_URL, data=payload, files=payload_files) if response.url == WHAT_UPLOAD_URL: try: errors = extract_upload_errors(response.text) except Exception: errors = '' exception = Exception( 'Error uploading data to what.cd. Errors: {0}'.format( '; '.join(errors))) exception.response_text = response.text raise exception except Exception as ex: upload_exception = ex finally: what.session.headers['Content-type'] = old_content_type try: new_torrent = safe_retrieve_new_torrent( what, get_info_hash_from_data(book_upload.what_torrent_file)) book_upload.what_torrent = WhatTorrent.get_or_create( request, what_id=new_torrent['torrent']['id']) book_upload.save() except Exception as ex: if upload_exception: raise upload_exception raise ex move_to_dest_add(request, book_upload) return book_upload.what_torrent
def update_freeleech(request): start_time = time.time() added = 0 total_bytes = 0 total_torrents = 0 try: master = ReplicaSet.get_what_master() what_client = get_what_client(request) for what_id, what_group, what_torrent in what_client.get_free_torrent_ids(): total_bytes += what_torrent['size'] total_torrents += 1 if not WhatTorrent.is_downloaded(request, what_id=what_id): download_locations = DownloadLocation.objects.filter(zone=ReplicaSet.ZONE_WHAT) download_locations = [l for l in download_locations if l.free_space_percent >= MIN_FREE_DISK_SPACE] if len(download_locations) == 0: LogEntry.add(request.user, u'error', u'Unable to update freeleech: not enough space on disk.') return { 'success': False, 'error': u'Not enough free space on disk.' } download_location = choice(download_locations) instance = master.get_preferred_instance() m_torrent = manage_torrent.add_torrent(request, instance, download_location, what_id, True) m_torrent.what_torrent.tags = 'seed' m_torrent.what_torrent.added_by = request.user m_torrent.what_torrent.save() added += 1 LogEntry.add(request.user, u'action', u'Added freeleech {0} to {1} - {2}'.format(m_torrent, m_torrent.instance, download_location.path)) log_type = u'action' if added > 0 else u'info' if added >= FREELEECH_EMAIL_THRESHOLD and socket.gethostname() == FREELEECH_HOSTNAME: send_freeleech_email(u'Added {0} freeleech torrents'.format(added)) time_taken = time.time() - start_time LogEntry.add(request.user, log_type, u'Successfully updated freeleech in {0:.3f}s. {1} added. {2} / {3} torrents total.'.format( time_taken, added, filesizeformat(total_bytes), total_torrents)) except Exception as ex: tb = traceback.format_exc() LogEntry.add(request.user, u'error', u'Error updating freeleech: {0}({1})'.format(type(ex).__name__, unicode(ex)), tb) return { 'success': True, 'added': added }
def sync_instance_db(request, instance): m_torrents = instance.get_m_torrents_by_hash() t_torrents = instance.get_t_torrents_by_hash(TransTorrent.sync_t_arguments) for hash, m_torrent in m_torrents.items(): if hash not in t_torrents: m_torrent_path = m_torrent.path.encode('utf-8') messages = [] with transaction.atomic(): m_torrent.delete() del m_torrents[hash] if instance.replica_set.is_master: if os.path.exists(m_torrent_path): files = os.listdir(m_torrent_path) if any(f for f in files if '.torrent' not in f and 'ReleaseInfo2.txt' != f): messages.append( u'There are other files so leaving in place.') else: messages.append( u'No other files. Deleting directory.') shutil.rmtree(m_torrent_path) else: messages.append(u'Path does not exist.') LogEntry.add( None, u'action', u'Torrent {0} deleted from instance {1}. {2}'.format( m_torrent, instance, ' '.join(messages))) with transaction.atomic(): for hash, t_torrent in t_torrents.items(): if hash not in m_torrents: w_torrent = WhatTorrent.get_or_create(request, info_hash=hash) d_location = DownloadLocation.get_by_full_path( t_torrent.downloadDir) m_torrent = manage_torrent.add_torrent(request, instance, d_location, w_torrent.id, False) m_torrents[m_torrent.info_hash] = m_torrent LogEntry.add( None, u'action', u'Torrent {0} appeared in instance {1}. Added to DB.'. format(m_torrent, instance)) m_torrent = m_torrents[hash] m_torrent.sync_t_torrent(t_torrent) if (SYNC_SYNCS_FILES or 'sync_files' in request.GET) and instance.replica_set.is_master: m_torrent.sync_files()
def add_torrent(request, instance, download_location, what_id, add_to_client=True, moving=False): w_torrent = WhatTorrent.get_or_create(request, what_id=what_id) masters = list(ReplicaSet.get_what_master().transinstance_set.all()) with LockModelTables(TransTorrent, LogEntry): if add_to_client and not moving: try: existing_one = TransTorrent.objects.get( instance__in=masters, info_hash=w_torrent.info_hash) raise TorrentAlreadyAddedException( u'Already added (instance={0}, new_instance={1}, info_hash={2}).' .format(instance, existing_one.instance, w_torrent.info_hash)) except TransTorrent.DoesNotExist: pass if add_to_client: manager = transaction.atomic else: manager = dummy_context_manager with manager(): if True: m_torrent = TransTorrent( instance=instance, location=download_location, what_torrent=w_torrent, info_hash=w_torrent.info_hash, ) m_torrent.save() if add_to_client: download_dir = os.path.join(download_location.path, unicode(w_torrent.id)) t_torrent = instance.client.add_torrent( w_torrent.torrent_file, download_dir=download_dir, paused=False) t_torrent = instance.client.get_torrent( t_torrent.id, arguments=TransTorrent.sync_t_arguments) norm_t_torrent(t_torrent) m_torrent.sync_t_torrent(t_torrent) m_torrent.sync_files() return m_torrent
def do_pop(request): download_location = DownloadLocation.get_what_preferred() if download_location.free_space_percent < MIN_FREE_DISK_SPACE: LogEntry.add(request.user, u'error', u'Failed to add torrent. Not enough disk space.') return { 'success': False, 'error': u'Not enough free space on disk.' } front = QueueItem.get_front() if not front: return { 'success': False, 'message': 'Queue is empty.' } instance = ReplicaSet.get_what_master().get_preferred_instance() if WhatTorrent.is_downloaded(request, what_id=front.what_id): front.delete() return { 'success': True, 'message': 'Already added.' } try: m_torrent = manage_torrent.add_torrent(request, instance, download_location, front.what_id) m_torrent.what_torrent.added_by = request.user m_torrent.what_torrent.tags = 'seed project' m_torrent.what_torrent.save() front.delete() LogEntry.add(request.user, u'action', u'Popped {0} from queue.'.format(m_torrent)) except Exception as ex: tb = traceback.format_exc() LogEntry.add(request.user, u'error', u'Tried popping what_id={0} from queue. Error: {1}'.format(front.what_id, unicode(ex)), tb) return { 'success': False, 'error': unicode(ex), 'traceback': tb } return { 'success': True }
def seed_upload(request, upload_id): qobuz_upload = QobuzUpload.objects.get(id=upload_id) temp_dir = get_temp_dir(qobuz_upload.upload.metadata.id) torrent_path = os.path.join(temp_dir, qobuz_upload.upload.metadata.torrent_name + '.torrent') assert os.path.isfile(wm_str(torrent_path)) info_hash = get_info_hash(torrent_path) what_torrent = WhatTorrent.get_or_create(request, info_hash=info_hash) command = import_external_what_torrent.Command() command.handle(wm_str(temp_dir), wm_str(torrent_path), base_dir=False) try: run_request_transcode(request, what_torrent.id) except Exception: pass qiller = qobuz_upload.upload qiller.state = STATE_DONE qobuz_upload.set_upload(qiller) qobuz_upload.save() return redirect(edit_upload, upload_id)
def handle(self, *args, **options): if not self.check_args(args): print u'Pass the torrent data directory as a first argument, ' \ u'a path to the .torrent file as a second.' return self.data_path, self.torrent_path = [wm_unicode(i) for i in args] with open(wm_str(self.torrent_path), 'rb') as f: self.torrent_info = bencode.bdecode(f.read()) if options['base_dir']: self.data_path = os.path.join(self.data_path, wm_unicode(self.torrent_info['info']['name'])) self.what_torrent = WhatTorrent.get_or_create(self.pseudo_request, info_hash=self.info_hash) if not self.check_files(): return self.move_files() print 'Adding torrent to WM...' manage_torrent.add_torrent(self.pseudo_request, self.trans_instance, self.download_location, self.what_torrent.id) print 'Done!'
def sync_instance_db(request, instance): m_torrents = instance.get_m_torrents_by_hash() t_torrents = instance.get_t_torrents_by_hash(TransTorrent.sync_t_arguments) for hash, m_torrent in m_torrents.items(): if hash not in t_torrents: m_torrent_path = m_torrent.path.encode('utf-8') messages = [] with transaction.atomic(): m_torrent.delete() del m_torrents[hash] if instance.replica_set.is_master: if os.path.exists(m_torrent_path): files = os.listdir(m_torrent_path) if any(f for f in files if '.torrent' not in f and 'ReleaseInfo2.txt' != f): messages.append(u'There are other files so leaving in place.') else: messages.append(u'No other files. Deleting directory.') shutil.rmtree(m_torrent_path) else: messages.append(u'Path does not exist.') LogEntry.add(None, u'action', u'Torrent {0} deleted from instance {1}. {2}' .format(m_torrent, instance, ' '.join(messages))) with transaction.atomic(): for hash, t_torrent in t_torrents.items(): if hash not in m_torrents: w_torrent = WhatTorrent.get_or_create(request, info_hash=hash) d_location = DownloadLocation.get_by_full_path(t_torrent.downloadDir) m_torrent = manage_torrent.add_torrent(request, instance, d_location, w_torrent.id, False) m_torrents[m_torrent.info_hash] = m_torrent LogEntry.add(None, u'action', u'Torrent {0} appeared in instance {1}. Added to DB.' .format(m_torrent, instance)) m_torrent = m_torrents[hash] m_torrent.sync_t_torrent(t_torrent) if (SYNC_SYNCS_FILES or 'sync_files' in request.GET) and instance.replica_set.is_master: m_torrent.sync_files()
def start_seeding(request, upload_id): qobuz_upload = QobuzUpload.objects.get(id=upload_id) dest_upload_dir = DownloadLocation.get_what_preferred().path torrent_file_path = os.path.join(qobuz_upload.temp_media_path, qobuz_upload.torrent_name + u'.torrent') info_hash = get_info_hash(torrent_file_path) what_torrent = WhatTorrent.get_or_create(request, info_hash=info_hash) qobuz_upload.what_torrent = what_torrent qobuz_upload.save() dest_path = os.path.join(dest_upload_dir, str(what_torrent.id)) shutil.rmtree(wm_str(qobuz_upload.spectrals_path)) os.remove(wm_str(torrent_file_path)) try: os.makedirs(wm_str(dest_path)) except OSError: raise Exception('Dest torrent directory already exists.') os.chmod(wm_str(dest_path), 0777) shutil.move(wm_str(qobuz_upload.temp_media_path), wm_str(dest_path)) add_to_wm_transcode(str(what_torrent.id)) return redirect(edit_upload, upload_id)
def add_torrent(request, instance, download_location, what_id, add_to_client=True, moving=False): w_torrent = WhatTorrent.get_or_create(request, what_id=what_id) masters = list(ReplicaSet.get_what_master().transinstance_set.all()) with LockModelTables(TransTorrent, LogEntry): if add_to_client and not moving: try: existing_one = TransTorrent.objects.get(instance__in=masters, info_hash=w_torrent.info_hash) raise TorrentAlreadyAddedException(u'Already added (instance={0}, new_instance={1}, info_hash={2}).'.format( instance, existing_one.instance, w_torrent.info_hash)) except TransTorrent.DoesNotExist: pass if add_to_client: manager = transaction.atomic else: manager = dummy_context_manager with manager(): if True: m_torrent = TransTorrent( instance=instance, location=download_location, what_torrent=w_torrent, info_hash=w_torrent.info_hash, ) m_torrent.save() if add_to_client: download_dir = os.path.join(download_location.path, unicode(w_torrent.id)) t_torrent = instance.client.add_torrent( w_torrent.torrent_file, download_dir=download_dir, paused=False ) t_torrent = instance.client.get_torrent(t_torrent.id, arguments=TransTorrent.sync_t_arguments) norm_t_torrent(t_torrent) m_torrent.sync_t_torrent(t_torrent) m_torrent.sync_files() return m_torrent
def update_freeleech(request): start_time = time.time() added = 0 total_bytes = 0 total_torrents = 0 try: master = ReplicaSet.get_what_master() what_client = get_what_client(request) for what_id, what_group, what_torrent in what_client.get_free_torrent_ids(): total_bytes += what_torrent["size"] total_torrents += 1 if not WhatTorrent.is_downloaded(request, what_id=what_id): freeleech_add_torrent(request, master, what_id) added += 1 log_type = u"action" if added > 0 else u"info" if added >= FREELEECH_EMAIL_THRESHOLD and socket.gethostname() == FREELEECH_HOSTNAME: send_freeleech_email(u"Added {0} freeleech torrents".format(added)) time_taken = time.time() - start_time LogEntry.add( request.user, log_type, u"Successfully updated freeleech in {0:.3f}s. " u"{1} added. {2} / {3} torrents total.".format( time_taken, added, filesizeformat(total_bytes), total_torrents ), ) except Exception as ex: tb = traceback.format_exc() LogEntry.add( request.user, u"error", u"Error updating freeleech: {0}({1})".format(type(ex).__name__, unicode(ex)), tb ) return {"success": True, "added": added}
def request_transcode(request): try: request_what_user = request_get_what_user(request) except Exception: return { 'message': 'You don\'t have permission to add transcode requests.' } try: what_id = int(request.POST['what_id']) except: return { 'message': 'Missing or invalid what id' } try: try: TranscodeRequest.objects.get(what_torrent_id=what_id) return { 'message': 'This has already been requested.' } except TranscodeRequest.DoesNotExist: pass what_client = get_what_client(request) what_torrent = WhatTorrent.get_or_create(request, what_id=what_id) if what_torrent.info_loads['torrent']['format'] != 'FLAC': return { 'message': 'This is not a FLAC torrent. Only FLACs can be transcoded.' } if what_torrent.info_loads['torrent']['reported']: return { 'message': 'You cannot add a request for a torrent that has been reported.' } if what_torrent.info_loads['torrent']['scene']: return { 'message': 'Cannot transcode a scene torrent due to possible release group name in the file names.' } if torrent_is_preemphasized(what_torrent.info_loads): return { 'message': 'This sounds as if it is pre-emphasized. Will not add request.' } group = what_client.request('torrentgroup', id=what_torrent.info_loads['group']['id'])['response'] mp3_ids = get_mp3_ids(group, what_torrent.info_loads) if '320' in mp3_ids and 'V0' in mp3_ids: return { 'message': 'This torrent already has both a V0 and a 320.' } transcode_request = TranscodeRequest( what_torrent=what_torrent, requested_by_ip=request.META['REMOTE_ADDR'], requested_by_what_user=request_what_user, ) transcode_request.save() try: get_trans_torrent(what_torrent) except TransTorrent.DoesNotExist: instance = ReplicaSet.get_what_master().get_preferred_instance() download_location = DownloadLocation.get_what_preferred() m_torrent = add_torrent(request, instance, download_location, what_id) if request.user.is_authenticated(): m_torrent.what_torrent.added_by = request.user else: m_torrent.what_torrent.added_by = None m_torrent.what_torrent.tags = 'transcode' m_torrent.what_torrent.save() if request.user.is_authenticated(): log_user = request.user else: log_user = None LogEntry.add(log_user, u'action', u'Transcode What.CD user {0} added {1} to {2}'.format(request_what_user, m_torrent, m_torrent.instance)) return { 'message': 'Request added.', } except Exception as ex: tb = traceback.format_exc() if request.user.is_authenticated(): current_user = request.user else: current_user = None LogEntry.add(current_user, u'error', u'What user {0} tried adding what_id {1}. Error: {2}'.format(request_what_user, what_id, ex), tb) return { 'message': 'Error adding request: ' + str(ex) }
def run_request_transcode(request, what_id): try: request_what_user = request_get_what_user(request) except Exception: return { 'message': 'You don\'t have permission to add transcode requests.' } try: what_id = int(what_id) except: return {'message': 'Missing or invalid what id'} try: try: TranscodeRequest.objects.get(what_torrent_id=what_id) return {'message': 'This has already been requested.'} except TranscodeRequest.DoesNotExist: pass what_client = get_what_client(request) what_torrent = WhatTorrent.get_or_create(request, what_id=what_id) if what_torrent.info_loads['torrent']['format'] != 'FLAC': return { 'message': 'This is not a FLAC torrent. Only FLACs can be transcoded.' } if what_torrent.info_loads['torrent']['reported']: return { 'message': 'You cannot add a request for a torrent that has been reported.' } if what_torrent.info_loads['torrent']['scene']: return { 'message': 'Cannot transcode a scene torrent due to possible' ' release group name in the file names.' } if torrent_is_preemphasized(what_torrent.info_loads): return { 'message': 'This sounds as if it is pre-emphasized. Will not add request.' } group = what_client.request( 'torrentgroup', id=what_torrent.info_loads['group']['id'])['response'] mp3_ids = get_mp3_ids(group, what_torrent.info_loads) if all(f in mp3_ids for f in TRANSCODER_FORMATS): return { 'message': 'This torrent already has all the formats: {0}.'.format( ', '.join(TRANSCODER_FORMATS)) } transcode_request = TranscodeRequest( what_torrent=what_torrent, requested_by_ip=request.META['REMOTE_ADDR'], requested_by_what_user=request_what_user, ) transcode_request.save() try: get_trans_torrent(what_torrent) except TransTorrent.DoesNotExist: instance = ReplicaSet.get_what_master().get_preferred_instance() download_location = DownloadLocation.get_what_preferred() m_torrent = add_torrent(request, instance, download_location, what_id) if request.user.is_authenticated(): m_torrent.what_torrent.added_by = request.user else: m_torrent.what_torrent.added_by = None m_torrent.what_torrent.tags = 'transcode' m_torrent.what_torrent.save() if request.user.is_authenticated(): log_user = request.user else: log_user = None LogEntry.add( log_user, u'action', u'Transcode What.CD user {0} added {1} to {2}'.format( request_what_user, m_torrent, m_torrent.instance)) return { 'message': 'Request added.', } except Exception as ex: tb = traceback.format_exc() if request.user.is_authenticated(): current_user = request.user else: current_user = None LogEntry.add( current_user, u'error', u'What user {0} tried adding what_id {1}. Error: {2}'.format( request_what_user, what_id, ex), tb) return {'message': 'Error adding request: ' + str(ex)}
def is_existing(what_id): try: QueueItem.objects.get(what_id=what_id) return True except QueueItem.DoesNotExist: return WhatTorrent.is_downloaded(None, what_id=what_id)
def handle(self, *args, **options): if not self.check_args(args): print u'Pass the directory containing your torrent directories from a previous WM' \ u' install. Subfolders of this directory should be named by torrent ID. After' \ u' import, all errored torrent/data sets will be organized into subfolders for' \ u' manual inspection/import.' return self.wm_media = wm_unicode(args[0]) self.error_move = not options['no_move'] for self.torrent_id in next(os.walk(self.wm_media))[1]: try: # Is this actually a directory? if not os.path.isdir(self.base_dir()): print u'"{}" is not a valid directory. Skipping..'.format(self.base_dir()) continue # Get all torrents torrents = [] hashes = [] for p in os.listdir(self.base_dir()): if p.endswith('.torrent') and not p.startswith('._'): try: p = os.path.join(self.base_dir(), wm_unicode(p)) hashes.append(get_info_hash(p)) torrents.append(p) except IOError: print('Warning: Invalid torrent found in {}'.format(self.torrent_id)) continue except BTFailure as e: print('Warning: {}. Invalid torrent found in {}'.format(str(e), self.torrent_id)) continue # Are there any valid torrents? if len(torrents) == 0: if self.torrent_id.isdigit(): print u'Error: No valid torrent files found in "{}".'.format(self.base_dir()) self.subfolder_move('no_torrents', self.torrent_id) continue # Are there multiple unique torrents? if len(set(hashes)) > 1: print u'Error: Multiple unique torrents found' self.subfolder_move('multiple_torrent', self.torrent_id) continue except UnicodeDecodeError as e: print u'UnicodeDecodeError: Please import manually. Skipping..' continue with open(wm_str(torrents[0]), 'rb') as f: try: self.torrent_info = bencode.bdecode(f.read()) self.info_hash = get_info_hash(torrents[0]) except: print u'Error: Invalid torrent file.' self.subfolder_move('invalid_torrent', self.torrent_id) continue self.data_path = os.path.join(self.base_dir(), wm_unicode(self.torrent_info['info']['name'])) print u'Checking to see if torrent is already loaded into WM..' masters = list(ReplicaSet.get_what_master().transinstance_set.all()) try: TransTorrent.objects.get(instance__in=masters, info_hash=self.info_hash) print u'Error: Torrent already added to WM.' self.subfolder_move('already_added', self.torrent_id) continue except TransTorrent.DoesNotExist: pass try: self.what_torrent = WhatTorrent.get_or_create(self.pseudo_request, info_hash=self.info_hash) except RequestException as e: if 'bad hash' in str(e): print u'Error: Bad hash. Torrent may have been trumped/deleted.'.format(str(e)) self.subfolder_move('bad_hash', self.torrent_id) continue else: raise e except OperationalError as e: if 'MySQL' in str(e): print u'Error: {}. Please check {} manually.'.format(str(e), self.torrent_id) self.subfolder_move('mysql_error', self.torrent_id) continue else: raise e if not self.check_files(): print u'Error: File check failed.' try: self.subfolder_move('file_check_fail', self.torrent_id) except UnicodeDecodeError as e: print u'UnicodeDecodeError. Move failed. Please manually check {} Skipping..'.format(self.torrent_id) continue self.move_files() print u'Adding torrent to WM...' self.trans_instance = ReplicaSet.get_what_master().get_preferred_instance() manage_torrent.add_torrent(self.pseudo_request, self.trans_instance, self.download_location, self.what_torrent.id) print u'Done!'
def add_torrent(request): if not request.user.has_perm('home.add_whattorrent'): return { 'success': False, 'error': 'You don\'t have permission to add torrents. Talk to the administrator.' } if 'dir' in request.POST: download_location = DownloadLocation.objects.get( zone=ReplicaSet.ZONE_WHAT, path=request.POST['dir'] ) else: download_location = DownloadLocation.get_what_preferred() if download_location.free_space_percent < MIN_FREE_DISK_SPACE: LogEntry.add(request.user, u'error', u'Failed to add torrent. Not enough disk space.') return { 'success': False, 'error': u'Not enough free space on disk.' } try: what_id = int(request.POST['id']) except ValueError: return { 'success': False, 'error': u'Invalid id' } instance = ReplicaSet.get_what_master().get_preferred_instance() try: if WhatTorrent.is_downloaded(request, what_id=what_id): m_torrent = TransTorrent.objects.filter(what_torrent_id=what_id)[0] raise TorrentAlreadyAddedException() m_torrent = manage_torrent.add_torrent(request, instance, download_location, what_id, True) m_torrent.what_torrent.added_by = request.user m_torrent.what_torrent.save() except TorrentAlreadyAddedException: LogEntry.add(request.user, u'info', u'Tried adding what_id={0}, already added.'.format(what_id)) what_torrent = WhatTorrent.get_or_none(request, what_id=what_id) return { 'success': False, 'error_code': u'already_added', 'error': u'Already added.', 'torrent_id': m_torrent.what_torrent_id, 'artist': what_torrent.info_artist if what_torrent else '<<< Unable to find torrent >>>', 'title': what_torrent.info_title if what_torrent else '<<< Unable to find torrent >>>', } except Exception as ex: tb = traceback.format_exc() LogEntry.add(request.user, u'error', u'Tried adding what_id={0}. Error: {1}'.format(what_id, unicode(ex)), tb) return { 'success': False, 'error': unicode(ex), 'traceback': tb, } tags = request.POST.get('tags') if tags: m_torrent.what_torrent.tags = tags m_torrent.what_torrent.save() LogEntry.add(request.user, u'action', u'Added {0} to {1}'.format(m_torrent, m_torrent.instance)) return { 'success': True, 'artist': m_torrent.what_torrent.info_artist, 'title': m_torrent.what_torrent.info_title, }
def find_dupes(self): response = None existing_torrent_id = None t_info = self.what_torrent_info['torrent'] g_info = self.what_torrent_info['group'] remaster = t_info['remastered'] print 'What id: ', self.what_torrent['id'] print 'Title: ', '; '.join( a['name'] for a in g_info['musicInfo'] ['artists']), '-', html_parser.unescape(g_info['name']) print 'Year: ', g_info['year'] print 'Media: ', t_info['media'] print 'Format: ', t_info['format'] print 'Bitrate: ', t_info['encoding'] print 'Remaster: ', 'yes ({})'.format( t_info['remasterYear']) if remaster else 'no' print 'Label: ', t_info[ 'remasterRecordLabel'] if remaster else g_info['recordLabel'] print 'Cat no: ', t_info[ 'remasterCatalogueNumber'] if remaster else g_info[ 'catalogueNumber'] print 'Remaster desc:', t_info['remasterTitle'] print 'Torrent name: ', self.torrent_name print 'Torrent size: ', format_bytes_pth(self.get_total_size()) print self.find_existing_torrent_by_hash() if self.new_torrent: print 'Found existing torrent by hash ' + str( self.new_torrent['torrent']['id']) + ' reseeding!!!' self.migration_status = WhatTorrentMigrationStatus.objects.create( what_torrent_id=self.what_torrent['id'], status=WhatTorrentMigrationStatus.STATUS_RESEEDED, pth_torrent_id=self.new_torrent['torrent']['id'], ) return True self.find_existing_torrent_group() if self.existing_new_group: matching_torrent_id = self.find_matching_torrent_within_group() if matching_torrent_id: print 'Found matching torrent id:', matching_torrent_id response = 'reseed' else: existing_torrent_id = self.find_existing_torrent_within_group() if existing_torrent_id: print 'Found existing torrent id:', existing_torrent_id response = 'dup' if not response: response = raw_input( 'Choose action [up/dup/skip/skipp/reseed/changetg]: ') else: if response != 'reseed': new_response = raw_input(response + '. Override: ') if new_response: response = new_response if response == 'up': self.migration_status = WhatTorrentMigrationStatus( what_torrent_id=self.what_torrent['id'], status=WhatTorrentMigrationStatus.STATUS_PROCESSING, ) return True elif response == 'reseed': if not matching_torrent_id: matching_torrent_id = int( raw_input('Enter matching torrent id: ')) existing_torrent = WhatTorrent.get_or_create( dummy_request, what_id=matching_torrent_id) existing_info = bencode.bdecode( existing_torrent.torrent_file_binary) success = False try: if not torrentcheck.verify(existing_info['info'], self.full_location): raise Exception('Torrent does not verify') success = True except Exception as ex: print 'Existing torrent does not verify with', ex if success: self.new_torrent = self.what.request( 'torrent', id=matching_torrent_id)['response'] self.migration_status = WhatTorrentMigrationStatus.objects.create( what_torrent_id=self.what_torrent['id'], status=WhatTorrentMigrationStatus.STATUS_RESEEDED, pth_torrent_id=matching_torrent_id, ) return True self.migration_status = WhatTorrentMigrationStatus( what_torrent_id=self.what_torrent['id'], ) if response == 'dup': if not existing_torrent_id: existing_torrent_id = int( raw_input('Enter existing torrent id: ')) existing_torrent = self.what.request( 'torrent', id=existing_torrent_id)['response'] self.migration_status.status = WhatTorrentMigrationStatus.STATUS_DUPLICATE self.migration_status.pth_torrent_id = existing_torrent_id TorrentGroupMapping.objects.get_or_create( what_group_id=self.what_torrent_info['group']['id'], pth_group_id=existing_torrent['group']['id'], ) elif response == 'skip': self.migration_status.status = WhatTorrentMigrationStatus.STATUS_SKIPPED elif response == 'skipp': self.migration_status.status = WhatTorrentMigrationStatus.STATUS_SKIPPED_PERMANENTLY elif response == 'reseed': self.migration_status.status = WhatTorrentMigrationStatus.STATUS_DUPLICATE self.migration_status.pth_torrent_id = matching_torrent_id elif response == 'changetg': existing_group_id = raw_input( u'Enter existing group id (empty if non-existent): ') if existing_group_id: self.existing_new_group = self.existing_new_group = self.what.request( 'torrentgroup', id=existing_group_id)['response'] return self.find_dupes() else: raise Exception('Unknown response') self.migration_status.save() return False
def add_torrent(request): if not request.user.has_perm('home.add_whattorrent'): return { 'success': False, 'error': 'You don\'t have permission to add torrents. Talk to the administrator.', } try: if 'dir' in request.POST: download_location = DownloadLocation.objects.get( zone=ReplicaSet.ZONE_WHAT, path=request.POST['dir'] ) else: download_location = DownloadLocation.get_what_preferred() except DownloadLocation.DoesNotExist: return { 'success': False, 'error': u'Download location does not exist.', } if download_location.free_space_percent < MIN_FREE_DISK_SPACE: LogEntry.add(request.user, u'error', u'Failed to add torrent. Not enough disk space.') return { 'success': False, 'error': u'Not enough free space on disk.', } try: what_id = int(request.POST['id']) except (ValueError, MultiValueDictKeyError): return { 'success': False, 'error': u'Invalid id', } instance = ReplicaSet.get_what_master().get_preferred_instance() try: if WhatTorrent.is_downloaded(request, what_id=what_id): m_torrent = TransTorrent.objects.filter(what_torrent_id=what_id)[0] raise TorrentAlreadyAddedException() m_torrent = manage_torrent.add_torrent(request, instance, download_location, what_id, True) m_torrent.what_torrent.added_by = request.user m_torrent.what_torrent.save() except TorrentAlreadyAddedException: LogEntry.add(request.user, u'info', u'Tried adding what_id={0}, already added.'.format(what_id)) what_torrent = WhatTorrent.get_or_none(request, what_id=what_id) result = { 'success': False, 'error_code': u'already_added', 'error': u'Already added.', 'torrent_id': m_torrent.what_torrent_id, } if m_torrent.what_torrent.info_category_id == 1: result['artist'] = (what_torrent.info_artist if what_torrent else '<<< Unable to find torrent >>>') result['title'] = (what_torrent.info_title if what_torrent else '<<< Unable to find torrent >>>') return result except Exception as ex: tb = traceback.format_exc() LogEntry.add(request.user, u'error', u'Tried adding what_id={0}. Error: {1}'.format(what_id, unicode(ex)), tb) return { 'success': False, 'error': unicode(ex), 'traceback': tb, } tags = request.POST.get('tags') if tags: m_torrent.what_torrent.tags = tags m_torrent.what_torrent.save() LogEntry.add(request.user, u'action', u'Added {0} to {1}'.format(m_torrent, m_torrent.instance)) result = { 'success': True, } if m_torrent.what_torrent.info_category_id == 1: result['artist'] = m_torrent.what_torrent.info_artist, result['title'] = m_torrent.what_torrent.info_title, return result
def handle(self, *args, **options): if not self.check_args(args): print u'Pass the directory containing your torrent directories from a previous WM' \ u' install. Subfolders of this directory should be named by torrent ID. After' \ u' import, all errored torrent/data sets will be organized into subfolders for' \ u' manual inspection/import.' return self.wm_media = wm_unicode(args[0]) self.error_move = not options['no_move'] for self.torrent_id in next(os.walk(self.wm_media))[1]: try: # Is this actually a directory? if not os.path.isdir(self.base_dir()): print u'"{}" is not a valid directory. Skipping..'.format( self.base_dir()) continue # Get all torrents torrents = [] hashes = [] for p in os.listdir(self.base_dir()): if p.endswith('.torrent') and not p.startswith('._'): try: p = os.path.join(self.base_dir(), wm_unicode(p)) hashes.append(get_info_hash(p)) torrents.append(p) except IOError: print( 'Warning: Invalid torrent found in {}'.format( self.torrent_id)) continue except BTFailure as e: print('Warning: {}. Invalid torrent found in {}'. format(str(e), self.torrent_id)) continue # Are there any valid torrents? if len(torrents) == 0: if self.torrent_id.isdigit(): print u'Error: No valid torrent files found in "{}".'.format( self.base_dir()) self.subfolder_move('no_torrents', self.torrent_id) continue # Are there multiple unique torrents? if len(set(hashes)) > 1: print u'Error: Multiple unique torrents found' self.subfolder_move('multiple_torrent', self.torrent_id) continue except UnicodeDecodeError as e: print u'UnicodeDecodeError: Please import manually. Skipping..' continue with open(wm_str(torrents[0]), 'rb') as f: try: self.torrent_info = bencode.bdecode(f.read()) self.info_hash = get_info_hash(torrents[0]) except: print u'Error: Invalid torrent file.' self.subfolder_move('invalid_torrent', self.torrent_id) continue self.data_path = os.path.join( self.base_dir(), wm_unicode(self.torrent_info['info']['name'])) print u'Checking to see if torrent is already loaded into WM..' masters = list( ReplicaSet.get_what_master().transinstance_set.all()) try: TransTorrent.objects.get(instance__in=masters, info_hash=self.info_hash) print u'Error: Torrent already added to WM.' self.subfolder_move('already_added', self.torrent_id) continue except TransTorrent.DoesNotExist: pass try: self.what_torrent = WhatTorrent.get_or_create( self.pseudo_request, info_hash=self.info_hash) except RequestException as e: if 'bad hash' in str(e): print u'Error: Bad hash. Torrent may have been trumped/deleted.'.format( str(e)) self.subfolder_move('bad_hash', self.torrent_id) continue else: raise e except OperationalError as e: if 'MySQL' in str(e): print u'Error: {}. Please check {} manually.'.format( str(e), self.torrent_id) self.subfolder_move('mysql_error', self.torrent_id) continue else: raise e if not self.check_files(): print u'Error: File check failed.' try: self.subfolder_move('file_check_fail', self.torrent_id) except UnicodeDecodeError as e: print u'UnicodeDecodeError. Move failed. Please manually check {} Skipping..'.format( self.torrent_id) continue self.move_files() print u'Adding torrent to WM...' self.trans_instance = ReplicaSet.get_what_master( ).get_preferred_instance() manage_torrent.add_torrent(self.pseudo_request, self.trans_instance, self.download_location, self.what_torrent.id) print u'Done!'
def find_dupes(self): response = None existing_torrent_id = None t_info = self.what_torrent_info['torrent'] g_info = self.what_torrent_info['group'] remaster = t_info['remastered'] print 'What id: ', self.what_torrent['id'] print 'Title: ', '; '.join( a['name'] for a in g_info['musicInfo']['artists']), '-', html_parser.unescape( g_info['name']) print 'Year: ', g_info['year'] print 'Media: ', t_info['media'] print 'Format: ', t_info['format'] print 'Bitrate: ', t_info['encoding'] print 'Remaster: ', 'yes ({})'.format(t_info['remasterYear']) if remaster else 'no' print 'Label: ', t_info['remasterRecordLabel'] if remaster else g_info['recordLabel'] print 'Cat no: ', t_info['remasterCatalogueNumber'] if remaster else g_info[ 'catalogueNumber'] print 'Remaster desc:', t_info['remasterTitle'] print 'Torrent name: ', self.torrent_name print 'Torrent size: ', format_bytes_pth(self.get_total_size()) print self.find_existing_torrent_by_hash() if self.new_torrent: print 'Found existing torrent by hash ' + str( self.new_torrent['torrent']['id']) + ' reseeding!!!' self.migration_status = WhatTorrentMigrationStatus.objects.create( what_torrent_id=self.what_torrent['id'], status=WhatTorrentMigrationStatus.STATUS_RESEEDED, pth_torrent_id=self.new_torrent['torrent']['id'], ) return True self.find_existing_torrent_group() if self.existing_new_group: matching_torrent_id = self.find_matching_torrent_within_group() if matching_torrent_id: print 'Found matching torrent id:', matching_torrent_id response = 'reseed' else: existing_torrent_id = self.find_existing_torrent_within_group() if existing_torrent_id: print 'Found existing torrent id:', existing_torrent_id response = 'dup' if not response: response = raw_input('Choose action [up/dup/skip/skipp/reseed/changetg]: ') else: if response != 'reseed': new_response = raw_input(response + '. Override: ') if new_response: response = new_response if response == 'up': self.migration_status = WhatTorrentMigrationStatus( what_torrent_id=self.what_torrent['id'], status=WhatTorrentMigrationStatus.STATUS_PROCESSING, ) return True elif response == 'reseed': if not matching_torrent_id: matching_torrent_id = int(raw_input('Enter matching torrent id: ')) existing_torrent = WhatTorrent.get_or_create(dummy_request, what_id=matching_torrent_id) existing_info = bencode.bdecode(existing_torrent.torrent_file_binary) success = False try: if not torrentcheck.verify(existing_info['info'], self.full_location): raise Exception('Torrent does not verify') success = True except Exception as ex: print 'Existing torrent does not verify with', ex if success: self.new_torrent = self.what.request('torrent', id=matching_torrent_id)['response'] self.migration_status = WhatTorrentMigrationStatus.objects.create( what_torrent_id=self.what_torrent['id'], status=WhatTorrentMigrationStatus.STATUS_RESEEDED, pth_torrent_id=matching_torrent_id, ) return True self.migration_status = WhatTorrentMigrationStatus( what_torrent_id=self.what_torrent['id'], ) if response == 'dup': if not existing_torrent_id: existing_torrent_id = int(raw_input('Enter existing torrent id: ')) existing_torrent = self.what.request('torrent', id=existing_torrent_id)['response'] self.migration_status.status = WhatTorrentMigrationStatus.STATUS_DUPLICATE self.migration_status.pth_torrent_id = existing_torrent_id TorrentGroupMapping.objects.get_or_create( what_group_id=self.what_torrent_info['group']['id'], pth_group_id=existing_torrent['group']['id'], ) elif response == 'skip': self.migration_status.status = WhatTorrentMigrationStatus.STATUS_SKIPPED elif response == 'skipp': self.migration_status.status = WhatTorrentMigrationStatus.STATUS_SKIPPED_PERMANENTLY elif response == 'reseed': self.migration_status.status = WhatTorrentMigrationStatus.STATUS_DUPLICATE self.migration_status.pth_torrent_id = matching_torrent_id elif response == 'changetg': existing_group_id = raw_input(u'Enter existing group id (empty if non-existent): ') if existing_group_id: self.existing_new_group = self.existing_new_group = self.what.request( 'torrentgroup', id=existing_group_id)['response'] return self.find_dupes() else: raise Exception('Unknown response') self.migration_status.save() return False