Exemple #1
0
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
Exemple #2
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
    }
Exemple #3
0
def download_bibliotik_zip(request, bibliotik_id):
    b_torrent = None
    for instance in ReplicaSet.get_bibliotik_master().transinstance_set.all():
        try:
            b_torrent = BibliotikTransTorrent.objects.get(instance=instance,
                                                          bibliotik_torrent_id=bibliotik_id)
        except BibliotikTransTorrent.DoesNotExist:
            pass
    if not b_torrent:
        return HttpResponse('Could not find that torrent.')

    torrent_files = []
    for root, rel_path, files in os.walk(b_torrent.path):
        for file in files:
            assert root.find(b_torrent.path) != -1
            rel_path = root.replace(b_torrent.path, '')
            if rel_path.startswith('/') or rel_path.startswith('\\'):
                rel_path = rel_path[1:]
            rel_path = os.path.join(rel_path.encode('utf-8'), file)
            torrent_files.append((rel_path, os.path.join(root, file)))

    download_filename = u'[{0}] {1}.zip'.format(bibliotik_id, b_torrent.torrent_name)

    response = download_zip_handler(download_filename, torrent_files)
    LogEntry.add(request.user, u'action', u'Downloaded {0} - {1}'
                 .format(b_torrent, filesizeformat(response['Content-Length'])))
    return response
Exemple #4
0
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
Exemple #5
0
def download_zip(request, what_id):
    t_torrent = None
    for instance in ReplicaSet.get_what_master().transinstance_set.all():
        try:
            t_torrent = TransTorrent.objects.get(instance=instance, what_torrent_id=what_id)
        except TransTorrent.DoesNotExist:
            pass
    if not t_torrent:
        return HttpResponse('Could not find that torrent.')

    torrent_file = [f for f in os.listdir(t_torrent.path) if '.torrent' in f]
    if len(torrent_file) == 1:
        torrent_file = os.path.splitext(torrent_file[0])[0]
    else:
        return HttpResponse('Not one .torrent in dir: ' + t_torrent.path)

    target_dir = os.path.join(t_torrent.path, t_torrent.torrent_name).encode('utf-8')
    torrent_files = []
    for root, rel_path, files in os.walk(target_dir):
        for file in files:
            rel_path = root.replace(target_dir, '')
            if rel_path.startswith('/') or rel_path.startswith('\\'):
                rel_path = rel_path[1:]
            rel_path = os.path.join(rel_path.encode('utf-8'), file)
            torrent_files.append((rel_path, os.path.join(root, file)))

    download_filename = '[{0}] {1}.zip'.format(what_id, torrent_file)

    response = download_zip_handler(download_filename, torrent_files)
    LogEntry.add(request.user, u'action', u'Downloaded {0} - {1}'.format(
        t_torrent, filesizeformat(response['Content-Length'])
    ))
    return response
Exemple #6
0
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))
Exemple #7
0
def download_bibliotik_zip(request, bibliotik_id):
    b_torrent = None
    for instance in ReplicaSet.get_bibliotik_master().transinstance_set.all():
        try:
            b_torrent = BibliotikTransTorrent.objects.get(
                instance=instance, bibliotik_torrent_id=bibliotik_id)
        except BibliotikTransTorrent.DoesNotExist:
            pass
    if not b_torrent:
        return HttpResponse('Could not find that torrent.')

    torrent_files = []
    for root, rel_path, files in os.walk(b_torrent.path):
        for file in files:
            assert root.find(b_torrent.path) != -1
            rel_path = root.replace(b_torrent.path, '')
            if rel_path.startswith('/') or rel_path.startswith('\\'):
                rel_path = rel_path[1:]
            rel_path = os.path.join(rel_path.encode('utf-8'), file)
            torrent_files.append((rel_path, os.path.join(root, file)))

    download_filename = u'[{0}] {1}.zip'.format(bibliotik_id,
                                                b_torrent.torrent_name)

    response = download_zip_handler(download_filename, torrent_files)
    LogEntry.add(
        request.user, u'action', u'Downloaded {0} - {1}'.format(
            b_torrent, filesizeformat(response['Content-Length'])))
    return response
