-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs2slack.py
executable file
·110 lines (85 loc) · 3.38 KB
/
rs2slack.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import configparser
import logging
import logging.config
import time
import urllib.parse
import lxml.html
import redis
import requests
from slackclient import SlackClient
CONFIG_FILE = "rs2slack.config"
REALTIME_PREFIX = "http://realtime.search.yahoo.co.jp/search?p="
REALTIME_SUFFIX = "&ei=UTF-8"
XPATH_TWEET = "//*[@id=\"TSm\"]/div/div/p[1]/a[2]"
CRAWL_INTERVAL = 60
REDIS_HASH_NAME = "tweet-history"
class RealtimeSearchToSlack(object):
# magic methods
def __init__(self):
logging.config.fileConfig(CONFIG_FILE)
cp = configparser.ConfigParser()
cp.read(CONFIG_FILE)
section = "rs2slack"
self.keyword = cp.get(section, "keyword")
self.redis_host = cp.get(section, "redis_host")
self.redis_port = cp.getint(section, "redis_port")
self.redis_db = cp.getint(section, "redis_db")
self.slack_token = cp.get(section, "slack_token")
self.slack_channel_id = cp.get(section, "slack_channel_id")
logging.debug("keyword:{0}".format(self.keyword))
logging.debug("redis_host:{0}".format(self.redis_host))
logging.debug("redis_port:{0}".format(self.redis_port))
logging.debug("redis_db:{0}".format(self.redis_db))
logging.debug("slack_token:{0}".format(self.slack_token))
logging.debug("slack_channel_id:{0}".format(self.slack_channel_id))
# public methods
def start(self):
first_run = True
while True:
try:
logging.debug("start crawling...")
urls = self.crawl_yahoo_realtime()
logging.debug("crawled {0} tweets.".format(len(urls)))
if first_run:
self.update_tweet_history(urls)
first_run = False
for url in urls:
if self.should_post(url):
self.post_slack(self.slack_token, self.slack_channel_id, url)
logging.debug("posted: {0}".format(url))
time.sleep(5)
self.update_tweet_history(url)
except Exception as e:
# XXX: ignores all exceptions for now
logging.error(e)
finally:
logging.debug("waiting for next crawl...")
time.sleep(CRAWL_INTERVAL)
# private methods
def crawl_yahoo_realtime(self):
url = REALTIME_PREFIX + urllib.parse.quote_plus(self.keyword) + REALTIME_SUFFIX
logging.debug(url)
request = requests.get(url)
if request.status_code != 200:
return []
dom = lxml.html.fromstring(request.content)
elements = dom.xpath(XPATH_TWEET)
return [element.get('href') for element in elements]
def should_post(self, url):
return not self.redis_client().hexists(REDIS_HASH_NAME, url)
def update_tweet_history(self, url):
if isinstance(url, list):
urls = url
else:
urls = [url]
redis = self.redis_client()
for u in urls:
redis.hset(REDIS_HASH_NAME, u, 1)
def redis_client(self):
return redis.Redis(host=self.redis_host, port=self.redis_port, db=self.redis_db)
def post_slack(self, token, channel, url):
sc = SlackClient(token)
res = sc.api_call("chat.postMessage", channel=channel, text=url)
RealtimeSearchToSlack().start()