From e6472738b5253aa328f8b2a4f4f2a23abc8582c2 Mon Sep 17 00:00:00 2001 From: cedricbonhomme Date: Sun, 15 Apr 2012 18:59:50 +0200 Subject: Reorganization of folders. --- source/export.py | 190 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 source/export.py (limited to 'source/export.py') diff --git a/source/export.py b/source/export.py new file mode 100644 index 00000000..a14d47c0 --- /dev/null +++ b/source/export.py @@ -0,0 +1,190 @@ +#! /usr/bin/env python +#-*- coding: utf-8 -*- + +# pyAggr3g470r - A Web based news aggregator. +# Copyright (C) 2010 Cédric Bonhomme - http://cedricbonhomme.org/ +# +# For more information : http://bitbucket.org/cedricbonhomme/pyaggr3g470r/ +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see + +__author__ = "Cedric Bonhomme" +__version__ = "$Revision: 0.1 $" +__date__ = "$Date: 2011/10/24 $" +__copyright__ = "Copyright (c) Cedric Bonhomme" +__license__ = "GPLv3" + +# +# This file contains the export functions of pyAggr3g470r. Indeed +# it is possible to export the database of articles in different formats: +# - simple HTML webzine; +# - text file; +# - ePub file; +# - PDF file. +# + +import os +import hashlib + +import utils + + +htmlheader = '\n' + \ + '' + \ + '\n\tpyAggr3g470r - News aggregator\n' + \ + '\t' + \ + '\n\t\n' + \ + '\n' + +htmlfooter = '

This software is under GPLv3 license. You are welcome to copy, modify or' + \ + ' redistribute the source code according to the' + \ + ' GPLv3 license.

\n' + \ + '\n' + + + +def export_html(feeds): + """ + Export the articles given in parameter in a simple Webzine. + """ + index = htmlheader + index += "
\n\n
" + index += htmlfooter + with open(utils.path + "/var/export/webzine/" + "index.html", "w") as f: + f.write(index) + +def export_txt(feeds): + """ + Export the articles given in parameter in text files. + """ + for feed in feeds.values(): + # creates folder for each stream + folder = utils.path + "/var/export/txt/" + \ + utils.normalize_filename(feed.feed_title.strip().replace(':', '').lower()) + try: + os.makedirs(folder) + except OSError: + # directories already exists (not a problem) + pass + + for article in feed.articles.values(): + name = article.article_date.strip().replace(' ', '_') + name = os.path.normpath(folder + "/" + name + ".txt") + + content = "Title: " + article.article_title + "\n\n\n" + content += utils.clear_string(article.article_description) + + with open(name, "w") as f: + f.write(content) + +def export_epub(feeds): + """ + Export the articles given in parameter in ePub files. + """ + from epub import ez_epub + for feed in feeds.values(): + # creates folder for each stream + folder = utils.path + "/var/export/epub/" + \ + utils.normalize_filename(feed.feed_title.strip().replace(':', '').lower()) + try: + os.makedirs(folder) + except OSError: + # directories already exists (not a problem) + pass + + for article in feed.articles.values(): + name = article.article_date.strip().replace(' ', '_') + name = os.path.normpath(folder + "/" + name + ".epub") + + section = ez_epub.Section() + section.title = article.article_title.decode('utf-8') + section.paragraphs = [utils.clear_string(article.article_description).decode('utf-8')] + ez_epub.makeBook(article.article_title.decode('utf-8'), [feed.feed_title.decode('utf-8')], [section], \ + name, lang='en-US', cover=None) + +def export_pdf(feeds): + """ + Export the articles given in parameter in PDF files. + """ + from xhtml2pdf import pisa + import cStringIO as StringIO + for feed in feeds.values(): + # creates folder for each stream + folder = utils.path + "/var/export/pdf/" + \ + utils.normalize_filename(feed.feed_title.strip().replace(':', '').lower()) + try: + os.makedirs(folder) + except OSError: + # directories already exists (not a problem) + pass + + for article in feed.articles.values(): + name = article.article_date.strip().replace(' ', '_') + name = os.path.normpath(folder + "/" + name + ".pdf") + + content = htmlheader + content += '\n
\n' + content += """

%s


""" % \ + (article.article_link, article.article_title) + content += article.article_description + content += "
\n
\n" + content += htmlfooter + + try: + pdf = pisa.CreatePDF(StringIO.StringIO(content), file(name, "wb")) + except: + pass \ No newline at end of file -- cgit