Exemple #8
0
def checks(request):
    try:
        result = WhatManager2.checks.run_checks()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u"error", u"Error running checks: {0}".format(ex), tb)
        return {"success": False, "error": unicode(ex), "traceback": tb}
    result.update({"success": True})
    return result
Exemple #9
0
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()
Exemple #10
0
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 sync_instance_db(instance):
    b_torrents = instance.get_b_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(
        BibliotikTransTorrent.sync_t_arguments)

    for c_hash, b_torrent in b_torrents.items():
        if c_hash not in t_torrents:
            b_torrent_path = b_torrent.path.encode('utf-8')

            messages = []
            with transaction.atomic():
                b_torrent.delete()
                del b_torrents[c_hash]

                if instance.replica_set.is_master:
                    if os.path.exists(b_torrent_path):
                        files = os.listdir(b_torrent_path)
                        if len(files):
                            messages.append(
                                u'There are other files so leaving in place.')
                        else:
                            messages.append(
                                u'No other files. Deleting directory.')
                            os.rmdir(b_torrent_path)
                    else:
                        messages.append(u'Path does not exist.')

            LogEntry.add(
                None, u'action',
                u'Bibliotik torrent {0} deleted from instance {1}. {2}'.format(
                    b_torrent, instance, ' '.join(messages)))

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in b_torrents:
                torrent_id = int(os.path.basename(t_torrent.downloadDir))
                w_torrent = BibliotikTorrent.get_or_create(None, torrent_id)
                d_location = DownloadLocation.get_by_full_path(
                    t_torrent.downloadDir)
                m_torrent = manage_bibliotik.add_bibliotik_torrent(
                    w_torrent.id, instance, d_location, None, False)
                b_torrents[m_torrent.info_hash] = m_torrent
                LogEntry.add(
                    None, u'action',
                    u'Bibliotik torrent {0} appeared in instance {1}.'.format(
                        t_torrent.name, instance))
            else:
                b_torrent = b_torrents[c_hash]
                b_torrent.sync_t_torrent(t_torrent)
Exemple #12
0
def checks(request):
    try:
        result = WhatManager2.checks.run_checks()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error running checks: {0}'.format(ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }
    result.update({
        'success': True
    })
    return result
Exemple #13
0
def checks(request):
    try:
        result = WhatManager2.checks.run_checks()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error running checks: {0}'.format(ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }
    result.update({
        'success': True
    })
    return result
Exemple #14
0
def sync(request):
    start_time = time.time()

    try:
        master = ReplicaSet.get_bibliotik_master()
        trans_sync.sync_all_instances_db(master)
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(
            request.user, u'error',
            u'Error syncing bibliotik master DB: {0}({1})'.format(
                type(ex).__name__, ex), tb)
        return {'success': False, 'error': unicode(ex), 'traceback': tb}

    time_taken = time.time() - start_time
    LogEntry.add(request.user, u'info',
                 u'Completed bibliotik sync in {0:.3f}s.'.format(time_taken))
    return {'success': True}
