Пример #1
0
 def create_torrent(self):
     print 'Creating .torrent file...'
     args = [
         '-a', WHAT_ANNOUNCE,
         '-p',
         '-o', self.torrent_file_path,
         self.torrent_temp_dir,
     ]
     if call(['mktorrent'] + args) != 0:
         raise Exception('mktorrent returned non-zero')
     self.new_torrent_info_hash = get_info_hash(self.torrent_file_path)
 def check_args(self, args):
     if len(args) != 2:
         return False
     if not os.path.isdir(args[0]):
         return False
     if not os.path.isfile(args[1]):
         return False
     try:
         self.info_hash = get_info_hash(args[1])
     except Exception:
         print u'Invalid .torrent file.'
         return False
     return True
Пример #3
0
 def create_torrent(self):
     print 'Creating .torrent file...'
     args = [
         '-a', WHAT_ANNOUNCE,
         '-p',
         '-o', self.torrent_file_path,
         self.torrent_temp_dir,
     ]
     if call(['mktorrent'] + args) != 0:
         raise Exception('mktorrent returned non-zero')
     with open(self.torrent_file_path, 'rb') as f:
         torrent_data = f.read()
     torrent_data = pthify_torrent(torrent_data)
     with open(self.torrent_file_path, 'wb') as f:
         f.write(torrent_data)
     self.new_torrent_info_hash = get_info_hash(self.torrent_file_path)
Пример #4
0
 def create_torrent(self):
     print 'Creating .torrent file...'
     args = [
         '-a',
         WHAT_ANNOUNCE,
         '-p',
         '-o',
         self.torrent_file_path,
         self.torrent_temp_dir,
     ]
     if call(['mktorrent'] + args) != 0:
         raise Exception('mktorrent returned non-zero')
     with open(self.torrent_file_path, 'rb') as f:
         torrent_data = f.read()
     torrent_data = pthify_torrent(torrent_data)
     with open(self.torrent_file_path, 'wb') as f:
         f.write(torrent_data)
     self.new_torrent_info_hash = get_info_hash(self.torrent_file_path)
Пример #5
0
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)
Пример #6
0
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)
Пример #7
0
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)
Пример #8
0
 def create_torrent(self):
     print "Creating .torrent file..."
     args = ["-a", WHAT_ANNOUNCE, "-p", "-o", self.torrent_file_path, self.torrent_temp_dir]
     if call(["mktorrent"] + args) != 0:
         raise Exception("mktorrent returned non-zero")
     self.new_torrent_info_hash = get_info_hash(self.torrent_file_path)
    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 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!'