summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2017-11-21 02:08:25 (GMT)
committerAssaf Gordon <assafgordon@gmail.com>2017-11-21 02:19:30 (GMT)
commit679b612b82c98920eae4e43185b5323c2870b812 (patch)
treec9653bc1bfd9103d5d16c6bf89315f408f99b063
parentd9d3bc7ea9dd9473fe4439bd63817570021c75ce (diff)
downloadfetch-web-article-679b612b82c98920eae4e43185b5323c2870b812.zip
fetch-web-article-679b612b82c98920eae4e43185b5323c2870b812.tar.gz
fetch-web-article-679b612b82c98920eae4e43185b5323c2870b812.tar.bz2
create wrapper module for easier usage
-rw-r--r--fetch_web_article/runner.py51
-rw-r--r--fetch_web_article/tests/test_fetch_web_article.py4
-rw-r--r--fetch_web_article/wrapper.py50
-rw-r--r--setup.py2
4 files changed, 54 insertions, 53 deletions
diff --git a/fetch_web_article/runner.py b/fetch_web_article/runner.py
index 09f1f62..1c462e4 100644
--- a/fetch_web_article/runner.py
+++ b/fetch_web_article/runner.py
@@ -1,16 +1,6 @@
import argparse, re, sys, os, smtplib, shutil
from .version import __version__
-from .utils import get_article, \
- save_string_to_file, \
- html_string_to_markdown, \
- get_article_header, \
- get_article_wrapped_text, \
- get_whole_page_markdown, \
- get_article_markdown, \
- download_article_images, \
- article_is_technical, \
- create_article_mime_message
-
+from .wrapper import fetch_web_article_as_mime_msg
from tempfile import mkdtemp
from getpass import getuser
from socket import gethostname
@@ -83,45 +73,6 @@ This requires a local MTA to be installed and configued (e.g. exim4).
return args
-def fetch_web_article_as_mime_msg(url=None, _from_email=None, _to_emails=None,
- subject_prefix=None, subject_suffix=None,
- tmpdir=None):
- art = get_article(url)
-
- title = art.title.lower()
- title = re.sub(r'[^-a-z0-9_]+','-',title)
-
- md_page = get_whole_page_markdown(art)
- save_string_to_file ( os.path.join(tmpdir,'page.md'), md_page )
-
- save_string_to_file ( os.path.join(tmpdir,'page.html'), art.html )
-
- md_article = get_article_markdown(art)
- save_string_to_file ( os.path.join(tmpdir, 'content.md'), md_article )
-
- txt_article = get_article_wrapped_text(art)
- save_string_to_file ( os.path.join(tmpdir, 'content.txt'), txt_article )
-
- download_article_images(tmpdir, art)
-
- msg = create_article_mime_message(tmpdir,art)
-
- if _from_email:
- msg['From'] = _from_email
- if _to_emails:
- msg['To'] = ", ".join(_to_emails)
- if subject_prefix:
- oldsbj = msg['Subject']
- del msg['Subject']
- msg['Subject'] = subject_prefix + " " + oldsbj
- if subject_suffix:
- oldsbj = msg['Subject']
- del msg['Subject']
- msg['Subject'] = oldsbj + " " + subject_suffix
-
- save_string_to_file ( os.path.join(tmpdir, 'msg.mime'), str(msg) )
-
- return msg
diff --git a/fetch_web_article/tests/test_fetch_web_article.py b/fetch_web_article/tests/test_fetch_web_article.py
index d844254..5b01dc9 100644
--- a/fetch_web_article/tests/test_fetch_web_article.py
+++ b/fetch_web_article/tests/test_fetch_web_article.py
@@ -6,8 +6,8 @@ from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
-from fetch_web_article.runner import fetch_web_article_as_mime_msg, \
- send_msg_localsmtp, detect_from_email
+from fetch_web_article.runner import send_msg_localsmtp, detect_from_email
+from wrapper import fetch_web_article_as_mime_msg
class TestFetchWebArticle(TestCase):
diff --git a/fetch_web_article/wrapper.py b/fetch_web_article/wrapper.py
new file mode 100644
index 0000000..a5535b4
--- /dev/null
+++ b/fetch_web_article/wrapper.py
@@ -0,0 +1,50 @@
+from .utils import get_article, \
+ save_string_to_file, \
+ html_string_to_markdown, \
+ get_article_header, \
+ get_article_wrapped_text, \
+ get_whole_page_markdown, \
+ get_article_markdown, \
+ download_article_images, \
+ article_is_technical, \
+ create_article_mime_message
+
+def fetch_web_article_as_mime_msg(url=None, _from_email=None, _to_emails=None,
+ subject_prefix=None, subject_suffix=None,
+ tmpdir=None):
+ art = get_article(url)
+
+ title = art.title.lower()
+ title = re.sub(r'[^-a-z0-9_]+','-',title)
+
+ md_page = get_whole_page_markdown(art)
+ save_string_to_file ( os.path.join(tmpdir,'page.md'), md_page )
+
+ save_string_to_file ( os.path.join(tmpdir,'page.html'), art.html )
+
+ md_article = get_article_markdown(art)
+ save_string_to_file ( os.path.join(tmpdir, 'content.md'), md_article )
+
+ txt_article = get_article_wrapped_text(art)
+ save_string_to_file ( os.path.join(tmpdir, 'content.txt'), txt_article )
+
+ download_article_images(tmpdir, art)
+
+ msg = create_article_mime_message(tmpdir,art)
+
+ if _from_email:
+ msg['From'] = _from_email
+ if _to_emails:
+ msg['To'] = ", ".join(_to_emails)
+ if subject_prefix:
+ oldsbj = msg['Subject']
+ del msg['Subject']
+ msg['Subject'] = subject_prefix + " " + oldsbj
+ if subject_suffix:
+ oldsbj = msg['Subject']
+ del msg['Subject']
+ msg['Subject'] = oldsbj + " " + subject_suffix
+
+ save_string_to_file ( os.path.join(tmpdir, 'msg.mime'), str(msg) )
+
+ return msg
diff --git a/setup.py b/setup.py
index 5763f5b..81cb0ac 100644
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ def readme():
return f.read()
setup(name='fetch_web_article',
- version='0.1',
+ version='0.1.1',
description='Fetch a web-page article and email it',
author='Assaf Gordon',
author_email='assafgordon@gmail.com',