Ejemplo n.º 1
0
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),
    }
Ejemplo n.º 2
0
def add_artist(request, artist_name):
    what_client = get_what_client(request)
    response = what_client.request('artist', artistname=artist_name)['response']
    added = 0
    for group in response['torrentgroup']:
        if filter_group(response['name'], group):
            artist = html_unescape(response['name'])
            title = html_unescape(group['groupName'])
            release_type = group['releaseType']
            for torrent in group['torrent']:
                id = torrent['id']
                priority = filter_torrent(group, torrent)
                if priority and not is_existing(id):
                    format = torrent['format']
                    encoding = torrent['encoding']
                    torrent_size = torrent['size']
                    queue_item = QueueItem(
                        what_id=id,
                        priority=priority,
                        artist=artist,
                        title=title,
                        release_type=release_type,
                        format=format,
                        encoding=encoding,
                        torrent_size=torrent_size
                    )
                    queue_item.save()
                    added += 1
    return {
        'success': True,
        'added': added
    }
Ejemplo n.º 3
0
def top10_torrent_groups(request):
    count = request.GET.get('count', 10)
    what_client = get_what_client(request)
    top10 = what_client.request('top10', limit=100)['response']
    group_set = set()
    results = []
    for torrent in top10[0]['results']:
        if torrent['groupId'] in group_set:
            continue
        if torrent['groupCategory'] != 1:
            continue
        group_set.add(torrent['groupId'])
        results.append({
            'id': torrent['groupId'],
            'joined_artists': html_unescape(torrent['artist']),
            'name': html_unescape(torrent['groupName']),
            'year': torrent['groupYear'],
            'wiki_image': get_image_cache_url(torrent['wikiImage']),
        })
        if len(results) == count:
            break
    torrents_have = get_torrent_groups_have(t['id'] for t in results)
    for result in results:
        result.update(torrents_have[result['id']])
    return results
Ejemplo n.º 4
0
 def handle(self, *args, **options):
     what = get_what_client(lambda: None, True)
     with open('what_manager2_torrents.jsonl', 'rb') as torrents_input:
         for line in torrents_input:
             data = ujson.loads(line)
             info = ujson.loads(data['what_torrent']['info'])
             what_torrent_id = info['torrent']['id']
             what_group_id = info['group']['id']
             try:
                 TorrentGroupMapping.objects.get(what_group_id=what_group_id)
                 continue
             except TorrentGroupMapping.DoesNotExist:
                 pass
             try:
                 migration_status = WhatTorrentMigrationStatus.objects.get(
                     what_torrent_id=what_torrent_id)
             except WhatTorrentMigrationStatus.DoesNotExist:
                 continue
             if migration_status.status != WhatTorrentMigrationStatus.STATUS_COMPLETE:
                 continue
             pth_torrent_id = migration_status.pth_torrent_id
             if not pth_torrent_id:
                 continue
             try:
                 pth_torrent = what.request('torrent', id=pth_torrent_id)['response']
             except BadIdException:
                 continue
             pth_group_id = pth_torrent['group']['id']
             print 'Saving {} mapping to {}'.format(what_group_id, pth_group_id)
             TorrentGroupMapping.objects.create(
                 what_group_id=what_group_id,
                 pth_group_id=pth_group_id,
             )
Ejemplo n.º 5
0
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
    }
Ejemplo n.º 6
0
def sync_profile(request):
    user_id = settings.WHAT_USER_ID
    interval = settings.WHAT_PROFILE_SNAPSHOT_INTERVAL
    snapshots = WhatUserSnapshot.objects.order_by('-datetime')[:1]
    if len(snapshots) == 0 or (timezone.now() - snapshots[0].datetime).total_seconds() >= interval - 30:
        what = get_what_client(request)
        WhatUserSnapshot.get(what, user_id).save()
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def what_proxy(request):
    get = dict(request.GET.lists())
    action = get['action']
    del get['action']
    if 'auth' in get:
        del get['auth']
    what = get_what_client(request)
    response = what.request(action, **get)
    return response
Ejemplo n.º 9
0
def what_proxy(request):
    get = dict(request.GET.lists())
    action = get["action"]
    del get["action"]
    if "auth" in get:
        del get["auth"]
    what = get_what_client(request)
    response = what.request(action, **get)
    return response
Ejemplo n.º 10
0
def what_proxy(request):
    get = dict(request.GET.lists())
    action = get['action']
    del get['action']
    if 'auth' in get:
        del get['auth']
    what = get_what_client(request)
    response = what.request(action, **get)
    return response
