From 26ed2d31810681b14038bef1e5ae6dc700dad677 Mon Sep 17 00:00:00 2001 From: Borya Date: Sat, 9 Nov 2019 00:52:56 +0300 Subject: [PATCH 01/10] setup.py was added --- .idea/workspace.xml | 75 ++++++++++++++++++++++++++++++++++++++++ rss_reader/__init__.py | 0 rss_reader/rss_reader.py | 12 +++++++ setup.py | 11 ++++++ 4 files changed, 98 insertions(+) create mode 100644 .idea/workspace.xml create mode 100644 rss_reader/__init__.py create mode 100644 rss_reader/rss_reader.py create mode 100644 setup.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..485267f --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + 1573146497570 + + + + + + + + + + + + \ No newline at end of file diff --git a/rss_reader/__init__.py b/rss_reader/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py new file mode 100644 index 0000000..735b89d --- /dev/null +++ b/rss_reader/rss_reader.py @@ -0,0 +1,12 @@ +import argparse + + +def main(): + parser = argparse.ArgumentParser(description='T.') + parser + args = parser.parse_args() + print(args) + + +if __name__ == "__main__": + main() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..96eb32a --- /dev/null +++ b/setup.py @@ -0,0 +1,11 @@ +import setuptools + +setuptools.setup( + name='rss_reader', + version='1.0', + author='Boris Dashko', + author_email='borya.dashko@gmail.com', + url='https://github.com/BoryaD/PythonHomework/tree/FinalTask', + packages=setuptools.find_packages(), + python_requires='>=3.8', +) \ No newline at end of file From 8e891cf1e5f687f5c225e90f8489ea53733952c9 Mon Sep 17 00:00:00 2001 From: Borya Date: Sun, 10 Nov 2019 18:32:15 +0300 Subject: [PATCH 02/10] Basic functional of RSS-parser was added --- .gitignore | 2 ++ .idea/workspace.xml | 5 ++++- rss_reader/rss_reader.py | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 894a44c..f82a459 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ venv.bak/ # mypy .mypy_cache/ + +.idea/ diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 485267f..29bc74e 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,9 @@ - + + + diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index 735b89d..a5f2913 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -1,12 +1,41 @@ import argparse +import feedparser +import html +import re + + +version = '1.0' + + +def clean_from_tags(text_with_tags): + return re.sub('<.*?>', '', text_with_tags) + + +def show_news(entries, limit): + real_limit = len(entries) + if limit > 0: + if limit < len(entries): + real_limit = limit + for i in range(real_limit): + print("Title:", html.unescape(entries[i].title)) + print("Data:", html.unescape(entries[i].published)) + print("Link:", entries[i].link, "\n") + print("Description:", clean_from_tags(html.unescape(entries[i].description)), "\n") def main(): - parser = argparse.ArgumentParser(description='T.') - parser + parser = argparse.ArgumentParser(description='Python RSS-reader') + parser.add_argument("URL", type=str, help='RSS URL') + parser.add_argument("--version", help="Print version info", action="version", version=version) + parser.add_argument("--json", help="Print result as JSON in stdout", action="store_true") + parser.add_argument("-V", "--verbose", help="Outputs verbose status messages", action="store_true") + parser.add_argument("-L", "--limit", help="Limit news topics if this parameter is provided", type=int, default=0) args = parser.parse_args() - print(args) + feeds = feedparser.parse(args.URL) + print("\n", "Feed: ", feeds.feed.title, "\n") + show_news(feeds.entries, args.limit) if __name__ == "__main__": main() + From 1b6206c57b703da0a2c636164ca5fda4510132b2 Mon Sep 17 00:00:00 2001 From: Borya Date: Sun, 10 Nov 2019 18:41:19 +0300 Subject: [PATCH 03/10] .Idea was daleted --- .idea/workspace.xml | 78 --------------------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 29bc74e..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,78 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1573146497570 - - - - - - - - - - - - \ No newline at end of file From f65bcba858f60f309f263f25844dcc8d27c58f61 Mon Sep 17 00:00:00 2001 From: Borya Date: Sun, 17 Nov 2019 23:59:55 +0300 Subject: [PATCH 04/10] First iteration is ready. Logger was added. -- json was added. News.py was added --- rss_reader/news.py | 69 ++++++++++++++++++++++++++++++++++++++++ rss_reader/rss_reader.py | 58 +++++++++++++++++++++------------ 2 files changed, 106 insertions(+), 21 deletions(-) create mode 100644 rss_reader/news.py diff --git a/rss_reader/news.py b/rss_reader/news.py new file mode 100644 index 0000000..e857790 --- /dev/null +++ b/rss_reader/news.py @@ -0,0 +1,69 @@ +import html +import re +import json + +class News: + """This class contains news and methods of work whit news""" + def __init__(self, feeds_dict, limit): + + self.news = dict() + self.all_news = list() + self.name_of_source = feeds_dict.feed.title + + real_limit = len(feeds_dict.entries) + if limit > 0: + if limit < len(feeds_dict.entries): + real_limit = limit + + for i in range(real_limit): + self.news['title'] = html.unescape(feeds_dict.entries[i].title) + self.news['date'] = html.unescape(feeds_dict.entries[i].published) + self.news['link'] = html.unescape(feeds_dict.entries[i].link) + self.news['description'] = self.clean_from_tags(html.unescape(feeds_dict.entries[i].description)) + + if feeds_dict.entries[i].setdefault("media_content", None): + media = list() + if feeds_dict.entries[i].media_content: + for elem in feeds_dict.entries[i].media_content: + media.append({'url': elem.setdefault('url', None), 'type':elem.setdefault('type', None)}) + self.news['media'] = media.copy() + + links = list() + if feeds_dict.entries[i].links: + for elem in feeds_dict.entries[i].links: + links.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) + self.news['links'] = links.copy() + + self.all_news.append(self.news.copy()) + + @staticmethod + def clean_from_tags(text_with_tags): + """This function delete tags from string""" + return re.sub('<.*?>', '', text_with_tags) + + def print(self): + """This function print news to stdout in readable format""" + print(f" Source: {self.name_of_source}\n") + for elem in self.all_news: + print("Title: ", elem['title']) + print("Date: ", elem['date']) + print("Link: ", elem['link']) + print(f"Description: {elem['description']}\n") + + j = 1 + print("Links: ") + for link in elem['links']: + print(f'[{j}] {link["url"]} ({link["type"]})') + j = j + 1 + + if elem.setdefault('media', None): + print("Media: ") + for media in elem['media']: + print(f'[{j}] {media["url"]} ({media["type"]})') + j = j + 1 + print("\n") + + def to_json(self): + """This function returns JSON-string with news""" + return json.dumps({'Source:': self.name_of_source, 'Feeds': self.all_news}, ensure_ascii=False).encode('utf8') + diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index a5f2913..e8b4ced 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -1,29 +1,21 @@ import argparse import feedparser -import html -import re - +from news import News +import logging +import sys version = '1.0' -def clean_from_tags(text_with_tags): - return re.sub('<.*?>', '', text_with_tags) - - -def show_news(entries, limit): - real_limit = len(entries) - if limit > 0: - if limit < len(entries): - real_limit = limit - for i in range(real_limit): - print("Title:", html.unescape(entries[i].title)) - print("Data:", html.unescape(entries[i].published)) - print("Link:", entries[i].link, "\n") - print("Description:", clean_from_tags(html.unescape(entries[i].description)), "\n") - - def main(): + """Main function of program""" + logger = logging.getLogger('rss_reader') + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + file_handler = logging.FileHandler('rss_reader_logs.log') + file_handler.setLevel(logging.INFO) + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + parser = argparse.ArgumentParser(description='Python RSS-reader') parser.add_argument("URL", type=str, help='RSS URL') parser.add_argument("--version", help="Print version info", action="version", version=version) @@ -31,9 +23,33 @@ def main(): parser.add_argument("-V", "--verbose", help="Outputs verbose status messages", action="store_true") parser.add_argument("-L", "--limit", help="Limit news topics if this parameter is provided", type=int, default=0) args = parser.parse_args() + + if args.verbose: + stdout_handler = logging.StreamHandler(sys.stdout) + stdout_handler.setFormatter(formatter) + stdout_handler.setLevel(logging.INFO) + logger.addHandler(stdout_handler) feeds = feedparser.parse(args.URL) - print("\n", "Feed: ", feeds.feed.title, "\n") - show_news(feeds.entries, args.limit) + + if feeds.bozo: + logger.error("Feed is not well-formed XML") + else: + logger.info("The XML file with news is received and correct") + + news = News(feeds, args.limit) + logger.info("News is parsed") + + if args.json: + print(news.to_json().decode()) + logger.info("News is displayed in stdout in a json format") + else: + news.print() + logger.info("News is displayed in stdout in a readability format") + + logger.info("Program is over") + + + if __name__ == "__main__": From 7b1dff1deb9c19da7d0101c9c063eb07aac8fe67 Mon Sep 17 00:00:00 2001 From: Borya Date: Tue, 26 Nov 2019 18:07:13 +0300 Subject: [PATCH 05/10] Third iteration is ready. Bug fixed with setuptool Created new class Cache that include all functions that works witch cache file --- .gitignore | 2 ++ requirements.txt | 1 + rss_reader/__main__.py | 4 +++ rss_reader/cache.py | 66 ++++++++++++++++++++++++++++++++++ rss_reader/news.py | 78 +++++++++++++++++++++++++++++++++------- rss_reader/rss_reader.py | 51 +++++++++++++++----------- setup.py | 11 +++++- 7 files changed, 179 insertions(+), 34 deletions(-) create mode 100644 requirements.txt create mode 100644 rss_reader/__main__.py create mode 100644 rss_reader/cache.py diff --git a/.gitignore b/.gitignore index f82a459..6f0fd41 100644 --- a/.gitignore +++ b/.gitignore @@ -104,3 +104,5 @@ venv.bak/ .mypy_cache/ .idea/ + +*.db diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cb37aea --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +feedparser~=5.2 \ No newline at end of file diff --git a/rss_reader/__main__.py b/rss_reader/__main__.py new file mode 100644 index 0000000..51a024b --- /dev/null +++ b/rss_reader/__main__.py @@ -0,0 +1,4 @@ +from .rss_reader import main + +if __name__ == '__main__': + main() diff --git a/rss_reader/cache.py b/rss_reader/cache.py new file mode 100644 index 0000000..6f02961 --- /dev/null +++ b/rss_reader/cache.py @@ -0,0 +1,66 @@ +import sqlite3 +import logging + + +file_path = 'cache.db' +class Cache: + """"This class contains news and methods of work whit cache""" + cursor = None + conn = None + + def __init__(self): + """This method initialize cursor to database""" + if self.cursor: + logger = logging.getLogger('rss_reader') + logger.error("This is singleton class. Use get_cursor") + exit() + Cache.conn = sqlite3.connect(file_path) + Cache.cursor = Cache.conn.cursor() + Cache.cursor.execute('''CREATE TABLE IF NOT EXISTS news(id INTEGER PRIMARY KEY, + title text, pub_date_key numeric, pub_date text, link text, description text, UNIQUE(link))''') + Cache.cursor.execute('''CREATE TABLE IF NOT EXISTS links( id INTEGER PRIMARY KEY, + link text, news numeric)''') + Cache.cursor.execute('''CREATE TABLE IF NOT EXISTS media( id INTEGER PRIMARY KEY, + link text, news numeric)''') + + @staticmethod + def get_cursor(): + """Static access method. """ + if Cache.cursor is None: + Cache() + return Cache.cursor + + @staticmethod + def commit(): + """This method commit to database database""" + return Cache.conn.commit() + + @staticmethod + def close(): + """This method close connection to database""" + return Cache.conn.close() + + @staticmethod + def print_news(date): + """This method print news to std from selected date to database""" + Cache.get_cursor() + Cache.cursor.execute('''SELECT * FROM news WHERE pub_date_key = ?''', (date,)) + news = Cache.cursor.fetchall() + if len(news) == 0: + return 1 + for elem in news: + print('\nTitle: ', elem[1]) + print('Date: ', elem[3]) + print('Link: ', elem[4]) + print(f'Description: {elem[5]}\n') + Cache.cursor.execute('''SELECT * FROM links WHERE news= ?''', (elem[0],)) + links = Cache.cursor.fetchall() + i = 1 + for link in links: + print(f'Link[{i}]: ', link[1]) + i = i + 1 + Cache.cursor.execute('''SELECT * FROM media WHERE news= ?''', (elem[0],)) + links = Cache.cursor.fetchall() + for link in links: + print(f'Link[{i}]: ', link[1]) + i = i + 1 diff --git a/rss_reader/news.py b/rss_reader/news.py index e857790..7b660fc 100644 --- a/rss_reader/news.py +++ b/rss_reader/news.py @@ -1,40 +1,95 @@ import html import re import json +import logging +from .cache import Cache + class News: """This class contains news and methods of work whit news""" + http_header = 'http' + img_description = 'image / ?' def __init__(self, feeds_dict, limit): + logger = logging.getLogger('rss_reader') + formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') + file_handler = logging.FileHandler('rss_reader_logs.log') + file_handler.setFormatter(formatter) + logger.addHandler(file_handler) + logger.setLevel(logging.INFO) self.news = dict() self.all_news = list() - self.name_of_source = feeds_dict.feed.title + + self.name_of_source = feeds_dict.feed['title'] real_limit = len(feeds_dict.entries) if limit > 0: if limit < len(feeds_dict.entries): real_limit = limit + cursor = Cache.get_cursor() + for i in range(real_limit): + list_to_cache = list() self.news['title'] = html.unescape(feeds_dict.entries[i].title) self.news['date'] = html.unescape(feeds_dict.entries[i].published) self.news['link'] = html.unescape(feeds_dict.entries[i].link) self.news['description'] = self.clean_from_tags(html.unescape(feeds_dict.entries[i].description)) - if feeds_dict.entries[i].setdefault("media_content", None): + list_to_cache.append(self.news['title']) + date_dict = feeds_dict.entries[i].published_parsed + date_str = str(date_dict.tm_year) + str(date_dict.tm_mon) + str(date_dict.tm_mday) + list_to_cache.append(date_str) + list_to_cache.append(self.news['date']) + list_to_cache.append(self.news['link']) + list_to_cache.append(self.news['description']) + + cursor.execute('''INSERT or IGNORE INTO news (title, pub_date_key, pub_date, link, description) VALUES(?,?,?,?,?)''', + list_to_cache) + ids = cursor.lastrowid + Cache.commit() + list_to_cache.clear() + list_to_links = list() + + if feeds_dict.entries[i].setdefault('media_content', None): media = list() if feeds_dict.entries[i].media_content: for elem in feeds_dict.entries[i].media_content: - media.append({'url': elem.setdefault('url', None), 'type':elem.setdefault('type', None)}) - self.news['media'] = media.copy() + if elem['url'].rfind(self.http_header, 0, len(elem['url'])) > 0: + links = elem['url'].split(self.http_header) + j = 1 + while j < len(links): + media.append({'url': self.http_header + links[j], 'type': self.img_description}) + list_to_links.append(self.http_header + links[j]) + list_to_links.append(ids) + cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', + list_to_links) + list_to_links.clear() + j = j + 1 + Cache.commit() + else: + media.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) + list_to_links.append(elem.setdefault('url', None)) + list_to_links.append(ids) + cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', list_to_links) + list_to_links.clear() + Cache.commit() + self.news['media'] = media.copy() + else: + self.news['media'] = '' links = list() if feeds_dict.entries[i].links: for elem in feeds_dict.entries[i].links: links.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) + list_to_links.append(elem.setdefault('url', None)) + list_to_links.append(ids) + cursor.execute('''INSERT or IGNORE INTO links (link, news) VALUES(?,?)''', list_to_links) + list_to_links.clear() self.news['links'] = links.copy() - self.all_news.append(self.news.copy()) + Cache.commit() + Cache.close() @staticmethod def clean_from_tags(text_with_tags): @@ -43,15 +98,15 @@ def clean_from_tags(text_with_tags): def print(self): """This function print news to stdout in readable format""" - print(f" Source: {self.name_of_source}\n") + print(f'Source: {self.name_of_source}\n') for elem in self.all_news: - print("Title: ", elem['title']) - print("Date: ", elem['date']) - print("Link: ", elem['link']) + print('Title: ', elem['title']) + print('Date: ', elem['date']) + print('Link: ', elem['link']) print(f"Description: {elem['description']}\n") j = 1 - print("Links: ") + print('Links: ') for link in elem['links']: print(f'[{j}] {link["url"]} ({link["type"]})') j = j + 1 @@ -61,9 +116,8 @@ def print(self): for media in elem['media']: print(f'[{j}] {media["url"]} ({media["type"]})') j = j + 1 - print("\n") + print('\n') def to_json(self): """This function returns JSON-string with news""" return json.dumps({'Source:': self.name_of_source, 'Feeds': self.all_news}, ensure_ascii=False).encode('utf8') - diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index e8b4ced..b30ad6b 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -1,57 +1,66 @@ import argparse import feedparser -from news import News +from .news import News +from .cache import Cache import logging import sys -version = '1.0' + +version = '1.2' def main(): """Main function of program""" + logger = logging.getLogger('rss_reader') formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') file_handler = logging.FileHandler('rss_reader_logs.log') - file_handler.setLevel(logging.INFO) file_handler.setFormatter(formatter) logger.addHandler(file_handler) + logger.setLevel(logging.INFO) parser = argparse.ArgumentParser(description='Python RSS-reader') - parser.add_argument("URL", type=str, help='RSS URL') - parser.add_argument("--version", help="Print version info", action="version", version=version) - parser.add_argument("--json", help="Print result as JSON in stdout", action="store_true") - parser.add_argument("-V", "--verbose", help="Outputs verbose status messages", action="store_true") - parser.add_argument("-L", "--limit", help="Limit news topics if this parameter is provided", type=int, default=0) + parser.add_argument('URL', type=str, help='RSS URL') + parser.add_argument('--version', help='Print version info', action='version', version=version) + parser.add_argument('--json', help='Print result as JSON in stdout', action='store_true') + parser.add_argument('-V', '--verbose', help='Outputs verbose status messages', action='store_true') + parser.add_argument('-L', '--limit', help='Limit news topics if this parameter is provided', type=int, default=0) + parser.add_argument('--date', help='Find news in cache if this parameter is provided', type=int, default=0) args = parser.parse_args() if args.verbose: stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(formatter) - stdout_handler.setLevel(logging.INFO) logger.addHandler(stdout_handler) feeds = feedparser.parse(args.URL) + if args.date: + logger.info('Starting to read from cache') + state = Cache.print_news(args.date) + if state == 1: + print(f'''There are not exist news with date of publication at {args.date} + \nMake sure that your format date in %Y%m%d', file=sys.stderr''') + else: + logger.info('News from cache ware read') + exit() + if feeds.bozo: - logger.error("Feed is not well-formed XML") + logger.error('Feed is not well-formed XML') else: - logger.info("The XML file with news is received and correct") + logger.info('The XML file with news is received and correct') news = News(feeds, args.limit) - logger.info("News is parsed") + logger.info('News is parsed') if args.json: print(news.to_json().decode()) - logger.info("News is displayed in stdout in a json format") + logger.info('News is displayed in stdout in a json format') else: news.print() - logger.info("News is displayed in stdout in a readability format") - - logger.info("Program is over") - - - + logger.info('News is displayed in stdout in a readability format') + logger.info('Program is over') -if __name__ == "__main__": - main() +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/setup.py b/setup.py index 96eb32a..3fa41a8 100644 --- a/setup.py +++ b/setup.py @@ -1,11 +1,20 @@ import setuptools +with open('requirements.txt') as f: + required = f.read().splitlines() + setuptools.setup( name='rss_reader', - version='1.0', + version='1.1', author='Boris Dashko', author_email='borya.dashko@gmail.com', url='https://github.com/BoryaD/PythonHomework/tree/FinalTask', packages=setuptools.find_packages(), python_requires='>=3.8', + install_requires=required, + entry_points={ + 'console_scripts': [ + 'rss-reader = rss_reader.rss_reader:main', + ], + }, ) \ No newline at end of file From a7ed9df8b25151f1da3300d723f86d4672ff5030 Mon Sep 17 00:00:00 2001 From: Borya Date: Thu, 28 Nov 2019 03:11:25 +0300 Subject: [PATCH 06/10] Added support of creation of html version of news. --- ac.html | 4 ++++ rss_reader/cache.py | 2 ++ rss_reader/news.py | 24 ++++++++++++++++++++++++ rss_reader/rss_reader.py | 15 ++++++++++++--- setup.py | 2 +- 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 ac.html diff --git a/ac.html b/ac.html new file mode 100644 index 0000000..ace09f4 --- /dev/null +++ b/ac.html @@ -0,0 +1,4 @@ + +Yahoo News - Latest News & Headlines + +

