aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcedricbonhomme <devnull@localhost>2011-10-24 23:31:11 +0200
committercedricbonhomme <devnull@localhost>2011-10-24 23:31:11 +0200
commitf5d0b9cfae4d7ec4a3253d260189e96efd2aa705 (patch)
tree001bb647637dfba32a5a7e1bf9573110a210f344
parentMinor bugfix in export.py (diff)
downloadnewspipe-f5d0b9cfae4d7ec4a3253d260189e96efd2aa705.tar.gz
newspipe-f5d0b9cfae4d7ec4a3253d260189e96efd2aa705.tar.bz2
newspipe-f5d0b9cfae4d7ec4a3253d260189e96efd2aa705.zip
Refactored export functions properly. The appropriate export function of the 'export' module is called with getattr() in pyAggr3g470r.py.
-rw-r--r--export.py103
-rwxr-xr-xpyAggr3g470r.py25
2 files changed, 79 insertions, 49 deletions
diff --git a/export.py b/export.py
index 3157415c..9883eb65 100644
--- a/export.py
+++ b/export.py
@@ -1,6 +1,38 @@
#! /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 <http://www.gnu.org/licenses/>
+
+__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;
+# - html file.
+#
+
import os
import hashlib
@@ -12,7 +44,6 @@ htmlheader = '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
'\n\t<title>pyAggr3g470r - News aggregator</title>\n' + \
'\t<link rel="stylesheet" type="text/css" href="/css/style.css" />' + \
'\n\t<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>\n' + \
- '\n\t<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>\n' + \
'</head>\n'
htmlfooter = '<p>This software is under GPLv3 license. You are welcome to copy, modify or' + \
@@ -24,6 +55,7 @@ htmlfooter = '<p>This software is under GPLv3 license. You are welcome to copy,
def export_webzine(feeds):
"""
+ Export the articles given in parameter in a simple Webzine.
"""
index = htmlheader
index += "<br />\n<ul>"
@@ -74,11 +106,13 @@ def export_webzine(feeds):
with open(utils.path + "/var/export/webzine/" + "index.html", "w") as f:
f.write(index)
-
-def exports(feeds, export_method):
- for feed in self.feeds.values():
+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/" + \
+ folder = utils.path + "/var/export/txt/" + \
utils.normalize_filename(feed.feed_title.strip().replace(':', '').lower())
try:
os.makedirs(folder)
@@ -88,34 +122,39 @@ def exports(feeds, export_method):
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)
- # Export all articles in HTML format
- if export_method == "export_HTML":
- name = os.path.normpath(folder + "/" + name + ".html")
- content = htmlheader()
- content += '\n<div style="width: 50%; overflow:hidden; text-align: justify; margin:0 auto">\n'
- content += """<h1><a href="%s">%s</a></h1><br />""" % \
- (article.article_link, article.article_title)
- content += article.article_description
- content += "</div>\n<hr />\n"
- content += htmlfooter
-
- # Export for dokuwiki
- # example: http://wiki.cedricbonhomme.org/doku.php/news-archives
- elif export_method == "export_dokuwiki":
- name = os.path.normpath(folder + "/" + name.replace(':', '-') + ".txt")
- content = "<html>"
- content += '\n<div style="width: 50%; overflow:hidden; text-align: justify; margin:0 auto">\n'
- content += """<h1><a href="%s">%s</a></h1><br />""" % \
- (article.article_link, article.article_title)
- content += article.article_description
- content += '</div>\n<hr />Generated with <a href="http://bitbucket.org/cedricbonhomme/pyaggr3g470r/">pyAggr3g470r</a>\n</html>'
-
- # Export all articles in raw text
- elif export_method == "export_TXT":
- content = "Title: " + article.article_title + "\n\n\n"
- content += utils.clear_string(article.article_description)
- name = os.path.normpath(folder + "/" + name + ".txt")
+ with open(name, "w") as f:
+ f.write(content)
+
+def export_html(feeds):
+ """
+ Export the articles given in parameter in HTML files.
+ """
+ for feed in feeds.values():
+ # creates folder for each stream
+ folder = utils.path + "/var/export/html/" + \
+ 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 + ".html")
+
+ content = htmlheader
+ content += '\n<div style="width: 50%; overflow:hidden; text-align: justify; margin:0 auto">\n'
+ content += """<h1><a href="%s">%s</a></h1><br />""" % \
+ (article.article_link, article.article_title)
+ content += article.article_description
+ content += "</div>\n<hr />\n"
+ content += htmlfooter
with open(name, "w") as f:
f.write(content) \ No newline at end of file
diff --git a/pyAggr3g470r.py b/pyAggr3g470r.py
index 25effe12..de8485a1 100755
--- a/pyAggr3g470r.py
+++ b/pyAggr3g470r.py
@@ -22,7 +22,7 @@
__author__ = "Cedric Bonhomme"
__version__ = "$Revision: 2.9 $"
__date__ = "$Date: 2010/01/29 $"
-__revision__ = "$Date: 2011/09/26 $"
+__revision__ = "$Date: 2011/10/24 $"
__copyright__ = "Copyright (c) Cedric Bonhomme"
__license__ = "GPLv3"
@@ -280,10 +280,9 @@ class Root:
# Export functions
html += "<h1>Export articles</h1>\n\n"
html += """<form method=get action="/export/"><select name="export_method">\n"""
- html += """\t<option value="export_webzine" selected='selected'>Webzine</option>\n"""
- html += """\t<option value="export_HTML" selected='selected'>HTML</option>\n"""
- html += """\t<option value="export_TXT">Text</option>\n"""
- html += """\t<option value="export_dokuwiki">DokuWiki</option>\n"""
+ html += """\t<option value="export_webzine" selected='selected'>Simple Webzine</option>\n"""
+ html += """\t<option value="export_html" selected='selected'>HTML</option>\n"""
+ html += """\t<option value="export_txt">Text</option>\n"""
html += """</select>\n\t<input type="submit" value="Export">\n</form>\n"""
html += "<hr />\n\n"
@@ -1203,20 +1202,12 @@ class Root:
drop_base.exposed = True
- #
- # Export functions
- #
def export(self, export_method):
"""
- Export articles stored in the SQLite database in text
- (raw or HTML) files.
+ Export articles currently loaded from the SQLite database with
+ the appropriate function of the 'export' module.
"""
- if export_method == "export_webzine":
- export.export_webzine(self.feeds)
- return self.management()
- else:
- export.exports(self.feeds, export_method)
-
+ getattr(export, export_method)(self.feeds)
return self.management()
export.exposed = True
@@ -1344,4 +1335,4 @@ if __name__ == '__main__':
thread_watch_base.setDaemon(True)
thread_watch_base.start()
- cherrypy.quickstart(root, "/" ,config=utils.path + "/cfg/cherrypy.cfg")
+ cherrypy.quickstart(root, "/" ,config=utils.path + "/cfg/cherrypy.cfg") \ No newline at end of file
bgstack15