Ejemplo n.º 11
0
 def handle(self, *args, **options):
     what_client = get_what_client(lambda: None)
     while True:
         response = what_json.utils.refresh_whattorrent(what_client)
         print response
         if response['success']:
             time.sleep(5)
         else:
             time.sleep(15)
Ejemplo n.º 12
0
def get_artist(request, artist_id):
    try:
        if 'HTTP_X_REFRESH' in request.META:
            raise WhatArtist.DoesNotExist()
        artist = WhatArtist.objects.get(id=artist_id)
        if artist.is_shell:
            raise WhatArtist.DoesNotExist()
    except WhatArtist.DoesNotExist:
        artist = WhatArtist.update_from_what(get_what_client(request), artist_id)
    return get_artist_dict(artist, True)
 def handle(self, *args, **options):
     print 'Initiating what client...'
     what = get_what_client(dummy_request, True)
     index_response = what.request('index')
     print 'Status:', index_response['status']
     print 'Scanning replica sets...'
     try:
         ReplicaSet.objects.get(zone='what.cd')
         raise Exception('Please delete your what.cd replica set now')
     except ReplicaSet.DoesNotExist:
         pass
     try:
         pth_replica_set = ReplicaSet.get_what_master()
         if pth_replica_set.transinstance_set.count() < 1:
             raise ReplicaSet.DoesNotExist()
     except ReplicaSet.DoesNotExist:
         raise Exception('Please get your PTH replica set ready')
     print 'Scanning locations...'
     location_mapping = {}
     with open('what_manager2_torrents.jsonl', 'rb') as torrents_input:
         for line in torrents_input:
             data = ujson.loads(line)
             location_path = data['location']['path']
             if location_path not in location_mapping:
                 try:
                     new_location = DownloadLocationEquivalent.objects.get(
                         old_location=location_path).new_location
                 except DownloadLocationEquivalent.DoesNotExist:
                     new_location = raw_input(
                         'Enter the new location to map to {}: '.format(
                             location_path))
                     DownloadLocationEquivalent.objects.create(
                         old_location=location_path,
                         new_location=new_location,
                     )
                 location_mapping[location_path] = new_location
     print 'Location mappings:'
     for old_location, new_location in location_mapping.items():
         try:
             DownloadLocation.objects.get(zone='redacted.ch',
                                          path=new_location)
         except DownloadLocation.DoesNotExist:
             raise Exception(
                 'Please create the {} location in the DB in zone redacted.ch'
                 .format(new_location))
         print old_location, '=', new_location
     with open('what_manager2_torrents.jsonl', 'rb') as torrents_input:
         for line in torrents_input:
             data = ujson.loads(line)
             migration_job = TorrentMigrationJob(
                 what,
                 location_mapping,
                 data,
                 flac_only=options['flac_only'])
             migration_job.process()
Ejemplo n.º 14
0
def edit_upload_whatcd(request, upload):
    if request.method == 'POST':
        what_client = get_what_client(request)
        temp_dir = get_temp_dir(upload.upload.metadata.id)
        if request.GET['type'] == 'existing':
            group_id = request.POST['group_id']
            assert group_id
            if 'subgroup' in request.POST:
                upload.upload.upload_to_what(what_client, temp_dir, 'upload_in_subgroup',
                                             [group_id, None])
            elif 'original' in request.POST:
                upload.upload.upload_to_what(what_client, temp_dir, 'upload_in_original_release',
                                             [group_id])
            elif 'with_torrent' in request.POST:
                with_id = request.POST['with_id']
                assert with_id
                upload.upload.upload_to_what(
                    what_client, temp_dir, 'upload_in_subgroup', [group_id, with_id])
            else:
                raise Exception('Unknown button clicked')
        elif request.GET['type'] == 'new':
            release_type_id = request.POST['release_type']
            release_tags = request.POST['tags']
            assert release_type_id
            assert release_tags
            qiller = upload.upload

            def do_upload():
                kwargs = dict()
                if 'force_artists' in request.POST:
                    kwargs['force_artists'] = True
                if request.POST['original_year']:
                    kwargs['remaster'] = True
                    kwargs['original_year'] = request.POST['original_year']
                qiller.upload_to_what(
                    what_client, temp_dir, 'upload_new_group',
                    [release_type_id, release_tags], kwargs
                )
                upload.set_upload(qiller)
                upload.save()

            try:
                do_upload()
            except MissingImageException:
                do_upload_cover(upload, temp_dir, qiller)
                do_upload()
        else:
            raise Exception('Unknown type')
        return redirect(seed_upload, upload.id)
    data = {
        'upload': upload,
        'spectrals': get_spectral_files(upload.upload),
        'release_types': WHAT_RELEASE_TYPES,
    }
    return render(request, 'qobuz2/upload_whatcd.html', data)