def sync_instance_db(instance):
    mam_torrents = instance.get_mam_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(MAMTransTorrent.sync_t_arguments)

    for c_hash, mam_torrent in mam_torrents.items():
        if c_hash not in t_torrents:
            mam_torrent_path = mam_torrent.path.encode('utf-8')

            messages = []
            with transaction.atomic():
                mam_torrent.delete()
                del mam_torrents[c_hash]

                if instance.replica_set.is_master:
                    if os.path.exists(mam_torrent_path):
                        files = os.listdir(mam_torrent_path)
                        if len(files):
                            messages.append(u'There are other files so leaving in place.')
                        else:
                            messages.append(u'No other files. Deleting directory.')
                            os.rmdir(mam_torrent_path)
                    else:
                        messages.append(u'Path does not exist.')

            LogEntry.add(None, u'action',
                         u'MAM torrent {0} deleted from instance {1}. {2}'
                         .format(mam_torrent, instance, ' '.join(messages)))

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in mam_torrents:
                torrent_id = int(os.path.basename(t_torrent.downloadDir))
                w_torrent = MAMTorrent.get_or_create(None, torrent_id)
                d_location = DownloadLocation.get_by_full_path(t_torrent.downloadDir)
                m_torrent = manage_mam.add_mam_torrent(w_torrent.id, instance,
                                                       d_location, None, False)
                mam_torrents[m_torrent.info_hash] = m_torrent
                LogEntry.add(None, u'action',
                             u'MAM torrent {0} appeared in instance {1}.'
                             .format(t_torrent.name, instance))
            else:
                mam_torrent = mam_torrents[c_hash]
                mam_torrent.sync_t_torrent(t_torrent)
Exemple #16
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 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 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))
Exemple #19
0
def auto_pop(request):
    front = QueueItem.get_front()
    if not front:
        LogEntry.add(request.user, u'info', 'Auto pop: queue is empty.')
        return {
            'success': False,
            'error': 'Queue is empty.'
        }
    try:
        ratio_delta = get_auto_pop_ratio_delta(WhatUserSnapshot.get_last())
    except WhatUserSnapshot.DoesNotExist:
        LogEntry.add(request.user, u'info', 'Auto pop: User profile not updated, skipping pop.')
        return {
            'success': False,
            'error': u'User profile not updated, skipping pop.'
        }
    if ratio_delta >= front.torrent_size:
        return do_pop(request)
    else:
        message = u'Auto pop: ratio delta {0} < {1}, skipping pop.'.format(
            filesizeformat(ratio_delta),
            filesizeformat(front.torrent_size)
        )
        LogEntry.add(request.user, u'info', message)
        return {
            'success': False,
            'error': u'Buffer is {0}, skipping pop.'.format(message)
        }
Exemple #20
0
def sync_replicas(request):
    start_time = time.time()
    part_start_time = time.time()

    master = ReplicaSet.get_what_master()
    try:
        for replica_set in ReplicaSet.objects.all():
            if replica_set.id != master.id:
                trans_sync.sync_all_instances_db(request, replica_set)
        replicas_dbs_time = time.time() - part_start_time
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u"error", u"Error syncing replicas DB: {0}".format(ex), tb)
        return {"success": False, "error": unicode(ex), "traceback": tb}

    try:
        trans_sync.sync_all_replicas_to_master()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u"error", u"Error running replica sync: {0}".format(ex), tb)
        return {"success": False, "error": unicode(ex), "traceback": tb}
    time_taken = time.time() - start_time

    LogEntry.add(
        request.user,
        u"info",
        u"Completed replica sync in {0:.3f}s. DB in {1:.3f}s.".format(time_taken, replicas_dbs_time),
    )
    return {"success": True}
Exemple #21
0
def download_zip(request, what_id):
    t_torrent = None
    for instance in ReplicaSet.get_what_master().transinstance_set.all():
        try:
            t_torrent = TransTorrent.objects.get(instance=instance,
                                                 what_torrent_id=what_id)
        except TransTorrent.DoesNotExist:
            pass
    if not t_torrent:
        return HttpResponse('Could not find that torrent.')

    torrent_file = [f for f in os.listdir(t_torrent.path) if '.torrent' in f]
    if len(torrent_file) == 1:
        torrent_file = os.path.splitext(torrent_file[0])[0]
    else:
        return HttpResponse('Not one .torrent in dir: ' + t_torrent.path)

    target_dir = os.path.join(t_torrent.path,
                              t_torrent.torrent_name).encode('utf-8')
    torrent_files = []
    if not os.path.isdir(target_dir):
        torrent_files.append((t_torrent.torrent_name, target_dir))
    else:
        for root, rel_path, files in os.walk(target_dir):
            for file in files:
                rel_path = root.replace(target_dir, '')
                if rel_path.startswith('/') or rel_path.startswith('\\'):
                    rel_path = rel_path[1:]
                rel_path = os.path.join(rel_path.encode('utf-8'), file)
                torrent_files.append((rel_path, os.path.join(root, file)))

    download_filename = u'[{0}] {1}.zip'.format(what_id,
                                                torrent_file).encode('utf-8')

    response = download_zip_handler(download_filename, torrent_files)
    LogEntry.add(
        request.user, u'action', u'Downloaded {0} - {1}'.format(
            t_torrent, filesizeformat(response['Content-Length'])))
    return response
