Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions final_task/rss_reader/cmd_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import sys
import argparse
import logging
from final_task.rss_reader import rss_reader

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package import is redundant here. Think about where exactly import statements starts to search for specified module.



__version__ = '1.0'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global variables are usually declared in uppercase e.g. VERSION



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")
2 changes: 2 additions & 0 deletions final_task/rss_reader/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# cat requirements.txt
feedparser==5.2.1
45 changes: 45 additions & 0 deletions final_task/rss_reader/rss_reader.py
Original file line number Diff line number Diff line change
@@ -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")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually it is better to raise exceptions, not to return them

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
Comment on lines +15 to +19

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is better to use in operator instead of this function. For example:
if key in dict_from_rss_site:



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"