Ejemplo n.º 15
0
def get_torrent_group(request, group_id):
    try:
        if 'HTTP_X_REFRESH' in request.META:
            raise WhatTorrentGroup.DoesNotExist()
        torrent_group = WhatTorrentGroup.objects.get(id=group_id)
    except WhatTorrentGroup.DoesNotExist:
        what_client = get_what_client(request)
        torrent_group = WhatTorrentGroup.update_from_what(what_client, group_id)
    data = get_torrent_group_dict(torrent_group)
    data.update(get_torrent_groups_have([torrent_group.id], True)[torrent_group.id])
    return data
Ejemplo n.º 16
0
def sync_profile(request):
    user_id = settings.WHAT_USER_ID
    interval = settings.WHAT_PROFILE_SNAPSHOT_INTERVAL
    try:
        last_snap = WhatUserSnapshot.get_last()
        if (timezone.now() - last_snap.datetime).total_seconds() < interval - 30:
            return
    except WhatUserSnapshot.DoesNotExist:
        pass
    what = get_what_client(request)
    WhatUserSnapshot.get(what, user_id).save()
Ejemplo n.º 17
0
def sync_profile(request):
    user_id = settings.WHAT_USER_ID
    interval = settings.WHAT_PROFILE_SNAPSHOT_INTERVAL
    try:
        last_snap = WhatUserSnapshot.get_last()
        if (timezone.now() -
                last_snap.datetime).total_seconds() < interval - 30:
            return
    except WhatUserSnapshot.DoesNotExist:
        pass
    what = get_what_client(request)
    WhatUserSnapshot.get(what, user_id).save()
Ejemplo n.º 18
0
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 handle(self, *args, **options):
     print 'Initiating what client...'
     what = get_what_client(dummy_request, True)
     index_response = what.request('index')
     print 'Status:', index_response['status']
     print 'Scanning replica sets...'
     try:
         ReplicaSet.objects.get(zone='what.cd')
         raise Exception('Please delete your what.cd replica set now')
     except ReplicaSet.DoesNotExist:
         pass
     try:
         pth_replica_set = ReplicaSet.get_what_master()
         if pth_replica_set.transinstance_set.count() < 1:
             raise ReplicaSet.DoesNotExist()
     except ReplicaSet.DoesNotExist:
         raise Exception('Please get your PTH replica set ready')
     print 'Scanning locations...'
     location_mapping = {}
     with open('what_manager2_torrents.jsonl', 'rb') as torrents_input:
         for line in torrents_input:
             data = ujson.loads(line)
             location_path = data['location']['path']
             if location_path not in location_mapping:
                 try:
                     new_location = DownloadLocationEquivalent.objects.get(
                         old_location=location_path).new_location
                 except DownloadLocationEquivalent.DoesNotExist:
                     new_location = raw_input(
                         'Enter the new location to map to {}: '.format(location_path))
                     DownloadLocationEquivalent.objects.create(
                         old_location=location_path,
                         new_location=new_location,
                     )
                 location_mapping[location_path] = new_location
     print 'Location mappings:'
     for old_location, new_location in location_mapping.items():
         try:
             DownloadLocation.objects.get(zone='redacted.ch', path=new_location)
         except DownloadLocation.DoesNotExist:
             raise Exception(
                 'Please create the {} location in the DB in zone redacted.ch'.format(
                     new_location))
         print old_location, '=', new_location
     with open('what_manager2_torrents.jsonl', 'rb') as torrents_input:
         for line in torrents_input:
             data = ujson.loads(line)
             migration_job = TorrentMigrationJob(what, location_mapping, data,
                                                 flac_only=options['flac_only'])
             migration_job.process()
    def handle(self, *args, **options):
        if len(args) != 1:
            print u'Pass only the source directory.'
            return 1
        source_dir = wm_unicode(args[0])
        if source_dir.endswith('/'):
            source_dir = source_dir[:-1]

        what = get_what_client(lambda: None)
        job = TranscodeSingleJob(what, None, report_progress, None, None, source_dir)
        job.create_torrent()
        raw_input('Please upload the torrent and press enter...')
        job.move_torrent_to_dest()
        add_to_wm_transcode(job.new_torrent['torrent']['id'])
