def check_valid(self):
     print 'Verifying torrent data...'
     try:
         if not torrentcheck.verify(self.torrent_dict['info'], self.full_location):
             raise Exception('Torrent does not verify')
     except Exception as ex:
         WhatTorrentMigrationStatus.objects.create(
             what_torrent_id=self.what_torrent['id'],
             status=WhatTorrentMigrationStatus.STATUS_FAILED_VALIDATION
         )
         raw_input('Verification threw {}. Press enter to continue.'.format(ex))
         return False
     print('Hash matching')
     torrent_file_set = {'/'.join(f['path']) for f in self.torrent_dict['info']['files']}
     for dirpath, dirnames, filenames in os.walk(self.torrent_dir_path):
         for filename in filenames:
             abs_path = os.path.join(dirpath, filename)
             file_path = os.path.relpath(abs_path, self.torrent_dir_path)
             if file_path not in torrent_file_set:
                 raise Exception(
                     'Extraneous file: {}/{}'.format(self.torrent_dir_path, file_path))
             if filename.lower().endswith('.log'):
                 print 'Candidate log file', abs_path
                 with open(abs_path, 'r') as log_f:
                     try:
                         self.log_files.add(LogFile(log_f.read()))
                     except UnrecognizedRippingLogException:
                         print 'Skipping: unrecognized'
                         pass
                     except InvalidRippingLogException:
                         raw_input('Log file unrecognized!')
                 self.log_files_full_paths.append(abs_path)
     print('No extraneous files')
     print 'Torrent verification complete'
     return True
 def check_valid(self):
     print 'Verifying torrent data...'
     try:
         if not torrentcheck.verify(self.torrent_dict['info'],
                                    self.full_location):
             raise Exception('Torrent does not verify')
     except Exception as ex:
         WhatTorrentMigrationStatus.objects.create(
             what_torrent_id=self.what_torrent['id'],
             status=WhatTorrentMigrationStatus.STATUS_FAILED_VALIDATION)
         raw_input(
             'Verification threw {}. Press enter to continue.'.format(ex))
         return False
     print('Hash matching')
     torrent_file_set = {
         '/'.join(f['path'])
         for f in self.torrent_dict['info']['files']
     }
     for dirpath, dirnames, filenames in os.walk(self.torrent_dir_path):
         for filename in filenames:
             abs_path = os.path.join(dirpath, filename)
             file_path = os.path.relpath(abs_path, self.torrent_dir_path)
             if file_path not in torrent_file_set:
                 raise Exception('Extraneous file: {}/{}'.format(
                     self.torrent_dir_path, file_path))
             if filename.lower().endswith('.log'):
                 print 'Candidate log file', abs_path
                 with open(abs_path, 'r') as log_f:
                     try:
                         self.log_files.add(LogFile(log_f.read()))
                     except UnrecognizedRippingLogException:
                         print 'Skipping: unrecognized'
                         pass
                     except InvalidRippingLogException:
                         raw_input('Log file unrecognized!')
                 self.log_files_full_paths.append(abs_path)
     print('No extraneous files')
     print 'Torrent verification complete'
     return True
    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 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