def sync_instance_db(instance):
    b_torrents = instance.get_b_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(BibliotikTransTorrent.sync_t_arguments)

    for c_hash, b_torrent in b_torrents.items():
        if not c_hash in t_torrents:
            b_torrent_path = b_torrent.path.encode('utf-8')

            messages = []
            with transaction.atomic():
                b_torrent.delete()
                del b_torrents[c_hash]

                if instance.replica_set.is_master:
                    if os.path.exists(b_torrent_path):
                        files = os.listdir(b_torrent_path)
                        if len(files):
                            messages.append(u'There are other files so leaving in place.')
                        else:
                            messages.append(u'No other files. Deleting directory.')
                            os.rmdir(b_torrent_path)
                    else:
                        messages.append(u'Path does not exist.')

            LogEntry.add(None, u'action',
                         u'Bibliotik torrent {0} deleted from instance {1}. {2}'
                         .format(b_torrent, instance, ' '.join(messages)))

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if not c_hash in b_torrents:
                LogEntry.add(None, u'error',
                             u'Bibliotik torrent {0} appeared in instance {1}.'
                             .format(t_torrent.name, instance))
                break
            else:
                b_torrent = b_torrents[c_hash]
                b_torrent.sync_t_torrent(t_torrent)
Exemple #23
0
def sync(request):
    start_time = time.time()

    try:
        master = ReplicaSet.get_bibliotik_master()
        trans_sync.sync_all_instances_db(master)
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error syncing bibliotik master DB: {0}({1})'
                     .format(type(ex).__name__, ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }

    time_taken = time.time() - start_time
    LogEntry.add(request.user, u'info',
                 u'Completed bibliotik sync in {0:.3f}s.'
                 .format(time_taken))
    return {
        'success': True
    }
Exemple #24
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}
Exemple #25
0
def add_torrent(request, torrent_id):
    if 'bibliotik_id' not in request.GET:
        return {
            'success': False,
            'error': u'Missing bibliotik_id in GET.',
        }
    bibliotik_client = None
    try:
        bibliotik_id = request.GET['bibliotik_id']
        bibliotik_client = BibliotikClient(bibliotik_id)
        b_torrent = manage_bibliotik.add_bibliotik_torrent(
            torrent_id, bibliotik_client=bibliotik_client)
        bibliotik_torrent = b_torrent.bibliotik_torrent
        LogEntry.add(request.user, u'action',
                     u'Added {0} to {1}'.format(b_torrent, b_torrent.instance))
        return {
            'success': True,
            'title': bibliotik_torrent.title,
            'author': bibliotik_torrent.author,
        }
    except TorrentAlreadyAddedException:
        bibliotik_torrent = None
        try:
            bibliotik_torrent = BibliotikTorrent.objects.get(id=torrent_id)
        except BibliotikTorrent.DoesNotExist:
            pass
        LogEntry.add(
            request.user, u'info',
            u'Tried adding bibliotik torrent_id={0}, already added.'.format(
                torrent_id))
        return {
            'success':
            False,
            'error_code':
            u'already_added',
            'error':
            u'Already added.',
            'title': (bibliotik_torrent.title if bibliotik_torrent else
                      '<<< Unable to find torrent >>>'),
            'author': (bibliotik_torrent.author if bibliotik_torrent else
                       '<<< Unable to find torrent >>>'),
        }
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(
            request.user, u'error',
            u'Tried adding bibliotik torrent_id={0}. Error: {1}'.format(
                torrent_id, unicode(ex)), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb,
        }
