Exemple #1
0
def get_dest_upload_dir():
    global dest_upload_dir
    if dest_upload_dir is not None:
        return dest_upload_dir
    dest_upload_dir = DownloadLocation.get_what_preferred().path
    db.connection.close()
    return dest_upload_dir
Exemple #2
0
def get_dest_upload_dir():
    global dest_upload_dir
    if dest_upload_dir is not None:
        return dest_upload_dir
    dest_upload_dir = DownloadLocation.get_what_preferred().path
    db.connection.close()
    return dest_upload_dir
Exemple #3
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 #4
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),
    }
 def __init__(self):
     super(Command, self).__init__()
     self.pseudo_request = lambda: None
     self.trans_instance = ReplicaSet.get_what_master().get_preferred_instance()
     self.download_location = DownloadLocation.get_what_preferred()
     self.data_path = None
     self.torrent_path = None
     self.torrent_info = None
     self.info_hash = None
     self.dest_path = None
     self.what_torrent = None
 def __init__(self):
     super(Command, self).__init__()
     self.pseudo_request = lambda: None
     self.trans_instance = None
     self.download_location = DownloadLocation.get_what_preferred()
     self.data_path = None # folder containing files listed in torrent file
     self.torrent_info = None # torrent data
     self.torrent_id = None # torrent ID
     self.info_hash = None # hash of torrent data
     self.dest_path = None # path where transmission expects files to be
     self.what_torrent = None
     self.error_move = True # move folder when error is encountered
 def __init__(self):
     super(Command, self).__init__()
     self.pseudo_request = lambda: None
     self.trans_instance = None
     self.download_location = DownloadLocation.get_what_preferred()
     self.data_path = None  # folder containing files listed in torrent file
     self.torrent_info = None  # torrent data
     self.torrent_id = None  # torrent ID
     self.info_hash = None  # hash of torrent data
     self.dest_path = None  # path where transmission expects files to be
     self.what_torrent = None
     self.error_move = True  # move folder when error is encountered
Exemple #8
0
def move_to_dest_add(request, book_upload):
    location = DownloadLocation.get_what_preferred()
    dest_path = os.path.join(location.path, str(book_upload.what_torrent_id))
    book_path = os.path.join(dest_path, book_upload.target_filename)
    if not os.path.exists(dest_path):
        os.mkdir(dest_path)
    os.chmod(dest_path, 0777)
    shutil.copyfile(book_upload.book_data.storage.path(book_upload.book_data),
                    book_path)
    os.chmod(book_path, 0777)
    manage_torrent.add_torrent(
        request,
        ReplicaSet.get_what_master().get_preferred_instance(), location,
        book_upload.what_torrent_id)
Exemple #9
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 #10
0
def start_seeding(request, upload_id):
    qobuz_upload = QobuzUpload.objects.get(id=upload_id)
    dest_upload_dir = DownloadLocation.get_what_preferred().path
    torrent_file_path = os.path.join(qobuz_upload.temp_media_path,
                                     qobuz_upload.torrent_name + u'.torrent')
    info_hash = get_info_hash(torrent_file_path)
    what_torrent = WhatTorrent.get_or_create(request, info_hash=info_hash)
    qobuz_upload.what_torrent = what_torrent
    qobuz_upload.save()
    dest_path = os.path.join(dest_upload_dir, str(what_torrent.id))
    shutil.rmtree(wm_str(qobuz_upload.spectrals_path))
    os.remove(wm_str(torrent_file_path))
    try:
        os.makedirs(wm_str(dest_path))
    except OSError:
        raise Exception('Dest torrent directory already exists.')
    os.chmod(wm_str(dest_path), 0777)
    shutil.move(wm_str(qobuz_upload.temp_media_path), wm_str(dest_path))
    add_to_wm_transcode(str(what_torrent.id))
    return redirect(edit_upload, upload_id)
Exemple #11
0
def start_seeding(request, upload_id):
    qobuz_upload = QobuzUpload.objects.get(id=upload_id)
    dest_upload_dir = DownloadLocation.get_what_preferred().path
    torrent_file_path = os.path.join(qobuz_upload.temp_media_path,
                                     qobuz_upload.torrent_name + u'.torrent')
    info_hash = get_info_hash(torrent_file_path)
    what_torrent = WhatTorrent.get_or_create(request, info_hash=info_hash)
    qobuz_upload.what_torrent = what_torrent
    qobuz_upload.save()
    dest_path = os.path.join(dest_upload_dir, str(what_torrent.id))
    shutil.rmtree(wm_str(qobuz_upload.spectrals_path))
    os.remove(wm_str(torrent_file_path))
    try:
        os.makedirs(wm_str(dest_path))
    except OSError:
        raise Exception('Dest torrent directory already exists.')
    os.chmod(wm_str(dest_path), 0777)
    shutil.move(wm_str(qobuz_upload.temp_media_path), wm_str(dest_path))
    add_to_wm_transcode(str(what_torrent.id))
    return redirect(edit_upload, upload_id)
Exemple #12
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 #13
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)}
Exemple #14
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 #15
0
    html_unescape,
    fix_pathname,
    extract_upload_errors,
    norm_dest_path,
    get_channels_number,
    recursive_chmod,
    check_directory_tags_filenames,
    get_mp3_ids,
    safe_retrieve_new_torrent,
)


source_roots = [l.path for l in DownloadLocation.objects.filter(zone=ReplicaSet.ZONE_WHAT)]
print "Source roots are", source_roots
try:
    dest_upload_dir = DownloadLocation.get_what_preferred().path
except DownloadLocation.DoesNotExist:
    dest_upload_dir = None
print "Upload destination is", dest_upload_dir
db.connection.close()


class TranscodeSingleJob(object):
    def __init__(self, what, what_torrent, report_progress, source_dir, bitrate, torrent_temp_dir=None):
        self.what = what
        self.force_warnings = False
        self.what_torrent = what_torrent
        self.report_progress = report_progress
        self.source_dir = source_dir
        self.bitrate = bitrate
        if torrent_temp_dir is None:
Exemple #16
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,
    }