Пример #1
0
def external_source_run(source_id):
    ''' use the importer specified to see if there is any new data,
        and if there is, then import it. '''

    try:
        source = ExternalSource.get(id=source_id)
    except ExternalSource.DoesNotExist:
        return 'Invalid Source', 404

    time_now = now()
    if user_session.is_admin() and request.form.get('force', 'no') == 'yes':
        flash("Update forced.")
    else:
        if source.last_checked:
            next_check = source.last_checked + timedelta(
                minutes=source.frequency)

            if next_check > time_now:
                return "Nothing to do. Last: {0}, Next: {1}, Now: {2} ".format(
                    source.last_checked, next_check, time_now)

    module = external_source_types.load(source.type)

    settings_data = json.loads(source.settings)
    new_posts = module.get_new(settings_data)

    if new_posts:
        for fresh_data in new_posts:
            post = Post(type=fresh_data.get('type', 'html'), \
                        author=source.post_as_user)
            post_type_module = post_types.load(fresh_data.get('type', 'html'))

            post.feed = source.feed

            fresh_data['active_start'] = source.current_lifetime_start()
            fresh_data['active_end'] = source.current_lifetime_end()

            post_form_intake(post, fresh_data, post_type_module)
            post.display_time = source.display_time

            if source.publish:
                post.publisher = source.post_as_user
                post.publish_date = now()
                post.published = True
            post.save()
    # else, no new posts! oh well!

    source.settings = json.dumps(settings_data)
    source.last_checked = now()
    source.save()

    return 'Done!'
Пример #2
0
def external_source_run(source_id):
    ''' use the importer specified to see if there is any new data,
        and if there is, then import it. '''

    try:
        source = ExternalSource.get(id=source_id)
    except ExternalSource.DoesNotExist:
        return 'Invalid Source', 404

    time_now = now()
    if user_session.is_admin() and request.form.get('force', 'no') == 'yes':
        flash("Update forced.")
    else:
        if source.last_checked:
            next_check = source.last_checked + timedelta(minutes=source.frequency)

            if next_check > time_now:
                return "Nothing to do. Last: {0}, Next: {1}, Now: {2} ".format(
                    source.last_checked, next_check, time_now)

    module = external_source_types.load(source.type)

    settings_data = json.loads(source.settings)
    new_posts = module.get_new(settings_data)

    if new_posts:
        for fresh_data in new_posts:
            post = Post(type=fresh_data.get('type', 'html'), \
                        author=source.post_as_user)
            post_type_module = post_types.load(fresh_data.get('type', 'html'))

            post.feed = source.feed

            fresh_data['active_start'] = source.current_lifetime_start()
            fresh_data['active_end'] = source.current_lifetime_end()

            post_form_intake(post, fresh_data, post_type_module)
            post.display_time = source.display_time

            if source.publish:
                post.publisher = source.post_as_user
                post.publish_date = now()
                post.published = True
            post.save()
    # else, no new posts! oh well!

    source.settings = json.dumps(settings_data)
    source.last_checked = now()
    source.save()

    return 'Done!'