From 50b6b45459e9df4780c008fd93ddfda4bbae0487 Mon Sep 17 00:00:00 2001 From: stubbfel Date: Wed, 29 Nov 2017 00:12:09 +0100 Subject: [PATCH] init --- news2kindle.sh | 4 ++ python/dn2k/src/.gitignore | 1 + python/dn2k/src/ConvertFeed.py | 38 ++++++++++++++ python/dn2k/src/SendFeed.py | 15 ++++++ python/dn2k/test/ebookconvert/.gitignore | 1 + python/dn2k/test/ebookconvert/mobi/.gitignore | 1 + .../dn2k/test/ebookconvert/recipe/moz.recipe | 14 ++++++ .../test/ebookconvert/testEbookConvert.py | 49 +++++++++++++++++++ python/dn2k/test/ebookconvert/testSendFeed.py | 34 +++++++++++++ 9 files changed, 157 insertions(+) create mode 100755 news2kindle.sh create mode 100644 python/dn2k/src/.gitignore create mode 100644 python/dn2k/src/ConvertFeed.py create mode 100644 python/dn2k/src/SendFeed.py create mode 100644 python/dn2k/test/ebookconvert/.gitignore create mode 100644 python/dn2k/test/ebookconvert/mobi/.gitignore create mode 100644 python/dn2k/test/ebookconvert/recipe/moz.recipe create mode 100644 python/dn2k/test/ebookconvert/testEbookConvert.py create mode 100644 python/dn2k/test/ebookconvert/testSendFeed.py diff --git a/news2kindle.sh b/news2kindle.sh new file mode 100755 index 00000000..64802d8a --- /dev/null +++ b/news2kindle.sh @@ -0,0 +1,4 @@ +#!/bin/bash +NAME=$1 +xvfb-run ebook-convert recipe/$NAME.recipe mobi/$NAME.mobi --output-profile kindle +calibre-smtp -a mobi/$NAME.mobi -s $NAME stubbfel@b-tu.de stubbfel@kindle.com "$NAME" diff --git a/python/dn2k/src/.gitignore b/python/dn2k/src/.gitignore new file mode 100644 index 00000000..0d20b648 --- /dev/null +++ b/python/dn2k/src/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/python/dn2k/src/ConvertFeed.py b/python/dn2k/src/ConvertFeed.py new file mode 100644 index 00000000..a5fb130b --- /dev/null +++ b/python/dn2k/src/ConvertFeed.py @@ -0,0 +1,38 @@ +__author__ = 'dev' +import subprocess +import os +import glob + +EBOOK_CONVERT_CMD = "ebook-convert" +OUTPUT_PROFIL_OPTION = "--output-profile" +DEFAULT_OUTPUT_PROFIL = "kindle" +DEFAULT_FEED_MOBI_FILE_EXTENSION = ".mobi" +DEFAULT_FEED_RECIPE_FILE_EXTENSION = ".recipe" + + +def convert_feed(recipe_path, dst_path, profile=DEFAULT_OUTPUT_PROFIL): + recipe_path_abs = os.path.abspath(recipe_path) + dst_path_abs = os.path.abspath(dst_path) + + if not os.path.exists(recipe_path_abs): + raise os.error.filename + + if os.path.exists(dst_path_abs): + os.remove(dst_path_abs) + + subprocess.call([EBOOK_CONVERT_CMD ,recipe_path_abs, dst_path_abs, OUTPUT_PROFIL_OPTION, profile]) + + +def convert_all_feed(recipe_folder, dst_folder, profile=DEFAULT_OUTPUT_PROFIL, recipe_extension=DEFAULT_FEED_RECIPE_FILE_EXTENSION, mobi_extension=DEFAULT_FEED_MOBI_FILE_EXTENSION): + recipe_folder_path_abs = os.path.abspath(recipe_folder) + dst_folder_path_abs = os.path.abspath(dst_folder) + + if not os.path.exists(recipe_folder_path_abs) or not os.path.exists(dst_folder_path_abs): + raise os.error.filename + + for file in glob.glob(os.path.join(recipe_folder_path_abs, "*" + recipe_extension)): + file_base_name = os.path.splitext(os.path.basename(file))[0] + dst_path_abs = os.path.join(dst_folder_path_abs, file_base_name + mobi_extension) + if os.path.exists(dst_path_abs): + os.remove(dst_path_abs) + convert_feed(file, dst_path_abs, profile) \ No newline at end of file diff --git a/python/dn2k/src/SendFeed.py b/python/dn2k/src/SendFeed.py new file mode 100644 index 00000000..4b4a7da9 --- /dev/null +++ b/python/dn2k/src/SendFeed.py @@ -0,0 +1,15 @@ +__author__ = 'dev' + +import subprocess + + +EBOOK_SEND_CMD = "calibre-smtp" + + +def send_feed(mobi_path, feed_name, sender_mail_address, dst_mail_address): + subprocess.call([EBOOK_SEND_CMD, "-a", mobi_path, "-s", feed_name, sender_mail_address, dst_mail_address, feed_name]) + + +def send_feeds(feedDict, sender_mail_address, dst_mail_address): + for feed_name, mobi_path in feedDict.items(): + send_feed(mobi_path, feed_name, sender_mail_address, dst_mail_address) \ No newline at end of file diff --git a/python/dn2k/test/ebookconvert/.gitignore b/python/dn2k/test/ebookconvert/.gitignore new file mode 100644 index 00000000..0d20b648 --- /dev/null +++ b/python/dn2k/test/ebookconvert/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/python/dn2k/test/ebookconvert/mobi/.gitignore b/python/dn2k/test/ebookconvert/mobi/.gitignore new file mode 100644 index 00000000..3be42abb --- /dev/null +++ b/python/dn2k/test/ebookconvert/mobi/.gitignore @@ -0,0 +1 @@ +/moz.mobi diff --git a/python/dn2k/test/ebookconvert/recipe/moz.recipe b/python/dn2k/test/ebookconvert/recipe/moz.recipe new file mode 100644 index 00000000..3c1d1c89 --- /dev/null +++ b/python/dn2k/test/ebookconvert/recipe/moz.recipe @@ -0,0 +1,14 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +from __future__ import unicode_literals, division, absolute_import, print_function +from calibre.web.feeds.news import BasicNewsRecipe + +class AdvancedUserRecipe1422191832(BasicNewsRecipe): + title = 'moz' + oldest_article = 7 + max_articles_per_feed = 100 + auto_cleanup = True + + feeds = [ + ('moz-brandenburg', 'http://www.moz.de/nc/service/weiteres/rss-feeds-auf-mozde/?type=578&tx_rsmretrescorss_pi1%5Bshow%5D=1') + ] diff --git a/python/dn2k/test/ebookconvert/testEbookConvert.py b/python/dn2k/test/ebookconvert/testEbookConvert.py new file mode 100644 index 00000000..fdcd7f14 --- /dev/null +++ b/python/dn2k/test/ebookconvert/testEbookConvert.py @@ -0,0 +1,49 @@ +__author__ = 'dev' + +import unittest +import os +import subprocess +import ConvertFeed + +ABS_CUR = os.path.abspath(os.path.curdir) +TEST_FEED_NAME = "moz" +TEST_FEED_RECIPE_FOLDER = os.path.join(ABS_CUR, "recipe") +TEST_FEED_RECIPE_FILE_EXTENSION = ".recipe" +TEST_FEED_RECIPE = os.path.join(TEST_FEED_RECIPE_FOLDER, TEST_FEED_NAME + TEST_FEED_RECIPE_FILE_EXTENSION) + +TEST_FEED_MOBI_FOLDER = os.path.join(ABS_CUR, "mobi") + +TEST_FEED_MOBI_FILE_EXTENSION = ".mobi" +TEST_FEED_MOBI = os.path.join(TEST_FEED_MOBI_FOLDER, TEST_FEED_NAME + TEST_FEED_MOBI_FILE_EXTENSION) + +TEST_OUTPUT_PROFIL = "kindle" +TEST_OUTPUT_PROFIL_OPTION = "--output-profile" + +TEST_ECONVERT_CMD = "ebook-convert" + +class TestEbookConvert(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_ebook_convert(self): + + if os.path.exists(TEST_FEED_MOBI): + os.remove(TEST_FEED_MOBI) + + ConvertFeed.convert_feed(TEST_FEED_RECIPE, TEST_FEED_MOBI) + self.assertTrue(os.path.exists(TEST_FEED_MOBI)) + + def test_ebook_convert_all_feed(self): + + if os.path.exists(TEST_FEED_MOBI): + os.remove(TEST_FEED_MOBI) + + ConvertFeed.convert_all_feed(TEST_FEED_RECIPE_FOLDER , TEST_FEED_MOBI_FOLDER) + self.assertTrue(os.path.exists(TEST_FEED_MOBI)) + +if __name__ == '__main__': + unittest.main() diff --git a/python/dn2k/test/ebookconvert/testSendFeed.py b/python/dn2k/test/ebookconvert/testSendFeed.py new file mode 100644 index 00000000..6967a1f9 --- /dev/null +++ b/python/dn2k/test/ebookconvert/testSendFeed.py @@ -0,0 +1,34 @@ +__author__ = 'dev' + +import unittest +import os +import SendFeed + +ABS_CUR = os.path.abspath(os.path.curdir) +TEST_FEED_NAME = "moz" +TEST_FEED_MOBI_FOLDER = os.path.join(ABS_CUR, "mobi") +TEST_FEED_MOBI_FILE_EXTENSION = ".mobi" +TEST_FEED_MOBI = os.path.join(TEST_FEED_MOBI_FOLDER, TEST_FEED_NAME + TEST_FEED_MOBI_FILE_EXTENSION) +TEST_SENDER_MAIL = "stubbfel@b-tu.de" +TEST_DST_MAIL = "stubbfel@kindle.com" +TEST_FEED_DICT = {TEST_FEED_NAME: TEST_FEED_MOBI, TEST_FEED_NAME+"2": TEST_FEED_MOBI} + +class TestSendFeed(unittest.TestCase): + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_send_feed(self): + SendFeed.send_feed(TEST_FEED_MOBI, TEST_FEED_NAME, TEST_SENDER_MAIL, TEST_DST_MAIL) + self.assertTrue(True) + + def test_send_feeds(self): + SendFeed.send_feeds(TEST_FEED_DICT, TEST_SENDER_MAIL, TEST_DST_MAIL) + self.assertTrue(True) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file