예제 #1
0
def shared_download_media(shared_id, record_id, url):
    shared = SharedSearch(shared_id)
    record = Record.objects.get(
        id=record_id,
        manager=shared.get_source_id(),
    )
    storage = shared.get_storage()

    username = shared.shared.username
    password = shared.shared.password

    # do an authenticated request if we have a username and password
    if username and password:
        r = requests.get(url, auth=(username, password))
    else:
        r = requests.get(url)

    # turn our content into a "file-like" object
    file = StringIO(r.content)
    setattr(file, 'size', int(r.headers['content-length']))
    mimetype = r.headers['content-type']

    media = Media.objects.create(
        record=record,
        storage=storage,
        name=record.name,
        mimetype=mimetype,
    )
    media.save_file(record.name + guess_extension(mimetype), file)
예제 #2
0
def shared_download_media(job):

    log.info('shared_download_media started for %s' % job)
    jobinfo = JobInfo.objects.get(id=job.arg)

    try:
        if jobinfo.status.startswith == 'Complete':
            # job finished previously
            return
        arg = json.loads(jobinfo.arg)
        shared = SharedSearch(arg['shared_id'])
        record = Record.objects.get(id=arg['record'],
                                    manager=shared.get_source_id())
        url = arg['url']
        storage = shared.get_storage()
        file = urllib2.urlopen(url)
        setattr(file, 'size', int(file.info().get('content-length')))
        mimetype = file.info().get('content-type')
        media = Media.objects.create(
            record=record,
            storage=storage,
            name=record.name,
            mimetype=mimetype,
            )
        media.save_file(record.name + guess_extension(mimetype), file)
        jobinfo.complete('Complete', 'File downloaded')

    except Exception, ex:

        log.info('shared_download_media failed for %s (%s)' % (job, ex))
        jobinfo.update_status('Failed: %s' % ex)
예제 #3
0
def shared_download_media(job):

    logging.info('shared_download_media started for %s' % job)
    jobinfo = JobInfo.objects.get(id=job.arg)

    try:
        if jobinfo.status.startswith == 'Complete':
            # job finished previously
            return
        arg = simplejson.loads(jobinfo.arg)
        shared = SharedSearch(arg['shared_id'])
        record = Record.objects.get(id=arg['record'],
                                    manager=shared.get_source_id())
        url = arg['url']
        storage = shared.get_storage()
        file = urllib2.urlopen(url)
        setattr(file, 'size', int(file.info().get('content-length')))
        mimetype = file.info().get('content-type')
        media = Media.objects.create(
            record=record,
            storage=storage,
            name=record.name,
            mimetype=mimetype,
            )
        media.save_file(record.name + guess_extension(mimetype), file)
        jobinfo.complete('Complete', 'File downloaded')

    except Exception, ex:

        logging.info('shared_download_media failed for %s (%s)' % (job, ex))
        jobinfo.update_status('Failed: %s' % ex)
예제 #4
0
def shared_download_media(job):

    logging.info('shared_download_media started for %s' % job)
    jobinfo = JobInfo.objects.get(id=job.arg)

    try:
        if jobinfo.status.startswith == 'Complete':
            # job finished previously
            return
        arg = simplejson.loads(jobinfo.arg)
        shared = SharedSearch(arg['shared_id'])
        record = Record.objects.get(id=arg['record'],
                                    manager=shared.get_source_id())
        url = arg['url']
        storage = shared.get_storage()

        username = shared.shared.username
        password = shared.shared.password

        # do an authenticated request if we have a username and password
        if username and password:
            r = requests.get(url, auth=(username, password))
        else:
            r = requests.get(url)

        # turn our conent into a "file-like" object :)
        file = StringIO(r.content)
        setattr(file, 'size', int(r.headers['content-length']))
        mimetype = r.headers['content-type']

        media = Media.objects.create(
            record=record,
            storage=storage,
            name=record.name,
            mimetype=mimetype,
        )
        media.save_file(record.name + guess_extension(mimetype), file)
        jobinfo.complete('Complete', 'File downloaded')

    except Exception, ex:

        logging.info('shared_download_media failed for %s (%s)' % (job, ex))
        jobinfo.update_status('Failed: %s' % ex)