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
5 changes: 5 additions & 0 deletions final_task/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__init__.py
[__pycache__]/
*.pyc
.test_cache
event_tracker.log
136 changes: 0 additions & 136 deletions final_task/FinalTask.md

This file was deleted.

3 changes: 3 additions & 0 deletions final_task/MANIFEST.in
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 *
Comment on lines +1 to +3

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 will not work correctly on Windows as it has different path separators

112 changes: 109 additions & 3 deletions final_task/README.md
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 removed final_task/__init__.py
Empty file.
58 changes: 58 additions & 0 deletions final_task/rss_reader/cache.py
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
Loading