Kushner named Trump’s border-wall czar — along with practically everything else in government

Date of posting: Tue, 26 Nov 2019 17:33:14 -0500

President Trump has recently tasked his son-in-law — whose to-do list already includes brokering peace in the Middle East, leading U.S. trade policy, reorganizing the entire U.S. government and reforming the criminal justice system — with overseeing the construction of his border wall ahead of the 2020 election. 

Link to source



Then and now: Swiss glacier photos show impact of global warming

Date of posting: Tue, 26 Nov 2019 13:58:47 -0500

A collection of images — showing photos of modern-day mountain landscapes next to archive shots of the same scenes decades earlier — reveals the dramatic change.

Link to source



TSA officers find high-capacity gun magazines hidden in an infant toy at Orlando airport

Date of posting: Wed, 27 Nov 2019 14:10:34 -0500

TSA revealed officers discovered two high capacity magazines in an infant's toy at the Orlando International Airport.

Link to source



Utah banning ‘conversion therapy’ with Mormon church backing

Date of posting: Wed, 27 Nov 2019 10:58:44 -0500

Utah is on its way to becoming the 19th state to ban the discredited practice of conversion therapy in January after state officials formed a proposal that has the support of the influential Church of a Jesus Christ of Latter-day Saints. Republican Gov. Gary Herbert announced Tuesday night that church leaders back a regulatory rule his office helped craft after legislative efforts for a ban on the therapy failed earlier this year. The faith known widely as the Mormon church opposed a previous version of the rule because it wanted assurances that church leaders and members who are therapists would be allowed to provide spiritual counseling for parishioners or families — which were included in the latest conversion therapy ban plan.