Exemple #26
0
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
    }
Exemple #27
0
def add_torrent(request, torrent_id):
    if 'bibliotik_id' not in request.GET:
        return {
            'success': False,
            'error': u'Missing bibliotik_id in GET.',
        }
    bibliotik_client = None
    try:
        bibliotik_id = request.GET['bibliotik_id']
        bibliotik_client = BibliotikClient(bibliotik_id)
        b_torrent = manage_bibliotik.add_bibliotik_torrent(torrent_id,
                                                           bibliotik_client=bibliotik_client)
        bibliotik_torrent = b_torrent.bibliotik_torrent
        LogEntry.add(request.user, u'action', u'Added {0} to {1}'
                     .format(b_torrent, b_torrent.instance))
        return {
            'success': True,
            'title': bibliotik_torrent.title,
            'author': bibliotik_torrent.author,
        }
    except TorrentAlreadyAddedException:
        bibliotik_torrent = None
        try:
            bibliotik_torrent = BibliotikTorrent.objects.get(id=torrent_id)
        except BibliotikTorrent.DoesNotExist:
            pass
        LogEntry.add(request.user, u'info',
                     u'Tried adding bibliotik torrent_id={0}, already added.'.format(torrent_id))
        return {
            'success': False,
            'error_code': u'already_added',
            'error': u'Already added.',
            'title': (bibliotik_torrent.title if bibliotik_torrent
                      else '<<< Unable to find torrent >>>'),
            'author': (bibliotik_torrent.author if bibliotik_torrent
                       else '<<< Unable to find torrent >>>'),
        }
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error',
                     u'Tried adding bibliotik torrent_id={0}. Error: {1}'
                     .format(torrent_id, unicode(ex)), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb,
        }
Exemple #28
0
def sync(request):
    start_time = time.time()
    part_start_time = time.time()
    try:
        trans_sync.sync_profile(request)
        profile_time = time.time() - part_start_time
        part_start_time = time.time()
    except Exception as ex:
        profile_time = 0
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error syncing profile: {0}'.format(ex), tb)

    try:
        master = ReplicaSet.get_what_master()
        trans_sync.sync_all_instances_db(request, master)
        master_db_time = time.time() - part_start_time
        part_start_time = time.time()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error syncing master DB: {0}({1})'.format(
            type(ex).__name__, ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }

    # try:
    # if trans_sync.sync_fulltext():
    # LogEntry.add(request.user, u'error', u'Fulltext table was out of sync. Synced.')
    # except Exception as ex:
    # tb = traceback.format_exc()
    # LogEntry.add(request.user, u'error', u'Error syncing fulltext table: {0}'.format(ex), tb)
    # return {
    # 'success': False,
    # 'error': unicode(ex),
    # 'traceback': tb
    # }

    time_taken = time.time() - start_time
    LogEntry.add(request.user, u'info',
                 u'Completed what.cd sync in {0:.3f}s. Profile in {1:.3f}s. Master DB in {2:.3f}s.'
                 .format(time_taken, profile_time, master_db_time))
    return {
        'success': True
    }
Exemple #29
0
def sync(request):
    start_time = time.time()
    part_start_time = time.time()
    try:
        trans_sync.sync_profile(request)
        profile_time = time.time() - part_start_time
        part_start_time = time.time()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error syncing profile: {0}'.format(ex), tb)

    try:
        master = ReplicaSet.get_what_master()
        trans_sync.sync_all_instances_db(request, master)
        master_db_time = time.time() - part_start_time
        part_start_time = time.time()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error syncing master DB: {0}({1})'.format(type(ex).__name__, ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }

    # try:
    # if trans_sync.sync_fulltext():
    # LogEntry.add(request.user, u'error', u'Fulltext table was out of sync. Synced.')
    # except Exception as ex:
    # tb = traceback.format_exc()
    # LogEntry.add(request.user, u'error', u'Error syncing fulltext table: {0}'.format(ex), tb)
    #     return {
    #         'success': False,
    #         'error': unicode(ex),
    #         'traceback': tb
    #     }

    time_taken = time.time() - start_time
    LogEntry.add(request.user, u'info',
                 u'Completed what.cd sync in {0:.3f}s. Profile in {1:.3f}s. Master DB in {2:.3f}s.'
                 .format(time_taken, profile_time, master_db_time))
    return {
        'success': True
    }
