-
Notifications
You must be signed in to change notification settings - Fork 0
/
webScraper.py
71 lines (55 loc) · 2.18 KB
/
webScraper.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
import asyncio
import aiohttp
import bs4
import json
from deals import Deals
from utils import chunker, load_scraper_config
class WebScraper(object):
def __init__(self):
self.config = load_scraper_config()
asyncio.run(self.main())
async def extract_deal_teasers(self, text):
try:
soup = bs4.BeautifulSoup(text, 'html.parser')
return soup.find_all('div', class_="node node-ozbdeal node-teaser")
except Exception as e:
print(str(e))
async def message_channel(self, session, config, payload):
try:
headers = {'Content-Type': 'application/json'}
content = json.dumps(payload)
webhook_url = config['webhook_url']
webhook_data = content
response = await session.post(webhook_url, data=webhook_data, headers=headers)
return response
except Exception as ex:
print(ex)
async def fetch(self, session, config):
try:
url = config['oz_bargain_url']
async with session.get(url) as response:
text = await response.text()
teasers = await self.extract_deal_teasers(text)
teaser_deals = Deals(teasers)
teaser_embeds = teaser_deals.list()
message_tasks = []
if len(teaser_embeds) > 0:
for group in chunker(teaser_embeds, 10):
message_tasks.append(self.message_channel(session, config, {
"color": "0x0099ff",
"embeds": group
}))
else:
message_tasks.append(
self.message_channel(session, config, { "content": "No Deals Found." })
)
await asyncio.gather(*message_tasks)
return text, url, teasers
except Exception as e:
print(str(e))
async def main(self):
tasks = []
async with aiohttp.ClientSession() as session:
for config in self.config:
tasks.append(self.fetch(session, config))
await asyncio.gather(*tasks)