Showing
1 changed file
with
80 additions
and
0 deletions
covid.py
0 → 100644
1 | +import telegram | ||
2 | +from telegram.ext import Updater | ||
3 | +from telegram.ext import MessageHandler, Filters | ||
4 | +from bs4 import BeautifulSoup | ||
5 | +from selenium import webdriver | ||
6 | +import urllib.request as req | ||
7 | +import os | ||
8 | +from selenium.webdriver.support.ui import WebDriverWait | ||
9 | +from selenium.webdriver.support import expected_conditions as EC | ||
10 | +from selenium.webdriver.common.by import By | ||
11 | + | ||
12 | + | ||
13 | +#크롤링 관련 함수 | ||
14 | +def covid_num_crawling(): | ||
15 | + code = req.urlopen("https://search.naver.com/search.naver?where=nexearch&sm=top_hty&fbm=0&ie=utf8&query=%ED%99%95%EC%A7%84%EC%9E%90") | ||
16 | + soup = BeautifulSoup(code, "html.parser") | ||
17 | + info_num = soup.select("div.status_today em") | ||
18 | + result = int(info_num[0].string) + int(info_num[1].string) | ||
19 | + return result | ||
20 | + | ||
21 | +def covid_news_crawling(): | ||
22 | + code = req.urlopen("https://search.naver.com/search.naver?where=news&sm=tab_jum&query=%EC%BD%94%EB%A1%9C%EB%82%98") | ||
23 | + soup = BeautifulSoup(code, "html.parser") | ||
24 | + title_list = soup.select("a.news_tit") | ||
25 | + output_result = "" | ||
26 | + for i in title_list: | ||
27 | + title = i.text | ||
28 | + news_url = i.attrs["href"] | ||
29 | + output_result += title + "\n" + news_url + "\n\n" | ||
30 | + if title_list.index(i) == 2: | ||
31 | + break | ||
32 | + return output_result | ||
33 | + | ||
34 | + | ||
35 | +#텔레그램 관련 코드 | ||
36 | +token = "1721885449:AAHDGMbjSJfhXxML6nfSCpfiU7SghpL_vOE" | ||
37 | +id = "1657858421" | ||
38 | +bot = telegram.Bot(token) | ||
39 | + | ||
40 | +info_message = '''진세 진세, 오늘 코로나 확진자 수 몇명인지 알아?? 완전 무섭다,,, (#몇명인데?)''' | ||
41 | +bot.sendMessage(chat_id=id, text=info_message) | ||
42 | + | ||
43 | + | ||
44 | +news_message = '''관련 뉴스도 엄청 많이 보도되고 있더라,,, (#뉴스)''' | ||
45 | + | ||
46 | +worried_message = '''진세도 꼭 마스크 끼고 손 씻고 주의해!! (#웅웅 너도)''' | ||
47 | + | ||
48 | +react01_message = '''오키 ㅎㅎ''' | ||
49 | + | ||
50 | + | ||
51 | + | ||
52 | + | ||
53 | +updater = Updater(token=token, use_context=True) | ||
54 | +dispatcher = updater.dispatcher | ||
55 | +updater.start_polling() | ||
56 | + | ||
57 | + | ||
58 | +# 챗봇 응답 | ||
59 | +def handler(update, context): | ||
60 | + user_text = update.message.text # 사용자가 보낸 메세지를 user_text 변수에 저장합니다. | ||
61 | + | ||
62 | + # 오늘 확진자 수 답장 | ||
63 | + if (user_text == "몇명인데?"): | ||
64 | + covid_num = covid_num_crawling() | ||
65 | + bot.send_message(chat_id=id, text="오늘 확진자 수 : {} 명".format(covid_num)) | ||
66 | + bot.sendMessage(chat_id=id, text=news_message) | ||
67 | + | ||
68 | + # 코로나 관련 뉴스 답장 | ||
69 | + elif (user_text == "뉴스"): | ||
70 | + covid_news = covid_news_crawling() | ||
71 | + bot.send_message(chat_id=id, text=covid_news) | ||
72 | + bot.sendMessage(chat_id=id, text=worried_message) | ||
73 | + | ||
74 | + elif (user_text == "웅웅 너도"): | ||
75 | + bot.sendMessage(chat_id=id, text=react01_message) | ||
76 | + | ||
77 | +echo_handler = MessageHandler(Filters.text, handler) | ||
78 | +dispatcher.add_handler(echo_handler) | ||
79 | + | ||
80 | + |
-
Please register or login to post a comment