Exemple #30
0
def add_torrent(request, torrent_id):
    mam_client = MAMClient.get()
    try:
        m_torrent = manage_mam.add_mam_torrent(torrent_id,
                                               mam_client=mam_client)
        mam_torrent = m_torrent.mam_torrent
        LogEntry.add(request.user, u'action',
                     u'Added {0} to {1}'.format(m_torrent, m_torrent.instance))
        return {
            'success': True,
            'title': mam_torrent.title,
        }
    except TorrentAlreadyAddedException:
        mam_torrent = None
        try:
            mam_torrent = MAMTorrent.objects.get(id=torrent_id)
        except MAMTorrent.DoesNotExist:
            pass
        LogEntry.add(
            request.user, u'info',
            u'Tried adding MyAnonaMouse torrent_id={0}, already added.'.format(
                torrent_id))
        return {
            'success':
            False,
            'error_code':
            u'already_added',
            'error':
            u'Already added.',
            'title': (mam_torrent.title
                      if mam_torrent else '<<< Unable to find torrent >>>'),
        }
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(
            request.user, u'error',
            u'Tried adding MyAnonaMouse torrent_id={0}. Error: {1}'.format(
                torrent_id, unicode(ex)), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb,
        }
Exemple #31
0
def sync_replicas(request):
    start_time = time.time()
    part_start_time = time.time()

    master = ReplicaSet.get_what_master()
    try:
        for replica_set in ReplicaSet.objects.all():
            if replica_set.id != master.id:
                trans_sync.sync_all_instances_db(request, replica_set)
        replicas_dbs_time = time.time() - part_start_time
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error syncing replicas DB: {0}'.format(ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }

    try:
        trans_sync.sync_all_replicas_to_master()
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error', u'Error running replica sync: {0}'.format(ex), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb
        }
    time_taken = time.time() - start_time

    LogEntry.add(request.user, u'info',
                 u'Completed replica sync in {0:.3f}s. DB in {1:.3f}s.'.format(time_taken,
                                                                               replicas_dbs_time))
    return {
        'success': True
    }
Exemple #32
0
def add_torrent(request, torrent_id):
    mam_client = MAMClient.get()
    try:
        m_torrent = manage_mam.add_mam_torrent(torrent_id, mam_client=mam_client)
        mam_torrent = m_torrent.mam_torrent
        LogEntry.add(request.user, u'action', u'Added {0} to {1}'
                     .format(m_torrent, m_torrent.instance))
        return {
            'success': True,
            'title': mam_torrent.title,
        }
    except TorrentAlreadyAddedException:
        mam_torrent = None
        try:
            mam_torrent = MAMTorrent.objects.get(id=torrent_id)
        except MAMTorrent.DoesNotExist:
            pass
        LogEntry.add(request.user, u'info',
                     u'Tried adding MyAnonaMouse torrent_id={0}, already added.'.format(torrent_id))
        return {
            'success': False,
            'error_code': u'already_added',
            'error': u'Already added.',
            'title': (mam_torrent.title if mam_torrent
                      else '<<< Unable to find torrent >>>'),
        }
    except Exception as ex:
        tb = traceback.format_exc()
        LogEntry.add(request.user, u'error',
                     u'Tried adding MyAnonaMouse torrent_id={0}. Error: {1}'
                     .format(torrent_id, unicode(ex)), tb)
        return {
            'success': False,
            'error': unicode(ex),
            'traceback': tb,
        }
Exemple #33
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)
        }
Exemple #34
0
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,
    }
Exemple #35
0
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
Exemple #36
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)}