Пример #1
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
Пример #2
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),
    }
Пример #3
0
def init_sync_instance_db(instance):
    b_torrents = instance.get_b_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(
        BibliotikTransTorrent.sync_t_arguments)

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in b_torrents:
                try:
                    bibliotik_torrent = BibliotikTorrent.objects.get(
                        info_hash=c_hash)
                    d_location = DownloadLocation.get_by_full_path(
                        t_torrent.downloadDir)

                    b_torrent = manage_bibliotik.add_bibliotik_torrent(
                        bibliotik_torrent.id,
                        instance,
                        d_location,
                        add_to_client=False)
                    b_torrents[b_torrent.info_hash] = b_torrent
                except BibliotikTorrent.DoesNotExist:
                    raise Exception(u'Could not find hash {0} for name {1} in '
                                    u'DB during initial sync.'.format(
                                        c_hash, t_torrent.name))

            b_torrent = b_torrents[c_hash]
            b_torrent.sync_t_torrent(t_torrent)
Пример #4
0
def init_sync_instance_db(instance):
    b_torrents = instance.get_b_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(BibliotikTransTorrent.sync_t_arguments)

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if not c_hash in b_torrents:
                try:
                    bibliotik_torrent = BibliotikTorrent.objects.get(info_hash=c_hash)
                    d_location = DownloadLocation.get_by_full_path(t_torrent.downloadDir)

                    b_torrent = manage_bibliotik.add_bibliotik_torrent(
                        bibliotik_torrent.id,
                        instance,
                        d_location,
                        add_to_client=False
                    )
                    b_torrents[b_torrent.info_hash] = b_torrent
                except BibliotikTorrent.DoesNotExist:
                    raise Exception(u'Could not find hash {0} for name {1} in DB during initial sync.'.format(
                        c_hash, t_torrent.name
                    ))

            b_torrent = b_torrents[c_hash]
            b_torrent.sync_t_torrent(t_torrent)
Пример #5
0
def upload_book_to_bibliotik(bibliotik_client, book_upload):
    print 'Sending request for upload to bibliotik.me'

    payload_files = dict()
    payload_files['TorrentFileField'] = ('torrent.torrent', book_upload.bibliotik_torrent_file)

    payload = dict()
    payload['upload'] = ''
    payload['authkey'] = bibliotik_client.get_auth_key()
    payload['AuthorsField'] = book_upload.author
    payload['TitleField'] = book_upload.title
    payload['IsbnField'] = book_upload.isbn or ''
    payload['PublishersField'] = book_upload.publisher
    payload['PagesField'] = book_upload.pages or ''
    payload['YearField'] = book_upload.year
    payload['FormatField'] = {
        'AZW3': '21',
        'EPUB': '15',
        'PDF': '2',
    }[book_upload.format]
    payload['LanguageField'] = '1'  # English
    if book_upload.retail:
        payload['RetailField'] = '1'
    payload['TagsField'] = ','.join(book_upload.tag_list)
    payload['ImageField'] = book_upload.cover_url
    payload['DescriptionField'] = book_upload.description

    response = bibliotik_client.send_upload(payload, payload_files)
    response.raise_for_status()
    if response.status_code == requests.codes.ok:
        with open(os.path.join(MEDIA_ROOT, 'bibliotik_upload.html'), 'wb') as f:
            f.write(response.content)
        raise Exception('Bibliotik does not want this. Written to media/')
    redirect_match = re.match('^https://bibliotik.me/torrents/(?P<id>\d+)$',
                              response.headers['location'])
    if not redirect_match:
        raise Exception('Could not get new torrent ID.')
    torrent_id = redirect_match.groupdict()['id']

    book_upload.bibliotik_torrent = BibliotikTorrent.get_or_create(bibliotik_client, torrent_id)
    book_upload.save()

    # Add the torrent to the client
    location = DownloadLocation.get_bibliotik_preferred()
    download_dir = os.path.join(location.path, unicode(book_upload.bibliotik_torrent.id))
    book_path = os.path.join(download_dir, book_upload.target_filename)
    if not os.path.exists(download_dir):
        os.mkdir(download_dir)
    os.chmod(download_dir, 0777)
    shutil.copyfile(
        book_upload.book_data.storage.path(book_upload.book_data),
        book_path
    )
    os.chmod(book_path, 0777)

    manage_bibliotik.add_bibliotik_torrent(
        book_upload.bibliotik_torrent.id, location=location, bibliotik_client=bibliotik_client
    )

    return book_upload