Ejemplo n.º 21
0
    def handle(self, *args, **options):
        if len(args) != 1:
            print u'Pass only the source directory.'
            return 1
        source_dir = wm_unicode(args[0])
        if source_dir.endswith('/'):
            source_dir = source_dir[:-1]

        what = get_what_client(lambda: None)
        job = TranscodeSingleJob(what, None, report_progress, None, None,
                                 source_dir)
        job.create_torrent()
        raw_input('Please upload the torrent and press enter...')
        job.move_torrent_to_dest()
        add_to_wm_transcode(job.new_torrent['torrent']['id'])
Ejemplo n.º 22
0
    def run(self):
        temp_dir = make_temp_dir()
        if os.path.exists(temp_dir):
            raise Exception('Destination directory exists')

        # Pass an object that can hold the what_client property
        self.what = get_what_client(lambda: None)

        os.makedirs(temp_dir)
        try:
            self.transcode_upload_lossless(temp_dir)
        finally:
            try:
                shutil.rmtree(temp_dir)
            except OSError:
                pass
Ejemplo n.º 23
0
    def run(self):
        temp_dir = make_temp_dir()
        if os.path.exists(temp_dir):
            raise Exception('Destination directory exists')

        # Pass an object that can hold the what_client property
        self.what = get_what_client(lambda: None)

        os.makedirs(temp_dir)
        try:
            self.transcode_upload_lossless(temp_dir)
        finally:
            try:
                shutil.rmtree(temp_dir)
            except OSError:
                pass
Ejemplo n.º 24
0
    def run(self):
        try:
            shutil.rmtree(TRANSCODER_TEMP_DIR)
        except OSError:
            pass

        self.what = get_what_client(lambda: None)  # Pass an object that can hold the what_client property

        os.makedirs(TRANSCODER_TEMP_DIR)
        try:
            self.transcode_upload_lossless()
        finally:
            try:
                shutil.rmtree(TRANSCODER_TEMP_DIR)
            except OSError:
                pass
Ejemplo n.º 25
0
    def run(self):
        try:
            shutil.rmtree(TRANSCODER_TEMP_DIR)
        except OSError:
            pass

        # Pass an object that can hold the what_client property
        self.what = get_what_client(lambda: None)

        os.makedirs(TRANSCODER_TEMP_DIR)
        try:
            self.transcode_upload_lossless()
        finally:
            try:
                shutil.rmtree(TRANSCODER_TEMP_DIR)
            except OSError:
                pass
Ejemplo n.º 26
0
    def process_request(t_r):
        t_r.show_retry_button = False

        if t_r.celery_task_id is None:
            try:
                t_torrent = get_trans_torrent(t_r.what_torrent)
                t_torrent.sync_t_torrent()
                if t_torrent.torrent_done == 1:
                    t_r.status = 'download complete. transcoding should start within 1 minute.'
                else:
                    t_r.status = 'downloading ({0:.0%})'.format(
                        t_torrent.torrent_done)
                downloading.append(t_r)
            except TransTorrent.DoesNotExist:
                t_r.status = 'torrent has been removed'
                failed.append(t_r)
        elif t_r.date_completed is not None:
            t_r.status = 'completed {0} ago'.format(
                timesince_filter(t_r.date_completed))
            succeeded.append(t_r)
        else:
            async_result = AsyncResult(t_r.celery_task_id)
            if async_result.state == states.PENDING:
                t_r.status = 'pending transcoding'
                pending.append(t_r)
            elif async_result.state == states.STARTED:
                t_r.status = 'transcoding'
                transcoding.append(t_r)
            elif async_result.state == 'PROGRESS':
                t_r.status = 'transcoding: {0}'.format(
                    async_result.info['status_message'])
                transcoding.append(t_r)
            elif async_result.state == states.SUCCESS:
                t_r.status = 'completed'
                succeeded.append(t_r)
            elif async_result.state == states.FAILURE:
                t_r.show_retry_button = allow_retry
                t_r.status = 'failed - {0}({1})'.format(
                    type(async_result.result).__name__,
                    async_result.result.message)
                failed.append(t_r)
        what_client = get_what_client(request)
        t_r.status = t_r.status.replace(what_client.authkey,
                                        '<authkey>').replace(
                                            what_client.passkey, '<passkey>')
