Python
200819-,텔레그램 챗봇, 멜론차트 , 테스트 001
sucun
2020. 8. 19. 17:04
melon_rank.py
0.00MB
testBot001.py
0.00MB
testBot001.py
# import modules
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters,)
import logging
from melon_rank import show_music_rank
# print(show_music_rank())
print('kase',show_music_rank)
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
#My bot token from BotFather
token = '1356043789:AAG3egnGTbnNdpvDmmLunlns-OJHUFoxLS8'
# define command handlers
def start(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="봇이 정상 작동 합니다.")
# 정해진 커맨드가 아닌 다른 명령어를 받았을때 출력할 메시지
def unknown(bot, update):
bot.send_message(chat_id=update.message.chat_id, text="죄송하지만 ,명령어를 이해 할수 없습니다.")
# main 문을 정의
def main():
# Create Updater object and attach dispatcher to it
updater = Updater(token)
dp= updater.dispatcher
print("Bot started")
#Start the bot
updater.start_polling()
dp.add_handler(CommandHandler('start', start))
## 현재 한국어 코멘드가 들어가지 않음.
dp.add_handler(CommandHandler('melon_rank', show_music_rank))
dp.add_handler(MessageHandler(Filters.command, unknown))
# Run the bot until you press Ctrl-C
updater.idle()
updater.stop()
if __name__ == '__main__':
main()
ps: 현재 한국어 지원 안함
melon_rank.py
import time ,re , timeit
import pandas as pd
from selenium import webdriver
from bs4 import BeautifulSoup
from bs4 import SoupStrainer
from selenium import webdriver
from datetime import datetime
options = webdriver.ChromeOptions()
options.add_argument('headless')
options.add_argument('window-size=1920x1080')
options.add_argument("disable-gpu")
# 혹은 options.add_argument("--disable-gpu")
driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe", options = options)
# driver = webdriver.Chrome("C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe")
def show_music_rank(self,update):
# def show_music_rank(self):
addr = 'https://www.melon.com/chart/index.htm'
driver.get(addr)
melon = driver.page_source
soup = BeautifulSoup(melon, 'html.parser')
title = soup.select('#frm > div div.ellipsis.rank01 > span > a')
artist = soup.select('#frm > div div.ellipsis.rank02 > span > a')
titles = []
for i,j in enumerate(title):
if i < 10:
tts = str(i) + ' ' + j.get_text()
titles.append(tts)
artists = []
for i, j in enumerate(artist):
if i < 10:
tts = j.get_text()
artists.append(tts)
key_val = [titles, artists]
# rank_text = dict(zip(*key_val))
update.message.reply_text('실시간 멜론 차트\n'
+ '1 위: ' + '제목: ' + titles[0] +'-' '아티스트: ' + artists[0] + '\n'
+ '2 위: ' + '제목: ' + titles[1] +'-' '아티스트: ' + artists[1] + '\n'
+ '3 위: ' + '제목: ' + titles[2] +'-' '아티스트: ' + artists[2] + '\n'
+ '4 위: ' + '제목: ' + titles[3] +'-' '아티스트: ' + artists[3] + '\n'
+ '5 위: ' + '제목: ' + titles[4] +'-' '아티스트: ' + artists[4] + '\n'
+ '6 위: ' + '제목: ' + titles[5] +'-' '아티스트: ' + artists[5] + '\n'
+ '7 위: ' + '제목: ' + titles[6] +'-' '아티스트: ' + artists[6] + '\n'
+ '8 위: ' + '제목: ' + titles[7] +'-' '아티스트: ' + artists[7] + '\n'
+ '9 위: ' + '제목: ' + titles[8] +'-' '아티스트: ' + artists[8] + '\n'
+ '10 위: '+ '제목: ' + titles[9] +'-' '아티스트: ' + artists[9] + '\n'
)
반응형