Beispiel #1
0
def export_story(story, sketch, story_exporter, zip_file):
    """Export a story from a sketch into a ZIP file.

    Args:
        story (timesketch.models.sketch.Story): a story object.
        sketch (timesketch.models.sketch.Sketch): a sketch object.
        story_exporter (timesketch.lib.stories.StoryExporter): an instance of
            a story exporter that can be used to export story content.
        zip_file (ZipFile): a zip file handle that can be used to write
            content to.
    """
    with story_exporter() as exporter:
        data_fetcher = story_api_fetcher.ApiDataFetcher()
        data_fetcher.set_sketch_id(sketch.id)

        exporter.set_data_fetcher(data_fetcher)
        exporter.from_string(story.content)
        exporter.set_creation_date(story.created_at.isoformat())
        if story.user:
            author = story.user.username
        else:
            author = 'System'
        exporter.set_author(author)
        exporter.set_title(story.title)

        zip_file.writestr('stories/{0:04d}_{1:s}.html'.format(
            story.id, story.title),
                          data=exporter.export_story())
Beispiel #2
0
    def _export_story(story, sketch_id, export_format='markdown'):
        """Returns a story in a format as requested in export_format.

        Args:
            story: a story object (instance of Story) that is to be exported.
            sketch_id: integer with the sketch ID.
            export_format: string with the name of the format to export the
                story to. Defaults to "markdown".

        Returns:
            The exported story in the format described. This could be a text
            or a binary, depending on the output format.
        """
        exporter_class = story_export_manager.StoryExportManager.get_exporter(
            export_format)
        if not exporter_class:
            return b''

        with exporter_class() as exporter:
            data_fetcher = story_api_fetcher.ApiDataFetcher()
            data_fetcher.set_sketch_id(sketch_id)

            exporter.set_data_fetcher(data_fetcher)
            exporter.set_title(story.title)
            exporter.set_creation_date(story.created_at.isoformat())
            exporter.set_author(story.user)
            exporter.set_exporter(current_user.username)

            exporter.from_string(story.content)
            return exporter.export_story()