Ejemplo n.º 27
0
def refresh_whattorrent(request):
    if 'id' in request.GET:
        most_recent = WhatTorrent.objects.get(id=request.GET['id'])
    else:
        most_recent = WhatTorrent.objects.defer('torrent_file').order_by('retrieved')[0]
    most_recent_id = most_recent.id
    what = get_what_client(request)
    try:
        response = what.request('torrent', id=most_recent.id)['response']
    except RequestException as ex:
        if ex.response and type(ex.response) is dict and ex.response.get('error') == 'bad id parameter':
            try:
                TransTorrent.objects.get(instance__in=ReplicaSet.get_what_master().transinstance_set.all(),
                                         what_torrent=most_recent)
                return {
                    'success': False,
                    'id': most_recent_id,
                    'status': 'missing',
                }
            except TransTorrent.DoesNotExist:
                most_recent.delete()
                return {
                    'success': True,
                    'id': most_recent_id,
                    'status': 'deleted',
                }
        else:
            return {
                'success': False,
                'status': 'unknown request exception',
            }

    old_retrieved = most_recent.retrieved
    most_recent.info = json.dumps(response)
    most_recent.retrieved = timezone.now()
    most_recent.save()
    return {
        'success': True,
        'id': most_recent_id,
        'status': 'refreshed',
        'retrieved': unicode(old_retrieved),
    }
Ejemplo n.º 28
0
    def process_request(t_r):
        t_r.show_retry_button = False

        if t_r.celery_task_id is None:
            try:
                t_torrent = get_trans_torrent(t_r.what_torrent)
                t_torrent.sync_t_torrent()
                if t_torrent.torrent_done == 1:
                    t_r.status = 'download complete. transcoding should start within 1 minute.'
                else:
                    t_r.status = 'downloading ({0:.0%})'.format(t_torrent.torrent_done)
                downloading.append(t_r)
            except TransTorrent.DoesNotExist:
                t_r.status = 'torrent has been removed'
                failed.append(t_r)
        elif t_r.date_completed is not None:
            t_r.status = 'completed {0} ago'.format(timesince_filter(t_r.date_completed))
            succeeded.append(t_r)
        else:
            async_result = AsyncResult(t_r.celery_task_id)
            if async_result.state == states.PENDING:
                t_r.status = 'pending transcoding'
                pending.append(t_r)
            elif async_result.state == states.STARTED:
                t_r.status = 'transcoding'
                transcoding.append(t_r)
            elif async_result.state == 'PROGRESS':
                t_r.status = 'transcoding: {0}'.format(async_result.info['status_message'])
                transcoding.append(t_r)
            elif async_result.state == states.SUCCESS:
                t_r.status = 'completed'
                succeeded.append(t_r)
            elif async_result.state == states.FAILURE:
                t_r.show_retry_button = allow_retry
                t_r.status = 'failed - {0}({1})'.format(type(async_result.result).__name__,
                                                        async_result.result.message)
                failed.append(t_r)
        what_client = get_what_client(request)
        t_r.status = t_r.status.replace(what_client.authkey, '<authkey>').replace(
            what_client.passkey, '<passkey>')
Ejemplo n.º 29
0
def add_collage(request, collage_id):
    what_client = get_what_client(request)
    response = what_client.request('collage', id=collage_id)['response']
    added = 0
    torrent_group_count = 0
    torrent_count = 0
    for group in response['torrentgroups']:
        if group['categoryId'] not in [1, '1']:
            continue
        artist = get_artists(group)
        title = html_unescape(group['name'])
        release_type = group['releaseType']
        for torrent in group['torrents']:
            what_id = torrent['torrentid']
            priority = filter_torrent(group, torrent)
            if priority and not is_existing(what_id):
                torrent_format = torrent['format']
                encoding = torrent['encoding']
                torrent_size = torrent['size']
                queue_item = QueueItem(
                    what_id=what_id,
                    priority=priority,
                    artist=artist,
                    title=title,
                    release_type=release_type,
                    format=torrent_format,
                    encoding=encoding,
                    torrent_size=torrent_size
                )
                queue_item.save()
                added += 1
            torrent_count += 1
        torrent_group_count += 1
    return {
        'success': True,
        'added': added,
        'groups': torrent_group_count,
        'torrents': torrent_count
    }
Ejemplo n.º 30
0
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}
Ejemplo n.º 31
0
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)
        }
Ejemplo n.º 32
0
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)}
Ejemplo n.º 33
0
def refresh_whattorrent(request):
    what_torrent = None
    if 'id' in request.GET:
        what_torrent = WhatTorrent.objects.get(id=request.GET['id'])
    what_client = get_what_client(request)
    return utils.refresh_whattorrent(what_client, what_torrent)
Ejemplo n.º 34
0
def refresh_whattorrent(request):
    what_torrent = None
    if "id" in request.GET:
        what_torrent = WhatTorrent.objects.get(id=request.GET["id"])
    what_client = get_what_client(request)
    return utils.refresh_whattorrent(what_client, what_torrent)