-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheadline_feed.py
More file actions
79 lines (60 loc) · 2.76 KB
/
headline_feed.py
File metadata and controls
79 lines (60 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from headline import Headline
import json
import os.path
class HeadlineFeed:
"""Represents a timeline of class Headline headlines from Hacker News."""
def __init__(self, raw_list):
headline_list = []
for item in raw_list:
headline_list.append(Headline(item))
self._origin = headline_list
self._feed = headline_list.copy()
def keyword_filter(self, keywords):
"""Filters collected headlines based on if passed keyword is in title."""
self._feed.clear()
for headline in self._origin:
if headline.match_keywords(keywords):
self._feed.append(headline)
def load_from_file(self):
"""Loads file and merges or replaces current feed with saved headlines. Does not change origin."""
merge_str = str(input("Would you like to append current headline list or replace it? ('a' or 'r'): ")).lower()
if merge_str in ('a', 'r'):
if merge_str == 'r':
self._feed.clear()
filename = str(input("Enter name of file to load: ")) + ".json"
if os.path.isfile(filename):
with open(filename, 'r', encoding='utf-8') as f:
data = json.load(f)
for d in data:
self._feed.append(Headline(d))
else:
print("That file does not exist. Check your spelling and try again.")
else:
print("That is not valid input. Aborting load...")
def print_feed(self):
"""Outputs sorted/filtered headline list to command line."""
if len(self._feed) > 0:
for headline in self._feed:
print(headline)
else:
print("No posts match your criteria.")
def save_to_file(self):
"""Saves current feed to JSON file. Uses Headline to_dict method to convert."""
filename = str(input("Please enter filename: ")) + ".json"
if os.path.isfile(filename):
if not str(input("Would you like to overwrite this file? y/n: ") == 'y'):
return
with open(filename, 'w', encoding='utf-8') as f:
json_list = []
for headline in self._feed:
json_list.append(headline.to_dict())
json.dump(json_list, f, indent=4)
def sort_feed(self, sort_on="score", desc=True):
"""Sorts based on passed criteria. sort_on = ('date', 'score', 'title')."""
match sort_on:
case "date":
self._feed.sort(key=lambda x: x.get_date(), reverse=desc)
case "score":
self._feed.sort(key=lambda x: x.get_score(), reverse=desc)
case "title":
self._feed.sort(key=lambda x: x.get_title(), reverse=desc)