-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBUGS.py
98 lines (78 loc) · 3.12 KB
/
BUGS.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
# -*- coding: utf-8 -*-
import json
import os
import re
import urllib.request
from bs4 import BeautifulSoup
from slackclient import SlackClient
from flask import Flask, request, make_response, render_template
app = Flask(__name__)
slack_token = ""
slack_client_id = ""
slack_client_secret = ""
slack_verification = ""
sc = SlackClient(slack_token)
# 크롤링 함수 구현하기
def _crawl_naver_keywords(text):
url = "http://music.bugs.co.kr"
soup = BeautifulSoup(urllib.request.urlopen(url).read(), "html.parser")
#여기에 함수를 구현해봅시다.
keywords=[]
title=[]
artist=[]
if "bugs" in text:
for data in soup.find_all("p",class_="title"):
#10위까지 크롤링
if len(title) >= 10:
break
title.append(data.get_text().strip())
for data in soup.find_all("p",class_="artist"):
#10위까지 크롤링
if len(artist)>=10:
break
artist.append(data.get_text().strip())
keywords = ["bugs 실시간 음악 차트 TOP 10"]
for i in range(1,11):
keywords.append(str(i)+"위 : " + title[i-1] + "/" + artist[i-1])
# 한글 지원을 위해 앞에 unicode u를 붙혀준다.
return u'\n'.join(keywords)
# 이벤트 핸들하는 함수
def _event_handler(event_type, slack_event):
print(slack_event["event"])
if event_type == "app_mention":
channel = slack_event["event"]["channel"]
text = slack_event["event"]["text"]
keywords = _crawl_naver_keywords(text)
sc.api_call(
"chat.postMessage",
channel=channel,
text=keywords
)
return make_response("App mention message has been sent", 200,)
# ============= Event Type Not Found! ============= #
# If the event_type does not have a handler
message = "You have not added an event handler for the %s" % event_type
# Return a helpful error message
return make_response(message, 200, {"X-Slack-No-Retry": 1})
@app.route("/listening", methods=["GET", "POST"])
def hears():
slack_event = json.loads(request.data)
if "challenge" in slack_event:
return make_response(slack_event["challenge"], 200, {"content_type":
"application/json"
})
if slack_verification != slack_event.get("token"):
message = "Invalid Slack verification token: %s" % (slack_event["token"])
make_response(message, 403, {"X-Slack-No-Retry": 1})
if "event" in slack_event:
event_type = slack_event["event"]["type"]
return _event_handler(event_type, slack_event)
# If our bot hears things that are not events we've subscribed to,
# send a quirky but helpful error response
return make_response("[NO EVENT IN SLACK REQUEST] These are not the droids\
you're looking for.", 404, {"X-Slack-No-Retry": 1})
@app.route("/", methods=["GET"])
def index():
return "<h1>Server is ready.</h1>"
if __name__ == '__main__':
app.run('0.0.0.0', port=8080)