-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract_tasks.py
136 lines (102 loc) · 3.18 KB
/
extract_tasks.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
import json
from urllib.parse import urljoin
from celery import Celery
import requests
from bs4 import BeautifulSoup
import fasttext
from gensim.utils import simple_preprocess
queue = Celery(
'news',
broker='redis://localhost:6379/0',
result_backend='redis://localhost:6379/1',
)
@queue.task
def add(a, b):
return a + b
@queue.task
def fetch_website_task(domain):
r = requests.get(f'https://{domain}')
return {
'domain': domain,
'raw': r.text,
}
def bbc(tag):
return tag.name == 'a' and 'media__link' in tag.get('class', []) and tag.get('href', '').startswith('/') and tag.text.strip()
def guardian(tag):
return tag.name == 'a' and tag.get('data-link-name') == 'article' and tag.text.strip()
def wp(tag):
return tag.name == 'span' and tag.parent.name == 'a' and tag.text.strip()
def fox(tag):
return tag.name == 'a' and tag.parent.name == 'h2' and 'title' in tag.parent.get('class') and tag.text.strip()
def wsj(tag):
return any(['headline' in cls for cls in tag.get('class', [])]) and tag.text.strip()
filter_rules = {
'bbc.com': bbc,
'theguardian.com': guardian,
'washingtonpost.com': wp,
'foxnews.com': fox,
'wsj.com': wsj,
}
@queue.task
def extract_titles(data):
raw = data.get('raw')
domain = data.get('domain')
soup = BeautifulSoup(raw, 'html.parser')
titles = []
for tag in soup.find_all(filter_rules[domain]):
title = {
'text': tag.text.strip(),
'link': tag.get('href') or tag.parent.get('href', ''),
}
if not title['link'].startswith('http'):
title['link'] = urljoin(f'https://{domain}', title['link'])
titles.append(title)
return {'titles': titles, **data}
@queue.task
def post_slack(data, response_url):
titles = data.get('titles')
if not response_url or not titles:
return
titles = json.dumps(titles[:10], indent=2)
_json = {'text': f'```{titles}```'}
if blocks := data.get('blocks'):
_json = {'response_type': 'in_channel', 'blocks': blocks}
requests.post(response_url, json=_json)
return
model = fasttext.load_model('./opinion.bin')
@queue.task
def classify(data):
# enrich with classified label/score
titles = data.get('titles')
if not titles:
return data
for title in titles:
text = title.get('text')
text = ' '.join(simple_preprocess(text))
label, score = model.predict(text)
title['label'] = label[0].replace('__label__', '')
title['score'] = score[0]
return data
@queue.task
def format_slack(data):
domain = data.get('domain')
titles = data.get('titles')[:10]
blocks = [
{
'type': 'section',
'text': {
'type': 'mrkdwn',
'text': f'*Recent headlines from {domain}*',
},
},
{'type': 'divider'},
]
for title in titles:
blocks.append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"<{title['link']}|{title['text']}>\n{title['label']}: {round(title['score'] * 100, 2)}%"
},
})
return {'blocks': blocks, **data}