-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhnmanager.py
54 lines (48 loc) · 1.76 KB
/
hnmanager.py
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
from hackernews import hn
import json
import random
from urlshortner import URLShortner
class HNManager:
def __init__(self):
self.bestnews = []
self.interests = []
self.news_client = hn.NewsClient()
self.previousshownnews = []
def filterstories(self, story):
for interest in self.interests:
if interest.lower() in story.title.lower():
print("story : accepted: " + story.title)
return True
return False
def fetchbestnews(self):
self.previousshownnews.clear()
self.bestnews.clear()
storylist = self.news_client.get_best_story(fetchMax=500)
self.bestnews = list(filter(self.filterstories, storylist))
def getstory(self):
# Get length of list
# Generate random number based on length of
rand = random.randint(0, len(self.bestnews)-1)
print("Total news : ")
print(self.bestnews)
print(len(self.bestnews))
if rand not in self.previousshownnews:
try:
tweet = self.bestnews[rand].title
url = URLShortner().shorten(self.bestnews[rand].url)
except:
self.getstory()
tweet = tweet + "\n" + url
for interest in self.interests:
if interest.lower() in self.bestnews[rand].title.lower():
tweet = tweet + " #" + interest
tweet = tweet + " #programmer #developer #geek"
self.previousshownnews.append(rand)
print("random index : ")
print(rand)
return tweet
else:
self.getstory()
def fillinterestsdata(self):
f = open("interests.json")
self.interests = json.loads(f.read())["interests"]