diff --git a/final_task/rss_reader/cmd_line.py b/final_task/rss_reader/cmd_line.py new file mode 100644 index 0000000..bc1d51f --- /dev/null +++ b/final_task/rss_reader/cmd_line.py @@ -0,0 +1,31 @@ +import sys +import argparse +import logging +from final_task.rss_reader import rss_reader + + +__version__ = '1.0' + + +if __name__ == '__main__': + """This is executed when run from the command line""" + try: + parser = argparse.ArgumentParser(description='Some description') + # Required position argument + parser.add_argument('source', type=str, help='RSS URL') + # Specify output of "--version" + parser.add_argument('--version', action='version', version='%(prog)s Iteration {version}'.format(version=__version__), help='Print version info') + # Optional argument + parser.add_argument('--json', nargs='?', type=argparse.FileType('w'), default=sys.stdout, + help='Print result as JSON in stdout') + # Optional argument limit count of news + parser.add_argument('--limit', type=int, help='Limit news topics if this parameter provided') + args = parser.parse_args() + limit = args.limit + source = args.source + logging.basicConfig(filename='rss_logging.log', level=logging.INFO) + logging.info('return limit news') + print(rss_reader.get_news(source)['News'][:limit]) + logging.info('return limit news') + except (TypeError, ValueError, IndexError): + sys.exit("Error limit") diff --git a/final_task/rss_reader/requirements.txt b/final_task/rss_reader/requirements.txt index e69de29..9ecda60 100644 --- a/final_task/rss_reader/requirements.txt +++ b/final_task/rss_reader/requirements.txt @@ -0,0 +1,2 @@ +# cat requirements.txt +feedparser==5.2.1 \ No newline at end of file diff --git a/final_task/rss_reader/rss_reader.py b/final_task/rss_reader/rss_reader.py index e69de29..c481fdc 100644 --- a/final_task/rss_reader/rss_reader.py +++ b/final_task/rss_reader/rss_reader.py @@ -0,0 +1,45 @@ +import feedparser + + +def get_dict_from_rss_site(source): + """Function for getting dict from RSS URL""" + try: + dict_from_rss_site = feedparser.parse(source) + if dict_from_rss_site: + return dict_from_rss_site + else: return ConnectionError(f"{source} can't be reached") + except OSError: + raise ConnectionError(f"{source} can't be reached") + + +def check_key_in_dict_from_rss_site(dict_from_rss_site, key): + """Function for checking existing of key in dict""" + if dict_from_rss_site.get(key, None): + return True + return False + + +def get_news(source): + """Function for creation dict news, where key "News" and value is list of news, from RSS URL's news. + Sourse is RSS URL""" + dict_from_rss_site = get_dict_from_rss_site(source) + news = [] + news_reslt = {} + if check_key_in_dict_from_rss_site(dict_from_rss_site, 'entries'): + for entry in dict_from_rss_site.entries: + site_info = {} + if check_key_in_dict_from_rss_site(dict_from_rss_site, 'feed'): + if check_key_in_dict_from_rss_site(dict_from_rss_site.feed, 'title'): + site_info['Feed'] = dict_from_rss_site.feed.title + if check_key_in_dict_from_rss_site(entry, 'title'): + site_info['Title'] = entry.title + if check_key_in_dict_from_rss_site(entry, 'published'): + site_info['Date'] = entry.published + if check_key_in_dict_from_rss_site(entry, 'link'): + site_info['Link'] = entry.link + news.append(site_info) + news_reslt['News'] = news + if news: + return news_reslt + else: + return f"{source} can't be reached"