Skip to content

[WIP] Kudryavtsev Kirill #31

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
wants to merge 4 commits into
base: master
Choose a base branch
from
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# PythonHomework
[Introduction to Python] Homework Repository
branch with final Hometask

Package: feedparser
json format: {"Title":"title", "Link":"link", "Date":"date", "Content":"content"}
88 changes: 88 additions & 0 deletions finale.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import feedparser
import argparse
import json
import datetime
import os

version = '1.0'


def print_function(feed, limit, js):
func_limit = 0
d = {}
now = datetime.datetime.now()
year = now.year
month = now.month
day = now.day
year_str = str(year)
if day>9:
day_str = str(day)
else:
day_str = '' + '0' + str(day)
if month > 9:
month_str = str(month)
else:
month_str = '' + '0' + str(month)
name = '' + year_str + month_str + day_str + ".txt"
f = open(name, 'w')

for entry in feed.entries:
func_limit += 1
article_title = entry.title
article_link = entry.link
article_date = entry.published
content = entry.description

if js == 0:
print("Title: {}".format(article_title))
print("Link: [{}]".format(article_link))
print("Date: {}\n".format(article_date))
print("Content: [{}]\n\n".format(content))
else:
d["Title"] = article_title
d["Link"] = article_link
d["Date"] = article_date
d["Content"] = content
print(json.dumps(d) + '\n')

f.write("Title : " + article_title)
f.write("\nLink: [" + article_link + "]")
f.write("\nDate: " + article_date)
f.write("\nContent: [" + content + "]\n\n")

if func_limit == limit:
break

f.close()


def date_function(date):
name = '' + str(date) + '.txt'
if os.path.isfile(name):
f = open(name, 'r')
for line in f:
print(line)
f.close()
else:
print("ERROR")


parser = argparse.ArgumentParser(description='Rss reader programm')
parser.add_argument('link', help='Receive rss url in format: \"url\"')
parser.add_argument('--limit', type=int, default=0, help='Receive int \'x\' and print \'x\' block of news')
parser.add_argument('--version', action='count', default=0, help='Print current version of a program and complete work')
parser.add_argument('--json', action='count', default=0, help="print result as json")
parser.add_argument('--date', type=int, default=0, help='Receive yyyymmdd and print news, checked at that day, if they exist')

args = parser.parse_args()

feed = feedparser.parse(args.link)
feed_entries = feed.entries

if args.version != 0:
print("Current version is: ", version)
else:
if args.date == 0:
print_function(feed, args.limit, args.json)
else:
date_function(args.date)