-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscrapy_google_chat.py
85 lines (71 loc) · 3.07 KB
/
scrapy_google_chat.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
import json
from urllib.request import Request, urlopen
from scrapy import signals
from scrapy.exceptions import NotConfigured
class GoogleChatBot(object):
"""
Sends crawl reports to Google Chat
REQUIRED SETTING:
- GOOGLE_CHAT_WEBHOOK = 'https://chat.googleapis.com/v1/spaces/XXXXXXXX/messages?key=XXXXXXXX&token=XXXXXXXX'
Read here to learn how to create a webhook: https://developers.google.com/hangouts/chat/how-tos/webhooks
"""
def __init__(self, url, stats, image):
self.url = url
self.image = image
self.crawl_stats = stats
self.item_stats = {'scraped': 0, 'dropped': 0, 'errors': 0}
@classmethod
def from_crawler(cls, crawler):
if not (url := crawler.settings.get('GOOGLE_CHAT_WEBHOOK')):
raise NotConfigured
image = crawler.settings.get('GOOGLE_CHAT_IMAGE', 'https://img.icons8.com/ios/452/spider.png')
ext = cls(url, crawler.stats, image)
crawler.signals.connect(ext.spider_closed, signal=signals.spider_closed)
crawler.signals.connect(ext.item_scraped, signal=signals.item_scraped)
crawler.signals.connect(ext.item_dropped, signal=signals.item_dropped)
crawler.signals.connect(ext.item_error, signal=signals.item_error)
return ext
def spider_closed(self, spider):
self.item_stats['total'] = sum([v for k, v in self.item_stats.items() if k != 'total'])
self.item_stats['duration'] = self._get_duration()
msg = self._get_message(spider.name, self.item_stats)
response = urlopen(Request(
self.url,
data=str.encode(json.dumps(msg)),
headers={'Content-Type': 'application/json; charset=UTF-8'},
method='POST'
))
def item_scraped(self, item, spider):
self.item_stats['scraped'] += 1
def item_dropped(self, item, spider):
self.item_stats['dropped'] += 1
def item_error(self, item, spider):
self.item_stats['errors'] += 1
def _get_duration(self):
duration = self.crawl_stats.get_value('finish_time') - self.crawl_stats.get_value('start_time')
total_seconds = int(duration.total_seconds())
hours, remainder = divmod(total_seconds, 60 * 60)
minutes, seconds = divmod(remainder, 60)
return f"{hours}h {minutes}m {seconds}s"
def _get_message(self, spider_name, stats):
return {
'cards': [
{
'header': {
"title": "Spider Report",
"subtitle": f'Name: {spider_name}',
"imageUrl": self.image,
"imageStyle": "IMAGE"
},
'sections': [
{
'widgets': [{
'textParagraph': {
'text': '<br>'.join([f'<b>{key}:</b> {value}' for key, value in stats.items()]),
}
}]
}
]
}
]
}