-
Notifications
You must be signed in to change notification settings - Fork 32
Mlynarchik Artyom #40
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
Archeex
wants to merge
36
commits into
introduction-to-python-bsuir-2019:master
Choose a base branch
from
Archeex:final_task
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 11 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
e8be149
Initial commit
b060fb3
Main functionality made
8b8d8a1
Added display of article hrefs
bebd6cf
Create README.md
Archeex bfa6e10
Update README.md
Archeex 4df505d
Fix imports for project distribution
56dd8eb
Update README.md
94a0554
Update README.md
Archeex 29766e9
Update README.md
Archeex 1b5b06d
Update utility name in setup.py
01ce0c7
Add opportunity to print json to console, enumerate replaced with slices
1953b34
News caching done
22ac84d
Update README.md
Archeex db491e0
Argument '--date' implemented [Iteration 3 completed]
2bd4b78
Argument '--date' implemented [Iteration 3 completed]
3ca84ef
Code refactor
719b0cf
Code refactor
4111eaa
Update json_schema.json
Archeex abe312f
Update README.md
Archeex 9ada778
Code refactor and update setup.py
466ffd7
Code refactor and update setup.py
28bc86d
PDFConverter implemented, some code refactor
93d4e04
Update README.md
Archeex f9b6fa9
Update README.md
Archeex 8e7209b
Update README.md
Archeex 4cb5555
Update README.md
Archeex 208e667
HTMLConverter implemented
23ff39e
HTMLConverter implemented
c677187
Fix launch problems
f5767c1
Console output colorized. Iteration 5 completed
87a1e92
Fix launch problems
92bd0d1
Add requirements.txt
6e786eb
Code refactoring
3785b40
Code refactor and adding tests
cf99556
Fix launch problems
fc70aa5
Delete cached_news.json
Archeex 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,24 @@ | ||
# Pure Python RSS Reader [PythonHomework] | ||
|
||
Python version: v3.8 | ||
|
||
Current version: v0.2 | ||
|
||
Code checking: Code correspond to pep8 | ||
#### Usage: | ||
```shell | ||
usage: __main__.py [-h] [--version] [--json] [--verbose] [--limit LIMIT] source | ||
|
||
Pure Python command-line RSS reader. | ||
|
||
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 Limit news topics if this parameter provided | ||
``` | ||
JSON scheme is described in `json_schema.json` |
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,31 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema#", | ||
"title": "feed", | ||
"type": "object", | ||
"required": ["title", "date", "text", "link", "hrefs"], | ||
"properties": { | ||
"title": { | ||
"type": "string", | ||
"description": "Article title" | ||
}, | ||
"date": { | ||
"type": "date", | ||
"description": "Article published date" | ||
}, | ||
"text": { | ||
"type": "string", | ||
"description": "Article text" | ||
}, | ||
"link": { | ||
"type": "string", | ||
"description": "Article static link" | ||
}, | ||
"hrefs": { | ||
"type": "array", | ||
"items": { | ||
"type": "string", | ||
"description": "Article href" | ||
} | ||
} | ||
} | ||
} |
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,34 @@ | ||
import argparse | ||
import logging | ||
|
||
from rss_reader import Reader | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
if args.verbose: | ||
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) | ||
else: | ||
logging.basicConfig(format="%(levelname)s: %(message)s") | ||
|
||
reader = Reader(args.source, args.limit, args.json) | ||
reader.parse_url() | ||
|
||
reader.print_articles() | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description='Pure Python command-line RSS reader.') | ||
|
||
parser.add_argument('source', help='RSS URL') | ||
parser.add_argument('--version', help='Print version info', action='version', version='%(prog)s 0.2') | ||
parser.add_argument('--json', help='Print result as JSON in stdout', action='store_true') | ||
parser.add_argument('--verbose', help='Outputs verbose status messages', action='store_true') | ||
parser.add_argument('--limit', help='Limit news topics if this parameter provided', type=int) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,26 @@ | ||
"""Article module""" | ||
|
||
import re | ||
import time | ||
import logging | ||
|
||
|
||
class Article: | ||
logger = logging.getLogger('__main__.py') | ||
|
||
def __init__(self, title, date, text, link, hrefs): | ||
self.title = title | ||
self.date = date | ||
self.text = self.strip_html_string(text) | ||
self.link = link.split('?')[0] | ||
self.hrefs = hrefs | ||
|
||
def convert_time_to_unix(self): | ||
"""Convert datetime to unix time""" | ||
pattern_time = time.strptime(self.date, '%a, %d %b %Y %H:%M:%S %z') | ||
return int(time.mktime(pattern_time)) | ||
|
||
def strip_html_string(self, string): | ||
"""Remove html tags from a string""" | ||
strip_string = re.compile('<.*?>') | ||
return re.sub(strip_string, '', string) |
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,41 @@ | ||
"""Module with tools for working with Json""" | ||
|
||
import json | ||
import codecs | ||
import logging | ||
|
||
|
||
class Json: | ||
logger = logging.getLogger('__main__.py') | ||
|
||
def __init__(self): | ||
self.data = {} | ||
|
||
def __str__(self): | ||
"""Print JSON-file to console""" | ||
Archeex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.logger.info('Print JSON-data to console') | ||
|
||
return json.dumps(self.data, ensure_ascii=False, indent=4) | ||
|
||
# def print(self): | ||
# """Print JSON-file to console""" | ||
# self.logger.info('Print JSON-data to file') | ||
|
||
print(json.dumps(self.data, ensure_ascii=False, indent=4)) | ||
|
||
def write_to_file(self): | ||
"""Write JSON-data to file""" | ||
self.logger.info('Write JSON-data to file') | ||
|
||
with codecs.open('data.json', 'w', encoding='utf-8') as outfile: | ||
json.dump(self.data, outfile, ensure_ascii=False, indent=4) | ||
|
||
def format(self, data): | ||
"""Format file to JSON-format""" | ||
self.logger.info('Format data to JSON appereance') | ||
|
||
self.data = {} | ||
self.data['feed'] = [] | ||
|
||
for element in data: | ||
self.data['feed'].append(element) |
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,85 @@ | ||
"""RSS-reader module""" | ||
|
||
import feedparser | ||
import logging | ||
|
||
from article import Article | ||
from json_format import Json | ||
|
||
|
||
class Reader: | ||
logger = logging.getLogger('__main__.py') | ||
|
||
def __init__(self, link, limit, json): | ||
self.link = link | ||
self.limit = limit | ||
self.articles = [] | ||
self.json = json | ||
self.hrefs = [] | ||
|
||
def parse_url(self): | ||
"""Get RSS xml-file from url""" | ||
self.logger.info('Get RSS XML-file from url') | ||
|
||
self.feed = feedparser.parse(self.link) | ||
self.parse_xml(self.feed.entries[:self.limit]) | ||
|
||
def parse_xml(self, source): | ||
"""Parse xml-file to articles""" | ||
self.logger.info('Parse XML-file to articles') | ||
|
||
for item in source: | ||
content = [] | ||
|
||
try: | ||
for element in item.media_content: | ||
content.append(element['url']) | ||
except AttributeError: | ||
try: | ||
for element in item.media_thumbnail: | ||
content.append(element['url']) | ||
except AttributeError: | ||
content.append('No content!') | ||
# content.append('No content!') | ||
|
||
self.articles.append(Article(item.title, item.published, item.description, item.link, content)) | ||
|
||
if self.json is True: | ||
json_object = Json() | ||
feeds = self.articles_to_array() | ||
json_object.format(feeds) | ||
print(json_object) | ||
|
||
def articles_to_array(self): | ||
self.logger.info('Convert articles to array of dicts') | ||
|
||
array = [] | ||
for article in self.articles: | ||
feed_dict = {} | ||
feed_dict.update({'title': article.title}) | ||
feed_dict.update({'date': article.date}) | ||
feed_dict.update({'text': article.text}) | ||
feed_dict.update({'link': article.link}) | ||
feed_dict.update({'hrefs': article.hrefs}) | ||
array.append(feed_dict) | ||
|
||
return array | ||
|
||
def print_articles(self): | ||
self.logger.info('Print articles to console') | ||
|
||
for article in self.articles: | ||
self.print_article(article) | ||
print('\n-------------------------\n') | ||
|
||
def print_article(self, article): | ||
"""Print article to console""" | ||
|
||
print(f'Title: {article.title}') | ||
print(f'Date: {article.date}') | ||
print(f'Link: {article.link}') | ||
print('\nArticle text:') | ||
print(article.text) | ||
print('\nHrefs:') | ||
for href in article.hrefs: | ||
print(href) |
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,19 @@ | ||
import setuptools | ||
|
||
setuptools.setup( | ||
Archeex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name="rss-reader", | ||
version="0.2", | ||
author="Archeex", | ||
author_email="qsanich@gmail.com", | ||
description="Pure Python command-line RSS reader", | ||
packages=setuptools.find_packages(), | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"License :: OSI Approved :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
entry_points={ | ||
'console_scripts': ['rss_reader = rss_reader.__main__:main'] | ||
}, | ||
python_requires='>=3.8' | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.