Link to source



\ No newline at end of file diff --git a/rss_reader/cache.py b/rss_reader/cache.py index 6f02961..6b822de 100644 --- a/rss_reader/cache.py +++ b/rss_reader/cache.py @@ -3,6 +3,8 @@ file_path = 'cache.db' + + class Cache: """"This class contains news and methods of work whit cache""" cursor = None diff --git a/rss_reader/news.py b/rss_reader/news.py index 7b660fc..3b63faa 100644 --- a/rss_reader/news.py +++ b/rss_reader/news.py @@ -1,4 +1,5 @@ import html +import os import re import json import logging @@ -121,3 +122,26 @@ def print(self): def to_json(self): """This function returns JSON-string with news""" return json.dumps({'Source:': self.name_of_source, 'Feeds': self.all_news}, ensure_ascii=False).encode('utf8') + + def to_fb2(self, filepath): + """This function create file in fb2 format with news""" + pass + + def to_html(self, filepath): + print(filepath[-5::]) + if filepath[-5::] != ".html": + filename = filepath + ".html" + with open(filename, 'w') as html_file: + html_file.write(f'\n{self.name_of_source}\n\n') + for elem in self.all_news: + html_file.write(f'

{elem["title"]}

') + html_file.write(f'

Date of posting: {elem["date"]}

') + html_file.write(f'

{elem["description"]}

') + html_file.write(f'

Link to source

') + + for media in elem['media']: + print(f'

') + html_file.write(f'

') + html_file.write('
') + html_file.write('') + print(f'All news you can find at {os.path.realpath(filename)}') diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index b30ad6b..16acb29 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -6,7 +6,7 @@ import sys -version = '1.2' +version = '1.3' def main(): @@ -26,13 +26,14 @@ def main(): parser.add_argument('-V', '--verbose', help='Outputs verbose status messages', action='store_true') parser.add_argument('-L', '--limit', help='Limit news topics if this parameter is provided', type=int, default=0) parser.add_argument('--date', help='Find news in cache if this parameter is provided', type=int, default=0) + parser.add_argument('--to-html', help='Create a HTML file with news', type=str, default="") + parser.add_argument('--to-fb2', help='Create a fb2 file with news', type=str, default="") args = parser.parse_args() if args.verbose: stdout_handler = logging.StreamHandler(sys.stdout) stdout_handler.setFormatter(formatter) logger.addHandler(stdout_handler) - feeds = feedparser.parse(args.URL) if args.date: logger.info('Starting to read from cache') @@ -44,6 +45,8 @@ def main(): logger.info('News from cache ware read') exit() + feeds = feedparser.parse(args.URL) + if feeds.bozo: logger.error('Feed is not well-formed XML') else: @@ -52,7 +55,13 @@ def main(): news = News(feeds, args.limit) logger.info('News is parsed') - if args.json: + if args.to_html: + news.to_html(args.to_html) + + elif args.to_fb2: + news.to_fb2(args.to_fb2) + + elif args.json: print(news.to_json().decode()) logger.info('News is displayed in stdout in a json format') else: diff --git a/setup.py b/setup.py index 3fa41a8..423c810 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='rss_reader', - version='1.1', + version='1.3', author='Boris Dashko', author_email='borya.dashko@gmail.com', url='https://github.com/BoryaD/PythonHomework/tree/FinalTask', From 28ccb0262854f89922ae07979930bd5d2cf609c3 Mon Sep 17 00:00:00 2001 From: Borya Date: Sat, 30 Nov 2019 09:03:04 +0300 Subject: [PATCH 07/10] Added support of creation of fb2 version of news. --- ac.html | 4 --- rss_reader/news.py | 57 +++++++++++++++++++++++++++++++--------- rss_reader/rss_reader.py | 8 +++--- 3 files changed, 49 insertions(+), 20 deletions(-) delete mode 100644 ac.html diff --git a/ac.html b/ac.html deleted file mode 100644 index ace09f4..0000000 --- a/ac.html +++ /dev/null @@ -1,4 +0,0 @@ - -Yahoo News - Latest News & Headlines - -

Kushner named Trump’s border-wall czar — along with practically everything else in government

Date of posting: Tue, 26 Nov 2019 17:33:14 -0500

President Trump has recently tasked his son-in-law — whose to-do list already includes brokering peace in the Middle East, leading U.S. trade policy, reorganizing the entire U.S. government and reforming the criminal justice system — with overseeing the construction of his border wall ahead of the 2020 election. 

Link to source



Then and now: Swiss glacier photos show impact of global warming

Date of posting: Tue, 26 Nov 2019 13:58:47 -0500

A collection of images — showing photos of modern-day mountain landscapes next to archive shots of the same scenes decades earlier — reveals the dramatic change.

Link to source



TSA officers find high-capacity gun magazines hidden in an infant toy at Orlando airport

Date of posting: Wed, 27 Nov 2019 14:10:34 -0500

TSA revealed officers discovered two high capacity magazines in an infant's toy at the Orlando International Airport.

Link to source



Utah banning ‘conversion therapy’ with Mormon church backing

Date of posting: Wed, 27 Nov 2019 10:58:44 -0500

Utah is on its way to becoming the 19th state to ban the discredited practice of conversion therapy in January after state officials formed a proposal that has the support of the influential Church of a Jesus Christ of Latter-day Saints. Republican Gov. Gary Herbert announced Tuesday night that church leaders back a regulatory rule his office helped craft after legislative efforts for a ban on the therapy failed earlier this year. The faith known widely as the Mormon church opposed a previous version of the rule because it wanted assurances that church leaders and members who are therapists would be allowed to provide spiritual counseling for parishioners or families — which were included in the latest conversion therapy ban plan.

Link to source



\ No newline at end of file diff --git a/rss_reader/news.py b/rss_reader/news.py index 3b63faa..d5e8314 100644 --- a/rss_reader/news.py +++ b/rss_reader/news.py @@ -4,12 +4,14 @@ import json import logging from .cache import Cache +import base64 +import requests class News: """This class contains news and methods of work whit news""" http_header = 'http' - img_description = 'image / ?' + err_media_type = 'No type' def __init__(self, feeds_dict, limit): logger = logging.getLogger('rss_reader') @@ -61,7 +63,10 @@ def __init__(self, feeds_dict, limit): j = 1 while j < len(links): - media.append({'url': self.http_header + links[j], 'type': self.img_description}) + if j == 2: + media.append({'url': self.http_header + links[j], 'type': "img"}) + else: + media.append({'url': self.http_header + links[j], 'type': self.err_media_type}) list_to_links.append(self.http_header + links[j]) list_to_links.append(ids) cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', @@ -70,10 +75,11 @@ def __init__(self, feeds_dict, limit): j = j + 1 Cache.commit() else: - media.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) - list_to_links.append(elem.setdefault('url', None)) - list_to_links.append(ids) - cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', list_to_links) + if elem.setdefault('url', None): + media.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) + list_to_links.append(elem.setdefault('url', None)) + list_to_links.append(ids) + cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', list_to_links) list_to_links.clear() Cache.commit() self.news['media'] = media.copy() @@ -124,14 +130,39 @@ def to_json(self): return json.dumps({'Source:': self.name_of_source, 'Feeds': self.all_news}, ensure_ascii=False).encode('utf8') def to_fb2(self, filepath): - """This function create file in fb2 format with news""" - pass + if filepath[-4::] != ".fb2": + filename = filepath + ".fb2" + with open(filename, 'w', encoding="utf-8") as fb2_file: + fb2_file.write('\n') + fb2_file.write(f'''''') + fb2_file.write(f'''<p>{self.name_of_source.replace("&", "&")}</p>''') + for elem in self.all_news: + fb2_file.write(f'
<p>{elem["title"].replace("&", "&")}</p>') + fb2_file.write(f'

Date of posting: {elem["date"].replace("&", "&")}

') + fb2_file.write(f'

{elem["description"].replace("&", "&")}

') + fb2_file.write(f'

Source: {elem["link"]}

'.replace("&", "&")) + + for media in elem['media']: + if media['type'] != self.err_media_type: + fb2_file.write(f''' + ''') + pass + fb2_file.write('') + for elem in self.all_news: + for media in elem['media']: + if media['type'] != self.err_media_type: + fb2_file.write(f'') + content = base64.b64encode(requests.get(media["url"]).content) + fb2_file.write(content.decode('ascii')) + fb2_file.write('') + fb2_file.write('
') + + print(f'All news you can find at {os.path.realpath(filename)}') def to_html(self, filepath): - print(filepath[-5::]) if filepath[-5::] != ".html": filename = filepath + ".html" - with open(filename, 'w') as html_file: + with open(filename, 'w', encoding="utf-8") as html_file: html_file.write(f'\n{self.name_of_source}\n\n') for elem in self.all_news: html_file.write(f'

{elem["title"]}

') @@ -140,8 +171,8 @@ def to_html(self, filepath): html_file.write(f'

Link to source

') for media in elem['media']: - print(f'

') - html_file.write(f'

') - html_file.write('
') + if media['type'] != self.err_media_type: + html_file.write(f'

'.encode("utf-8")) + html_file.write('
') html_file.write('') print(f'All news you can find at {os.path.realpath(filename)}') diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index 16acb29..7cb9158 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -40,7 +40,7 @@ def main(): state = Cache.print_news(args.date) if state == 1: print(f'''There are not exist news with date of publication at {args.date} - \nMake sure that your format date in %Y%m%d', file=sys.stderr''') + \nMake sure that your format date in %Y%m%d''', file=sys.stderr) else: logger.info('News from cache ware read') exit() @@ -48,7 +48,9 @@ def main(): feeds = feedparser.parse(args.URL) if feeds.bozo: - logger.error('Feed is not well-formed XML') + print('This is not well formed XML', file=sys.stderr) + exit() + else: logger.info('The XML file with news is received and correct') @@ -65,8 +67,8 @@ def main(): print(news.to_json().decode()) logger.info('News is displayed in stdout in a json format') else: - news.print() logger.info('News is displayed in stdout in a readability format') + news.print() logger.info('Program is over') From 679c4ca4fbf40569a75bf1e5c1378ada50011bdb Mon Sep 17 00:00:00 2001 From: Borya Date: Sat, 30 Nov 2019 22:40:41 +0300 Subject: [PATCH 08/10] Added tests. Some refactor of news.py. Added scheme of json filee --- Json structure.md | 12 ++++ requirements.txt | 3 +- rss_reader/news.py | 117 +++++++++++++++++++++------------------ rss_reader/rss_reader.py | 4 +- setup.py | 2 +- tests/__init__.py | 0 tests/tests.py | 22 ++++++++ 7 files changed, 101 insertions(+), 59 deletions(-) create mode 100644 Json structure.md create mode 100644 tests/__init__.py create mode 100644 tests/tests.py diff --git a/Json structure.md b/Json structure.md new file mode 100644 index 0000000..fe15532 --- /dev/null +++ b/Json structure.md @@ -0,0 +1,12 @@ +{ + "Source:":string, + "Feeds": [{"title": string, + "date": string, + "link":string, + "description": string, + "media": [{"url": string, + "type": string}], + "links": [{"url": string, + "type": "string"}] + }] +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index cb37aea..ab35068 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -feedparser~=5.2 \ No newline at end of file +feedparser~=5.2 +requests~=2.22 diff --git a/rss_reader/news.py b/rss_reader/news.py index d5e8314..ab7f645 100644 --- a/rss_reader/news.py +++ b/rss_reader/news.py @@ -10,8 +10,10 @@ class News: """This class contains news and methods of work whit news""" + http_header = 'http' err_media_type = 'No type' + def __init__(self, feeds_dict, limit): logger = logging.getLogger('rss_reader') @@ -39,65 +41,71 @@ def __init__(self, feeds_dict, limit): self.news['link'] = html.unescape(feeds_dict.entries[i].link) self.news['description'] = self.clean_from_tags(html.unescape(feeds_dict.entries[i].description)) - list_to_cache.append(self.news['title']) date_dict = feeds_dict.entries[i].published_parsed date_str = str(date_dict.tm_year) + str(date_dict.tm_mon) + str(date_dict.tm_mday) + + list_to_cache.append(self.news['title']) list_to_cache.append(date_str) list_to_cache.append(self.news['date']) list_to_cache.append(self.news['link']) list_to_cache.append(self.news['description']) - cursor.execute('''INSERT or IGNORE INTO news (title, pub_date_key, pub_date, link, description) VALUES(?,?,?,?,?)''', - list_to_cache) - ids = cursor.lastrowid - Cache.commit() - list_to_cache.clear() - list_to_links = list() - - if feeds_dict.entries[i].setdefault('media_content', None): - media = list() - if feeds_dict.entries[i].media_content: - for elem in feeds_dict.entries[i].media_content: - if elem['url'].rfind(self.http_header, 0, len(elem['url'])) > 0: - links = elem['url'].split(self.http_header) - j = 1 - - while j < len(links): - if j == 2: - media.append({'url': self.http_header + links[j], 'type': "img"}) - else: - media.append({'url': self.http_header + links[j], 'type': self.err_media_type}) - list_to_links.append(self.http_header + links[j]) - list_to_links.append(ids) - cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', - list_to_links) - list_to_links.clear() - j = j + 1 - Cache.commit() - else: - if elem.setdefault('url', None): - media.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) - list_to_links.append(elem.setdefault('url', None)) - list_to_links.append(ids) - cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', list_to_links) - list_to_links.clear() - Cache.commit() - self.news['media'] = media.copy() - else: - self.news['media'] = '' - links = list() - if feeds_dict.entries[i].links: - for elem in feeds_dict.entries[i].links: - links.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) - list_to_links.append(elem.setdefault('url', None)) - list_to_links.append(ids) - cursor.execute('''INSERT or IGNORE INTO links (link, news) VALUES(?,?)''', list_to_links) - list_to_links.clear() - self.news['links'] = links.copy() + self.news['media'] = self._parse_media(feeds_dict.entries[i]) + self.news['links'] = self._parse_links(feeds_dict.entries[i]) + + self._cache_feed(list_to_cache, self.news['links'], self.news['media'], cursor) + self.all_news.append(self.news.copy()) - Cache.commit() Cache.close() + def _parse_links(self, news_dict): + """This function parse links of feed""" + list_of_links = list() + if news_dict.links: + for elem in news_dict.links: + list_of_links.append({'url': elem.setdefault('url', None), 'type': elem.setdefault('type', None)}) + return list_of_links + + def _parse_media(self, news_dict): + """This function parse media of feed""" + if news_dict.setdefault('media_content', None): + media = list() + if news_dict.media_content: + for elem in news_dict.media_content: + if elem['url'].rfind(self.http_header, 0, len(elem['url'])) > 0: + # Some sources of news write two links in one string of media. And only second string is image + links = elem['url'].split(self.http_header) + media.append({'url': self.http_header + links[2], 'type': "img"}) + else: + if elem.setdefault('url', None): + media.append({'url': elem.setdefault('url', None), + 'type': elem.setdefault('type', None)}) + return media + else: + return '' + + def _cache_feed(self, list_of_main_info, list_of_links, list_of_media, cursor): + """This function write feed to cache""" + cursor.execute('''INSERT or IGNORE INTO news (title, pub_date_key, pub_date, link, description) + VALUES(?,?,?,?,?)''', list_of_main_info) + ids = cursor.lastrowid + + list_to_cache_of_links = list() + for elem in list_of_links: + list_to_cache_of_links.append(elem.setdefault('url', None)) + list_to_cache_of_links.append(ids) + cursor.execute('''INSERT or IGNORE INTO links (link, news) VALUES(?,?)''', list_to_cache_of_links) + list_to_cache_of_links.clear() + + list_to_cache_of_media = list() + for elem in list_of_media: + list_to_cache_of_media.append(elem.setdefault('url', None)) + list_to_cache_of_media.append(ids) + cursor.execute('''INSERT or IGNORE INTO media (link, news) VALUES(?,?)''', list_to_cache_of_media) + list_to_cache_of_media.clear() + + Cache.commit() + @staticmethod def clean_from_tags(text_with_tags): """This function delete tags from string""" @@ -107,10 +115,10 @@ def print(self): """This function print news to stdout in readable format""" print(f'Source: {self.name_of_source}\n') for elem in self.all_news: - print('Title: ', elem['title']) - print('Date: ', elem['date']) - print('Link: ', elem['link']) - print(f"Description: {elem['description']}\n") + print(f'Title: {elem["title"]}') + print(f'Date: {elem["date"]}') + print(f'Link: {elem["link"]}') + print(f'Description: {elem["description"]}\n') j = 1 print('Links: ') @@ -123,7 +131,6 @@ def print(self): for media in elem['media']: print(f'[{j}] {media["url"]} ({media["type"]})') j = j + 1 - print('\n') def to_json(self): """This function returns JSON-string with news""" @@ -172,7 +179,7 @@ def to_html(self, filepath): for media in elem['media']: if media['type'] != self.err_media_type: - html_file.write(f'

'.encode("utf-8")) + html_file.write(f'

') html_file.write('
') html_file.write('') print(f'All news you can find at {os.path.realpath(filename)}') diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index 7cb9158..1e66dbf 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -6,7 +6,7 @@ import sys -version = '1.3' +version = '1.5' def main(): @@ -67,8 +67,8 @@ def main(): print(news.to_json().decode()) logger.info('News is displayed in stdout in a json format') else: + news.print() logger.info('News is displayed in stdout in a readability format') - news.print() logger.info('Program is over') diff --git a/setup.py b/setup.py index 423c810..2510aa8 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='rss_reader', - version='1.3', + version='1.5', author='Boris Dashko', author_email='borya.dashko@gmail.com', url='https://github.com/BoryaD/PythonHomework/tree/FinalTask', diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/tests.py b/tests/tests.py new file mode 100644 index 0000000..71a814c --- /dev/null +++ b/tests/tests.py @@ -0,0 +1,22 @@ +import unittest +import rss_reader.news as feed + + +class DateTest(unittest.TestCase): + + def setUp(self) -> None: + self.useful_data = "DataClear" + self.data_with_tags = "DataClear" + + def test_clear_from_tags(self): + self.assertEqual(self.useful_data, feed.clean_from_tags(self.data_with_tags)) + + def _parse_links(self): + pass + + def _parse_media(self): + pass + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From 298ffcbbdb773c2e55b9e72a44ff196a16bb9499 Mon Sep 17 00:00:00 2001 From: Borya <48147710+BoryaD@users.noreply.github.com> Date: Sat, 30 Nov 2019 22:41:40 +0300 Subject: [PATCH 09/10] Update Json structure.md --- Json structure.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Json structure.md b/Json structure.md index fe15532..57cb35d 100644 --- a/Json structure.md +++ b/Json structure.md @@ -1,12 +1,21 @@ { + "Source:":string, + "Feeds": [{"title": string, + "date": string, + "link":string, + "description": string, + "media": [{"url": string, + "type": string}], + "links": [{"url": string, + "type": "string"}] }] -} \ No newline at end of file +} From 408216986e5279d6083be5e7be12c1a1768eb19a Mon Sep 17 00:00:00 2001 From: Borya Date: Sun, 1 Dec 2019 12:53:14 +0300 Subject: [PATCH 10/10] Tests was updated. Some methods is Cache was refactored --- .gitignore | 4 ++++ rss_reader/cache.py | 14 +++++++++----- rss_reader/news.py | 7 ++++--- rss_reader/rss_reader.py | 40 ++++++++++++++++++++-------------------- setup.py | 2 +- tests/cache_tests.py | 27 +++++++++++++++++++++++++++ tests/news_tests.py | 31 +++++++++++++++++++++++++++++++ tests/tests.py | 22 ---------------------- 8 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 tests/cache_tests.py create mode 100644 tests/news_tests.py delete mode 100644 tests/tests.py diff --git a/.gitignore b/.gitignore index 6f0fd41..32a6946 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,7 @@ venv.bak/ .idea/ *.db + +*.fb2 + +*.html diff --git a/rss_reader/cache.py b/rss_reader/cache.py index 6b822de..04ba10e 100644 --- a/rss_reader/cache.py +++ b/rss_reader/cache.py @@ -12,18 +12,22 @@ class Cache: def __init__(self): """This method initialize cursor to database""" - if self.cursor: + if self.cursor is None: + Cache._init_cursor() + else: logger = logging.getLogger('rss_reader') logger.error("This is singleton class. Use get_cursor") - exit() + + @staticmethod + def _init_cursor(): Cache.conn = sqlite3.connect(file_path) Cache.cursor = Cache.conn.cursor() Cache.cursor.execute('''CREATE TABLE IF NOT EXISTS news(id INTEGER PRIMARY KEY, - title text, pub_date_key numeric, pub_date text, link text, description text, UNIQUE(link))''') + title text, pub_date_key numeric, pub_date text, link text, description text, UNIQUE(link))''') Cache.cursor.execute('''CREATE TABLE IF NOT EXISTS links( id INTEGER PRIMARY KEY, - link text, news numeric)''') + link text, news numeric)''') Cache.cursor.execute('''CREATE TABLE IF NOT EXISTS media( id INTEGER PRIMARY KEY, - link text, news numeric)''') + link text, news numeric)''') @staticmethod def get_cursor(): diff --git a/rss_reader/news.py b/rss_reader/news.py index ab7f645..68e9c46 100644 --- a/rss_reader/news.py +++ b/rss_reader/news.py @@ -58,7 +58,8 @@ def __init__(self, feeds_dict, limit): self.all_news.append(self.news.copy()) Cache.close() - def _parse_links(self, news_dict): + @staticmethod + def _parse_links(news_dict): """This function parse links of feed""" list_of_links = list() if news_dict.links: @@ -136,7 +137,7 @@ def to_json(self): """This function returns JSON-string with news""" return json.dumps({'Source:': self.name_of_source, 'Feeds': self.all_news}, ensure_ascii=False).encode('utf8') - def to_fb2(self, filepath): + def create_fb2(self, filepath): if filepath[-4::] != ".fb2": filename = filepath + ".fb2" with open(filename, 'w', encoding="utf-8") as fb2_file: @@ -166,7 +167,7 @@ def to_fb2(self, filepath): print(f'All news you can find at {os.path.realpath(filename)}') - def to_html(self, filepath): + def create_html(self, filepath): if filepath[-5::] != ".html": filename = filepath + ".html" with open(filename, 'w', encoding="utf-8") as html_file: diff --git a/rss_reader/rss_reader.py b/rss_reader/rss_reader.py index 1e66dbf..2028ab3 100644 --- a/rss_reader/rss_reader.py +++ b/rss_reader/rss_reader.py @@ -43,35 +43,35 @@ def main(): \nMake sure that your format date in %Y%m%d''', file=sys.stderr) else: logger.info('News from cache ware read') - exit() + else: - feeds = feedparser.parse(args.URL) + feeds = feedparser.parse(args.URL) - if feeds.bozo: - print('This is not well formed XML', file=sys.stderr) - exit() + if feeds.bozo: + print('This is not well formed XML', file=sys.stderr) + exit() - else: - logger.info('The XML file with news is received and correct') + else: + logger.info('The XML file with news is received and correct') - news = News(feeds, args.limit) - logger.info('News is parsed') + news = News(feeds, args.limit) + logger.info('News is parsed') - if args.to_html: - news.to_html(args.to_html) + if args.to_html: + news.create_html(args.to_html) - elif args.to_fb2: - news.to_fb2(args.to_fb2) + elif args.to_fb2: + news.create_fb2(args.to_fb2) - elif args.json: - print(news.to_json().decode()) - logger.info('News is displayed in stdout in a json format') - else: - news.print() - logger.info('News is displayed in stdout in a readability format') + elif args.json: + print(news.to_json().decode()) + logger.info('News is displayed in stdout in a json format') + else: + news.print() + logger.info('News is displayed in stdout in a readability format') logger.info('Program is over') if __name__ == '__main__': - main() \ No newline at end of file + main() diff --git a/setup.py b/setup.py index 2510aa8..6b7fd40 100644 --- a/setup.py +++ b/setup.py @@ -17,4 +17,4 @@ 'rss-reader = rss_reader.rss_reader:main', ], }, -) \ No newline at end of file +) diff --git a/tests/cache_tests.py b/tests/cache_tests.py new file mode 100644 index 0000000..6e01a14 --- /dev/null +++ b/tests/cache_tests.py @@ -0,0 +1,27 @@ +import unittest +from rss_reader.cache import Cache + + +class CacheTest(unittest.TestCase): + + def setUp(self): + pass + + def test_close(self): + pass + + def test_commit(self): + pass + + def test_print_news(self): + pass + + def test_get_cursor(self): + pass + + def test_init_cursor(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/news_tests.py b/tests/news_tests.py new file mode 100644 index 0000000..0edcd0a --- /dev/null +++ b/tests/news_tests.py @@ -0,0 +1,31 @@ +import unittest +from rss_reader.news import News + + +class NewsTest(unittest.TestCase): + + def setUp(self): + self.useful_data = "DataClear" + self.data_with_tags = "DataClear" + + def test_clear_from_tags(self): + self.assertEqual(self.useful_data, News.clean_from_tags(self.data_with_tags)) + + def test_parse_links(self): + pass + + def test_parse_media(self): + pass + + def test_create_fb2(self): + pass + + def test_create_html(self): + pass + + def test_to_json(self): + pass + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/tests.py b/tests/tests.py deleted file mode 100644 index 71a814c..0000000 --- a/tests/tests.py +++ /dev/null @@ -1,22 +0,0 @@ -import unittest -import rss_reader.news as feed - - -class DateTest(unittest.TestCase): - - def setUp(self) -> None: - self.useful_data = "DataClear" - self.data_with_tags = "DataClear" - - def test_clear_from_tags(self): - self.assertEqual(self.useful_data, feed.clean_from_tags(self.data_with_tags)) - - def _parse_links(self): - pass - - def _parse_media(self): - pass - - -if __name__ == '__main__': - unittest.main() \ No newline at end of file