-
Notifications
You must be signed in to change notification settings - Fork 24
Yuliya_Brechka_juliabrechka@gmail.com #21
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
Open
brechka
wants to merge
13
commits into
epam-python-courses-7-bsu:master
Choose a base branch
from
brechka:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
34b4b61
implement first iteration
brechka 202298c
implement second iteration
brechka 3c5274e
fix according to comments
brechka cc91592
implement third iteration
brechka f8ec24c
implement 4 and 5 iterations. Add README.md and requirements.txt.
brechka 7c05ad6
test utility
brechka 00cd863
add fonts for pdf_converter.py
brechka 3fbd35e
final version
brechka 7120933
ignore listed files
brechka c333bfc
ignore listed files
brechka f967395
fix bug
brechka 22be293
change name in setup.py
brechka b03ff1d
remove change dir method
brechka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| __init__.py | ||
| [__pycache__]/ | ||
| *.pyc | ||
| .test_cache | ||
| event_tracker.log |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| recursive-include rss_reader/templates * | ||
| recursive-include rss_reader/tests/files * | ||
| recursive-include rss_reader/fonts * | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,109 @@ | ||
| # Your readme here | ||
| Some text. | ||
| Checkout how to write this file using *markdown*. | ||
| # Python RSS-reader | ||
| RSS reader is a command-line utility which receives RSS URL and prints results in human-readable format. | ||
|
|
||
| **Utility provides the following interface:** | ||
|
|
||
| ``` | ||
| usage: rss_reader.py [-h] [--version] [--json] [--verbose] [--limit LIMIT] | ||
| [--date DATE] [--to-html TO_HTML] [--to-pdf TO_PDF] | ||
| [--colorize] | ||
| [source] | ||
|
|
||
| Performs a variety of operations on a file. | ||
|
|
||
| positional arguments: | ||
| source RSS URL | ||
|
|
||
| optional arguments: | ||
| -h, --help show this help message and exit | ||
| --version Print version info | ||
| --json Print result as JSON in stdout | ||
| --verbose Outputs verbose status messages | ||
| --limit LIMIT Limits the number of displayed news | ||
| --date DATE Displays news for the specified day. It takes a date in %Y%m%d format. | ||
| --to-html TO_HTML Converts news in html format. Receives the path for file | ||
| saving | ||
| --to-pdf TO_PDF Converts news in pdf format. Receives the path for file | ||
| saving | ||
| --colorize Make stdout in colour | ||
| ``` | ||
|
|
||
| In addition to the --verbose argument, the utility also provides recording logging events in a event_tracker.log file that remembering all messages from earlier runs. | ||
| In case of --limit is 0 or greater than amount of received news - all available news will be displayed. | ||
|
|
||
|
|
||
|
|
||
| ## [Requirements](https://github.com/brechka/FinalTaskRssParser/blob/master/final_task/rss_reader/requirements.txt) | ||
|
|
||
| The REST API was created using Python 3.8. To run the APP you need to install with pip packages listed in [requirements.txt](https://github.com/brechka/FinalTaskRssParser/blob/master/final_task/rss_reader/requirements.txt) | ||
| (better to use virtual environment): | ||
|
|
||
| ``` | ||
| pip install -r requirements.txt | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## Usage of RSS-reader | ||
|
|
||
| For usage the utility use followed option: | ||
|
|
||
| clone current repository and install the requirements (see the description above), it's better to use isolated environment with virtualenv. | ||
| The entire application is contained within the rss_reader.py file. For running the utility use previously listed command line arguments. For example: | ||
|
|
||
| ``` | ||
| rss-reader https://news.yahoo.com/rss/ --limit 1 | ||
| ``` | ||
|
|
||
| The output would be the following structure: | ||
|
|
||
| ``` | ||
| Feed: Yahoo News - Latest News & Headlines | ||
|
|
||
| Title: GOP claim that Trump cares about corruption takes a hit at impeachment hearing | ||
|
|
||
| Date: Wed, 20 Nov 2019 20:26:09 -0500 | ||
| Link: https://news.yahoo.com/gop-claim-that-trump-cares-about-corruption-takes-a-hit-at-impeachment-heaing-012609516.html | ||
|
|
||
| [image 2: GOP claim that Trump cares about corruption takes a hit at impeachment hearing] | ||
| Rep. Jim Himes, D-Conn., took issue with a defense of President Trump floated by Rep. John Ratcliffe, R-Texas. | ||
|
|
||
| Links: | ||
| [1]: https://news.yahoo.com/gop-claim-that-trump-cares-about-corruption-takes-a-hit-at-impeachment-heaing-012609516.html (link to the article) | ||
|
|
||
| [2]: http://l.yimg.com/uu/api/res/1.2/cpj4jzp35ZTzQ6ds8B1M0w--/YXBwaWQ9eXRhY2h5b247aD04Njt3PTEzMDs-/https://media-mbst-pub-ue1.s3.amazonaws.com/creatr-uploaded-images/2019-11/64948b40-0bee-11ea-ad7c-1300326b62d1 | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| ## Description | ||
|
|
||
| ###### [rss_reader/rss_reader.py](https://github.com/brechka/FinalTaskRssParser/blob/master/final_task/rss_reader/rss_reader.py) | ||
|
|
||
| In rss_reader.py RSS-reader utility is initialized and configured. The utility provides the following features: | ||
|
|
||
| ##### 1) *Data caching:* | ||
|
|
||
| The RSS news are stored in the local storage while reading. For this purpose [shelve](https://docs.python.org/3/library/shelve.html), dictionary-like object, was used. | ||
| With the optional argument ```--date %Y%m%d``` (for example: ```--date 20191120```) the cashed news can be read. Also, adding URL make the displayed news sorted by date and link. | ||
| If the news are not found - error should be returned. | ||
|
|
||
| ##### 2) *Data conversion to .pdf and .html:* | ||
|
|
||
| The RSS-reader utility provides news converter to .pdf and .html formats. It can be accomplished with the following optional arguments: ```--to-pdf``` and ```--to-html```. | ||
| Both arguments receive the path where new file would be saved. The name of the file generates automatically. | ||
| There are two options for news conversion: | ||
|
|
||
| - *convert from cache* | ||
|
|
||
| For this case news conversion doesn't depend on internet. With ```rss_reader.py --date 20191120 --limit 1 --to-html ~/finaltask/rss-reader``` one news for the specified day would be converted (the same with ```--to-pdf```), and file with it would be generated in the specified PATH. | ||
| There would be clickable links to the full article and images. | ||
|
|
||
| - *convert fresh news from the Internet* | ||
|
|
||
| For this case Internet is required. With ```rss_reader.py https://news.yahoo.com/rss/ --limit 1 --to-html ~/finaltask/rss-reader``` one news from listed link would be converted (the same with ```--to-pdf```), and file with it would be generated in the specified PATH. | ||
| In this case all pictures would be clickable and would be displayed, if the format permits. Otherwise, there would be clickable links to the images. Also result would be contain a links to the full articles. | ||
|
|
||
| ##### 3) *Output in colour:* | ||
|
|
||
| For colorizing the output there are ```--colorize``` optional argument. Utility provides colorization of the logs and RSS news output (json included). |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| #!/usr/bin/env python3.8 | ||
| """Module for caching news in .feeds_cache file""" | ||
|
|
||
| import itertools | ||
| import shelve | ||
| from os import path | ||
|
|
||
| from logger import LOGGER | ||
| from rss_exceptions import SpecifiedDayNewsError, EmptyCacheError | ||
|
|
||
|
|
||
| DIRECTORY = path.abspath(path.dirname(__file__)) | ||
|
|
||
|
|
||
| def cache_news(parsed_news): | ||
| """ | ||
| Cache news in 'feeds_cache' file. | ||
| """ | ||
| LOGGER.info('Trying to cache news into a file') | ||
| with shelve.open(path.join(DIRECTORY, '.feeds_cache')) as cache: | ||
| for new in parsed_news: | ||
| date = new['date'] | ||
| cache[date] = new | ||
|
|
||
| LOGGER.info('News was cached successfully.') | ||
|
|
||
|
|
||
| def get_cached_news(cmd_args): | ||
| """ | ||
| Extract news from the cache for a specified day. | ||
| """ | ||
| LOGGER.info("Get started fetching cached news if exists.") | ||
|
|
||
| with shelve.open(path.join(DIRECTORY, '.feeds_cache')) as cache: | ||
| if not cache: | ||
| raise EmptyCacheError('Cache is empty. Please, retrieve data from internet.') | ||
|
|
||
| limit = cmd_args.limit or len(cache) | ||
| all_news = list(itertools.islice(make_news_item(cmd_args, cache), 0, limit)) | ||
|
|
||
| if not all_news: | ||
| raise SpecifiedDayNewsError('On the specified day there are no entries in the cache.') | ||
|
|
||
| LOGGER.info('News was extracted from the cache successfully.') | ||
|
|
||
| return all_news | ||
|
|
||
|
|
||
| def make_news_item(cmd_args, cache): | ||
| """ | ||
| Iterate the cache with required key 'data' and 'url', if specified. | ||
| """ | ||
| for key_date, news_date in cache.items(): | ||
| if cmd_args.date in key_date: | ||
| if cmd_args.source == news_date['feed_url']: | ||
| yield news_date | ||
| elif not cmd_args.source: | ||
| yield news_date |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 will not work correctly on Windows as it has different path separators