-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliloFetch.py
273 lines (214 loc) · 7.8 KB
/
liloFetch.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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from goose import Goose
from collections import defaultdict
from string import punctuation
from heapq import nlargest
from pymessenger import Bot, Element
import nltk
import pprint
import praw
import logging
import tldextract
import random
logging.raiseExceptions = False
class rAPI_Service():
reddit_service = None
def __init__(self):
self.reddit_service = self.connect()
return None
def connect(self):
reddit = praw.Reddit(client_id='J9ccJUbL7QSHaA',
client_secret='vVSFAE1gyzJW4_VL_cXQPY17CSc',
password='password1!',
user_agent='testscript by /u/spiceminerjoe',
username='spiceminerjoe')
return reddit
def get_api_user(self):
if self.reddit_service is None:
self.connect()
return self.reddit_service.user.me()
def check_articles_are_selfPost(self, articles):
sub_is_self_posts = False
for article in articles:
if article.is_self is False:
sub_is_self_posts = False
else:
sub_is_self_posts = True
return sub_is_self_posts
def check_sub_exists(self, sub):
real_sub = False
if self.reddit_service is None:
self.connect()
try:
subreddit = self.reddit_service.subreddit(sub)
articles = [article for article in subreddit.hot(limit=25)]
sampleArticle = articles[random.randint(0,len(articles) - 1)]
count = len(articles)
selfPost_sub = self.check_articles_are_selfPost(articles)
if count >= 10 and selfPost_sub is False:
print("[i] Sub Exists:\t" + sub)
real_sub = True
else:
print("[X] Sub Doesnt Exist:\t" + sub)
real_sub = False
except Exception as e:
print("[X] Sub Doesnt Exist:\t" + sub)
real_sub = False
return real_sub
def sample_sub(self, sub, filter, lim):
subcontent = None
if self.reddit_service is None:
self.connect()
if filter == 'hot':
sub_content = self.reddit_service.subreddit(sub).hot(limit=lim)
elif filter == 'top':
sub_content = self.reddit_service.subreddit(sub).top(limit=lim)
else:
sub_content = self.reddit_service.subreddit(sub).new(limit=lim)
if sub_content is not None:
try:
for post in sub_content:
if len(post.title) > 0:
return sub_content
else:
return False
except:
return False
def sample_article(self, sub):
article_Service = articleService()
if self.reddit_service is None:
self.connect()
try:
subreddit = self.reddit_service.subreddit(sub)
articles = [article for article in subreddit.hot(limit=25)]
sampleArticle = articles[random.randint(0,len(articles) - 1)]
return sampleArticle
except Exception as e:
print("Exception\t::\t" + str(e))
return False
def goosify_rapi_article(self, article):
goose = Goose()
gooseArticle = False
if self.reddit_service is None:
self.connect()
try:
gooseArticle = goose.extract(url=article.url)
except:
return False
return gooseArticle
class FrequencySummarizer:
def __init__(self, min_cut=0.1, max_cut=0.9):
"""
Initilize the text summarizer.
Words that have a frequency term lower than min_cut
or higer than max_cut will be ignored.
"""
self._min_cut = min_cut
self._max_cut = max_cut
self._stopwords = set(nltk.corpus.stopwords.words('english') + list(punctuation))
def _compute_frequencies(self, word_sent):
"""
Compute the frequency of each of word.
Input:
word_sent, a list of sentences already tokenized.
Output:
freq, a dictionary where freq[w] is the frequency of w.
"""
freq = defaultdict(int)
for s in word_sent:
for word in s:
if word not in self._stopwords:
freq[word] += 1
# frequencies normalization and fitering
m = float(max(freq.values()))
for w in freq.keys():
freq[w] = freq[w]/m
if freq[w] >= self._max_cut or freq[w] <= self._min_cut:
del freq[w]
return freq
def summarize(self, text, n):
"""
Return a list of n sentences
which represent the summary of text.
"""
sents = nltk.tokenize.sent_tokenize(text)
word_sent = [nltk.tokenize.word_tokenize(s.lower()) for s in sents]
self._freq = self._compute_frequencies(word_sent)
ranking = defaultdict(int)
for i,sent in enumerate(word_sent):
for w in sent:
if w in self._freq:
ranking[i] += self._freq[w]
sents_idx = self._rank(ranking, n)
return [sents[j] for j in sents_idx]
def _rank(self, ranking, n):
""" return the first n sentences with highest ranking """
return nlargest(n, ranking, key=ranking.get)
class articleService():
image_domains = ['imgur','i.redd']
video_domains = ['youtube','v.redd','youtu']
article_domains = ['bbc']
filter_domains = ['reddit','twitter','facebook']
def __init__(self):
return None
def summarize_article(self, article):
fs = FrequencySummarizer()
for s in fs.summarize(article, 2):
article = s
return article
def format_article(self, microarticle, type):
microarticle_formatted = False
rapi = rAPI_Service()
article_Service = articleService()
article_title = microarticle.title.title()
article_url = microarticle.url
extracted_article = rapi.goosify_rapi_article(microarticle)
try:
thumb = extracted_article.top_image.src
except:
thumb = microarticle.thumbnail
elements = []
if type == 'article':
try:
article_summary = str(article_Service.summarize_article(extracted_article.cleaned_text))
print("[i] Article Summary: ")
print(article_summary)
except Exception as e:
print("Cant get summary::\t" + str(e))
article_summary = "Fetched by Lilo!"
element = Element(title=article_title, image_url=thumb, subtitle=article_summary, item_url=article_url)
elif type == 'image':
element = Element(title=article_title, image_url=thumb, subtitle="Click to view", item_url=article_url)
elif type == 'video':
element = Element(title=article_title, image_url=thumb, subtitle="Click to view", item_url=article_url)
print("TYPE\t::\t" + str(type))
elements.append(element)
return elements
def type_check(self, microarticle):
try:
link = microarticle.url
except:
link = "https://reddit.com"
domain_extracted = tldextract.extract(link)
domain = domain_extracted.domain
subdomain = domain_extracted.subdomain
if domain == 'redd':
domain = subdomain + "." + domain
if domain in self.filter_domains:
return False
elif domain in self.image_domains:
return 'image'
elif domain in self.video_domains:
return 'video'
elif domain in self.article_domains:
return 'article'
else:
if microarticle.is_self is True:
return 'filtered'
else:
return 'article'
# print(str(type) + " - " + link)
return False
def get_tokens(self, str):
tokens = []
tokens = nltk.tokenize.word_tokenize(str)
return tokens