-
Notifications
You must be signed in to change notification settings - Fork 24
Veranika_Rechyts_verarechyts@gmail.com #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
|
||
|
|
||
| __version__ = '1.0' | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Global variables are usually declared in uppercase e.g. |
||
|
|
||
|
|
||
| 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") | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # cat requirements.txt | ||
| feedparser==5.2.1 |
| 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") | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to use |
||
|
|
||
|
|
||
| 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" | ||
There was a problem hiding this comment.
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
importstatements starts to search for specified module.