Пример #6
0
def init_sync_instance_db(instance):
    mam_torrents = instance.get_mam_torrents_by_hash()
    t_torrents = instance.get_t_torrents_by_hash(MAMTransTorrent.sync_t_arguments)

    with transaction.atomic():
        for c_hash, t_torrent in t_torrents.items():
            if c_hash not in mam_torrents:
                try:
                    mam_torrent = MAMTorrent.objects.get(info_hash=c_hash)
                    d_location = DownloadLocation.get_by_full_path(t_torrent.downloadDir)

                    mam_torrent = manage_mam.add_mam_torrent(
                        mam_torrent.id,
                        instance,
                        d_location,
                        add_to_client=False
                    )
                    mam_torrents[mam_torrent.info_hash] = mam_torrent
                except MAMTorrent.DoesNotExist:
                    raise Exception(u'Could not find hash {0} for name {1} in '
                                    u'DB during initial sync.'
                                    .format(c_hash, t_torrent.name))

            mam_torrent = mam_torrents[c_hash]
            mam_torrent.sync_t_torrent(t_torrent)
Пример #7
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
Пример #8
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
Пример #9
0
def add_bibliotik_torrent(torrent_id,
                          instance=None,
                          location=None,
                          bibliotik_client=None,
                          add_to_client=True):
    bibliotik_torrent = BibliotikTorrent.get_or_create(bibliotik_client,
                                                       torrent_id)

    if not instance:
        instance = ReplicaSet.get_bibliotik_master().get_preferred_instance()
    if not location:
        location = DownloadLocation.get_bibliotik_preferred()

    with LockModelTables(BibliotikTransTorrent, TransInstance):
        try:
            existing_one = BibliotikTransTorrent.objects.get(
                info_hash=bibliotik_torrent.info_hash)
            raise TorrentAlreadyAddedException(
                u'Already added (instance={0}, new_instance={1}, info_hash={2}).'
                .format(instance, existing_one.instance,
                        bibliotik_torrent.info_hash))
        except BibliotikTransTorrent.DoesNotExist:
            pass

        download_dir = os.path.join(location.path,
                                    unicode(bibliotik_torrent.id))

        def create_b_torrent():
            new_b_torrent = BibliotikTransTorrent(
                instance=instance,
                location=location,
                bibliotik_torrent=bibliotik_torrent,
                info_hash=bibliotik_torrent.info_hash,
            )
            new_b_torrent.save()
            return new_b_torrent

        if add_to_client:
            with transaction.atomic():
                b_torrent = create_b_torrent()

                t_torrent = instance.client.add_torrent(
                    base64.b64encode(bibliotik_torrent.torrent_file),
                    download_dir=download_dir,
                    paused=False)
                t_torrent = instance.client.get_torrent(
                    t_torrent.id,
                    arguments=BibliotikTransTorrent.sync_t_arguments)

                if not os.path.exists(download_dir):
                    os.mkdir(download_dir)
                if not os.stat(download_dir).st_mode & 0777 == 0777:
                    os.chmod(download_dir, 0777)

                norm_t_torrent(t_torrent)
                b_torrent.sync_t_torrent(t_torrent)
        else:
 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
Пример #13
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()
Пример #14
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)
Пример #15
0
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)
Пример #16
0
def add_bibliotik_torrent(torrent_id, instance=None, location=None, bibliotik_client=None,
                          add_to_client=True):
    bibliotik_torrent = BibliotikTorrent.get_or_create(bibliotik_client, torrent_id)

    if not instance:
        instance = ReplicaSet.get_bibliotik_master().get_preferred_instance()
    if not location:
        location = DownloadLocation.get_bibliotik_preferred()

    with LockModelTables(BibliotikTransTorrent, TransInstance):
        try:
            existing_one = BibliotikTransTorrent.objects.get(info_hash=bibliotik_torrent.info_hash)
            raise TorrentAlreadyAddedException(u'Already added (instance={0}, new_instance={1}, info_hash={2}).'.format(
                 instance, existing_one.instance, bibliotik_torrent.info_hash))
        except BibliotikTransTorrent.DoesNotExist:
            pass

        download_dir = os.path.join(location.path, unicode(bibliotik_torrent.id))

        def create_b_torrent():
            new_b_torrent = BibliotikTransTorrent(
                instance=instance,
                location=location,
                bibliotik_torrent=bibliotik_torrent,
                info_hash=bibliotik_torrent.info_hash,
            )
            new_b_torrent.save()
            return new_b_torrent

        if add_to_client:
            with transaction.atomic():
                b_torrent = create_b_torrent()

                t_torrent = instance.client.add_torrent(
                    base64.b64encode(bibliotik_torrent.torrent_file),
                    download_dir=download_dir,
                    paused=False
                )
                t_torrent = instance.client.get_torrent(
                    t_torrent.id, arguments=BibliotikTransTorrent.sync_t_arguments)

                if not os.path.exists(download_dir):
                    os.mkdir(download_dir)
                if not os.stat(download_dir).st_mode & 0777 == 0777:
                    os.chmod(download_dir, 0777)

                norm_t_torrent(t_torrent)
                b_torrent.sync_t_torrent(t_torrent)
        else:
