def sync_replica_set(master, slave): master_m_torrents = {} slave_m_torrents = {} for master_instance in master.transinstance_set.all(): for hash, m_torrent in master_instance.get_m_torrents_by_hash().items( ): if m_torrent.torrent_done == 1: master_m_torrents[hash] = m_torrent for slave_instance in slave.transinstance_set.all(): slave_m_torrents.update(slave_instance.get_m_torrents_by_hash()) for hash, m_torrent in master_m_torrents.items(): if hash not in slave_m_torrents: instance = slave.get_preferred_instance() manage_torrent.add_torrent(None, instance, m_torrent.location, m_torrent.what_torrent_id, True) LogEntry.add( None, u'info', u'Torrent {0} added to {1} during replication.'.format( m_torrent, instance)) for hash, m_torrent in slave_m_torrents.items(): if hash not in master_m_torrents: manage_torrent.remove_torrent(m_torrent) LogEntry.add( None, u'info', u'Torrent {0} removed from {1} during replication'.format( m_torrent, m_torrent.instance))
def download_torrent_group(request, group_id): 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: torrent_group = WhatTorrentGroup.objects.get(id=group_id) except WhatTorrentGroup.DoesNotExist: torrent_group = WhatTorrentGroup.update_from_what(get_what_client(request), group_id) if torrent_group.torrents_json is None: torrent_group = WhatTorrentGroup.update_from_what(get_what_client(request), group_id) ids = get_ids_to_download(torrent_group) try: instance = ReplicaSet.get_what_master().get_preferred_instance() download_location = DownloadLocation.get_what_preferred() for torrent_id in ids: add_torrent(request, instance, download_location, torrent_id) except Exception as ex: return { 'success': False, 'error': unicode(ex), 'traceback': traceback.format_exc(), } return { 'success': True, 'added': len(ids), }
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 move_to_dest_add(request, book_upload): location = DownloadLocation.get_what_preferred() dest_path = os.path.join(location.path, str(book_upload.what_torrent_id)) book_path = os.path.join(dest_path, book_upload.target_filename) if not os.path.exists(dest_path): os.mkdir(dest_path) os.chmod(dest_path, 0777) shutil.copyfile(book_upload.book_data.storage.path(book_upload.book_data), book_path) os.chmod(book_path, 0777) manage_torrent.add_torrent( request, ReplicaSet.get_what_master().get_preferred_instance(), location, book_upload.what_torrent_id)
def freeleech_add_torrent(request, master, what_id, retry=3): 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: raise Exception(u"Unable to update freeleech: not enough space on disk.") download_location = choice(download_locations) instance = master.get_preferred_instance() try: 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() LogEntry.add( request.user, u"action", u"Added freeleech {0} to {1} - {2}".format(m_torrent, m_torrent.instance, download_location.path), ) except TorrentAlreadyAddedException: pass except Exception as ex: LogEntry.add(request.user, u"error", u"Error adding freeleech torrent {0}".format(what_id)) if retry > 0: time.sleep(3) freeleech_add_torrent(request, master, what_id, retry - 1) else: raise ex
def freeleech_add_torrent(request, master, what_id, retry=3): 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: raise Exception(u'Unable to update freeleech: not enough space on disk.') download_location = choice(download_locations) instance = master.get_preferred_instance() try: 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() LogEntry.add(request.user, u'action', u'Added freeleech {0} to {1} - {2}'.format( m_torrent, m_torrent.instance, download_location.path)) except TorrentAlreadyAddedException: pass except Exception as ex: LogEntry.add(request.user, u'error', u'Error adding freeleech torrent {0}'.format(what_id)) if retry > 0: time.sleep(3) freeleech_add_torrent(request, master, what_id, retry - 1) else: raise ex
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 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_replica_set(master, slave): master_m_torrents = {} slave_m_torrents = {} for master_instance in master.transinstance_set.all(): for hash, m_torrent in master_instance.get_m_torrents_by_hash().items(): if m_torrent.torrent_done == 1: master_m_torrents[hash] = m_torrent for slave_instance in slave.transinstance_set.all(): slave_m_torrents.update(slave_instance.get_m_torrents_by_hash()) for hash, m_torrent in master_m_torrents.items(): if not hash in slave_m_torrents: instance = slave.get_preferred_instance() manage_torrent.add_torrent(None, instance, m_torrent.location, m_torrent.what_torrent_id, True) LogEntry.add(None, u'info', u'Torrent {0} added to {1} during replication.'.format(m_torrent, instance)) for hash, m_torrent in slave_m_torrents.items(): if not hash in master_m_torrents: manage_torrent.remove_torrent(m_torrent) LogEntry.add(None, u'info', u'Torrent {0} removed from {1} during replication'.format(m_torrent, m_torrent.instance))
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 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 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): 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 _add_to_wm(self): new_id = self.new_torrent['torrent']['id'] instance = ReplicaSet.get_what_master().get_preferred_instance() trans_torrent = add_torrent(dummy_request, instance, self.new_location_obj, new_id) print 'Added to', trans_torrent.instance.name
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 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 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 _add_to_wm(self): new_id = self.new_torrent['torrent']['id'] instance = ReplicaSet.get_what_master().get_preferred_instance() trans_torrent = add_torrent(dummy_request, instance, self.new_location_obj, new_id) print 'Added to', trans_torrent.instance.name
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 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!'