def add_torrent(request, instance, download_location, what_id, add_to_client=True):
    w_torrent = WhatTorrent.get_or_create(request, what_id=what_id)

    with LockModelTables(TransTorrent, LogEntry):
        try:
            TransTorrent.objects.get(instance=instance, info_hash=w_torrent.info_hash)
            raise TorrentAlreadyAddedException(u"Already added.")
        except TransTorrent.DoesNotExist:
            pass

        if add_to_client:
            manager = transaction.atomic
        else:
            manager = dummy_context_manager

        with manager():
            if True:
                m_torrent = TransTorrent(
                    instance=instance, location=download_location, what_torrent=w_torrent, info_hash=w_torrent.info_hash
                )
                m_torrent.save()

                if add_to_client:
                    download_dir = os.path.join(download_location.path, unicode(w_torrent.id))
                    t_torrent = instance.client.add_torrent(
                        w_torrent.torrent_file, download_dir=download_dir, paused=False
                    )
                    t_torrent = instance.client.get_torrent(t_torrent.id, arguments=TransTorrent.sync_t_arguments)
                    norm_t_torrent(t_torrent)
                    m_torrent.sync_t_torrent(t_torrent)
                    m_torrent.sync_files()
                return m_torrent
Example #2
0
    def sync_t_torrent(self, t_torrent=None):
        if t_torrent is None:
            t_torrent = self.instance.client.get_torrent(self.torrent_id, arguments=TransTorrentBase.sync_t_arguments)
            norm_t_torrent(t_torrent)

        if not match_properties(self, t_torrent, TransTorrentBase.sync_t_props):
            copy_properties(self, t_torrent, TransTorrentBase.sync_t_props)
            self.save()
Example #3
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:
Example #4
0
    def sync_t_torrent(self, t_torrent=None):
        if t_torrent is None:
            t_torrent = self.instance.client.get_torrent(
                self.torrent_id, arguments=TransTorrentBase.sync_t_arguments)
            norm_t_torrent(t_torrent)

        if not match_properties(self, t_torrent,
                                TransTorrentBase.sync_t_props):
            copy_properties(self, t_torrent, TransTorrentBase.sync_t_props)
            self.save()
Example #5
0
 def get_t_torrents(self, arguments):
     torrents = []
     locations = DownloadLocation.objects.filter(zone=self.replica_set.zone)
     if u'downloadDir' not in arguments:
         arguments.append(u'downloadDir')
     for t in self.client.get_torrents(arguments=arguments):
         if any([l for l in locations if t.downloadDir.startswith(l.path)]):
             norm_t_torrent(t)
             torrents.append(t)
     return torrents
Example #6
0
 def get_t_torrents(self, arguments):
     torrents = []
     locations = DownloadLocation.objects.filter(zone=self.replica_set.zone)
     if u'downloadDir' not in arguments:
         arguments.append(u'downloadDir')
     for t in self.client.get_torrents(arguments=arguments):
         if any([l for l in locations if t.downloadDir.startswith(l.path)]):
             norm_t_torrent(t)
             torrents.append(t)
     return torrents
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_torrent(request,
                instance,
                download_location,
                what_id,
                add_to_client=True,
                moving=False):
    w_torrent = WhatTorrent.get_or_create(request, what_id=what_id)

    masters = list(ReplicaSet.get_what_master().transinstance_set.all())
    with LockModelTables(TransTorrent, LogEntry):
        if add_to_client and not moving:
            try:
                existing_one = TransTorrent.objects.get(
                    instance__in=masters, info_hash=w_torrent.info_hash)
                raise TorrentAlreadyAddedException(
                    u'Already added (instance={0}, new_instance={1}, info_hash={2}).'
                    .format(instance, existing_one.instance,
                            w_torrent.info_hash))
            except TransTorrent.DoesNotExist:
                pass

        if add_to_client:
            manager = transaction.atomic
        else:
            manager = dummy_context_manager

        with manager():
            if True:
                m_torrent = TransTorrent(
                    instance=instance,
                    location=download_location,
                    what_torrent=w_torrent,
                    info_hash=w_torrent.info_hash,
                )
                m_torrent.save()

                if add_to_client:
                    download_dir = os.path.join(download_location.path,
                                                unicode(w_torrent.id))
                    t_torrent = instance.client.add_torrent(
                        w_torrent.torrent_file,
                        download_dir=download_dir,
                        paused=False)
                    t_torrent = instance.client.get_torrent(
                        t_torrent.id, arguments=TransTorrent.sync_t_arguments)
                    norm_t_torrent(t_torrent)
                    m_torrent.sync_t_torrent(t_torrent)
                    m_torrent.sync_files()
                return m_torrent
Example #9
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:
def add_torrent(request, instance, download_location, what_id, add_to_client=True, moving=False):
    w_torrent = WhatTorrent.get_or_create(request, what_id=what_id)

    masters = list(ReplicaSet.get_what_master().transinstance_set.all())
    with LockModelTables(TransTorrent, LogEntry):
        if add_to_client and not moving:
            try:
                existing_one = TransTorrent.objects.get(instance__in=masters, info_hash=w_torrent.info_hash)
                raise TorrentAlreadyAddedException(u'Already added (instance={0}, new_instance={1}, info_hash={2}).'.format(
                    instance, existing_one.instance, w_torrent.info_hash))
            except TransTorrent.DoesNotExist:
                pass

        if add_to_client:
            manager = transaction.atomic
        else:
            manager = dummy_context_manager

        with manager():
            if True:
                m_torrent = TransTorrent(
                    instance=instance,
                    location=download_location,
                    what_torrent=w_torrent,
                    info_hash=w_torrent.info_hash,
                )
                m_torrent.save()

                if add_to_client:
                    download_dir = os.path.join(download_location.path, unicode(w_torrent.id))
                    t_torrent = instance.client.add_torrent(
                        w_torrent.torrent_file,
                        download_dir=download_dir,
                        paused=False
                    )
                    t_torrent = instance.client.get_torrent(t_torrent.id,
                                                            arguments=TransTorrent.sync_t_arguments)
                    norm_t_torrent(t_torrent)
                    m_torrent.sync_t_torrent(t_torrent)
                    m_torrent.sync_files()
                return m_torrent