Пример #17
0
def add_mam_torrent(torrent_id, instance=None, location=None, mam_client=None,
                          add_to_client=True):
    mam_torrent = MAMTorrent.get_or_create(mam_client, torrent_id)

    if not instance:
        instance = ReplicaSet.get_myanonamouse_master().get_preferred_instance()
    if not location:
        location = DownloadLocation.get_myanonamouse_preferred()

    with LockModelTables(MAMTransTorrent):
        try:
            MAMTransTorrent.objects.get(info_hash=mam_torrent.info_hash)
            raise TorrentAlreadyAddedException(u'Already added.')
        except MAMTransTorrent.DoesNotExist:
            pass

        download_dir = os.path.join(location.path, unicode(mam_torrent.id))

        def create_b_torrent():
            new_b_torrent = MAMTransTorrent(
                instance=instance,
                location=location,
                mam_torrent=mam_torrent,
                info_hash=mam_torrent.info_hash,
            )
            new_b_torrent.save()
            return new_b_torrent

        if add_to_client:
            with transaction.atomic():
                b_torrent = create_b_torrent()

                t_torrent = instance.client.add_torrent(
                    base64.b64encode(mam_torrent.torrent_file),
                    download_dir=download_dir,
                    paused=False
                )
                t_torrent = instance.client.get_torrent(
                    t_torrent.id, arguments=MAMTransTorrent.sync_t_arguments)

                if not os.path.exists(download_dir):
                    os.mkdir(download_dir)
                if not os.stat(download_dir).st_mode & 0777 == 0777:
                    os.chmod(download_dir, 0777)

                norm_t_torrent(t_torrent)
                b_torrent.sync_t_torrent(t_torrent)
        else:
Пример #18
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
    }
Пример #19
0
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)
Пример #20
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()
Пример #21
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)
Пример #22
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)
Пример #23
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
Пример #24
0
def upload_book_to_bibliotik(bibliotik_client, book_upload):
    print 'Sending request for upload to bibliotik.me'

    payload_files = dict()
    payload_files['TorrentFileField'] = ('torrent.torrent',
                                         book_upload.bibliotik_torrent_file)

    payload = dict()
    payload['upload'] = ''
    payload['authkey'] = bibliotik_client.get_auth_key()
    payload['AuthorsField'] = book_upload.author
    payload['TitleField'] = book_upload.title
    payload['IsbnField'] = book_upload.isbn or ''
    payload['PublishersField'] = book_upload.publisher
    payload['PagesField'] = book_upload.pages or ''
    payload['YearField'] = book_upload.year
    payload['FormatField'] = {
        'AZW3': '21',
        'EPUB': '15',
        'PDF': '2',
    }[book_upload.format]
    payload['LanguageField'] = '1'  # English
    if book_upload.retail:
        payload['RetailField'] = '1'
    payload['TagsField'] = ','.join(book_upload.tag_list)
    payload['ImageField'] = book_upload.cover_url
    payload['DescriptionField'] = book_upload.description

    response = bibliotik_client.send_upload(payload, payload_files)
    response.raise_for_status()
    if response.status_code == requests.codes.ok:
        with open(os.path.join(MEDIA_ROOT, 'bibliotik_upload.html'),
                  'wb') as f:
            f.write(response.content)
        raise Exception('Bibliotik does not want this. Written to media/')
    redirect_match = re.match('^https://bibliotik.me/torrents/(?P<id>\d+)$',
                              response.headers['location'])
    if not redirect_match:
        raise Exception('Could not get new torrent ID.')
    torrent_id = redirect_match.groupdict()['id']

    book_upload.bibliotik_torrent = BibliotikTorrent.get_or_create(
        bibliotik_client, torrent_id)
    book_upload.save()

    # Add the torrent to the client
    location = DownloadLocation.get_bibliotik_preferred()
    download_dir = os.path.join(location.path,
                                unicode(book_upload.bibliotik_torrent.id))
    book_path = os.path.join(download_dir, book_upload.target_filename)
    if not os.path.exists(download_dir):
        os.mkdir(download_dir)
    os.chmod(download_dir, 0777)
    shutil.copyfile(book_upload.book_data.storage.path(book_upload.book_data),
                    book_path)
    os.chmod(book_path, 0777)

    manage_bibliotik.add_bibliotik_torrent(book_upload.bibliotik_torrent.id,
                                           location=location,
                                           bibliotik_client=bibliotik_client)

    return book_upload
Пример #25
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)
        }
Пример #26
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,
    }
Пример #27
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)}
Пример #28
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: