cord0318 / stock-bot Goto Github PK
View Code? Open in Web Editor NEW주식 정보를 가져다 주는 디스코드 봇 입니다.
주식 정보를 가져다 주는 디스코드 봇 입니다.
해외주식은 못보나요? 그리고 종목코드로는 조회를 할 수 있게 해주세요.
해외주식은 못보나요? 그리고 종목코드로는 조회를 할 수 있게 해주세요.
import discord
from discord.ext import commands
from bs4 import BeautifulSoup
import warnings
from urllib.request import URLError
from urllib.request import HTTPError
from urllib.request import urlopen
from urllib.request import Request, urlopen
import FinanceDataReader as fdr
import re
app = commands.Bot(command_prefix='!') # command_prefix에 자신이 원하는 접두사를 적어주세요 ex)!주식
token = "" # 봇의 토큰을 적어주세요.
game = "" # 자신이 원하는 상태 메시지를 적어주세요.
def stock_code(stock_name): # 주식 이름을 주면 주식 코드를 주는 함수
df_krx = fdr.StockListing('KRX')
name = df_krx[df_krx['Name']==stock_name]
symbol = name.iloc[0, 0]
return symbol
@app.event
async def on_ready():
print("Start Discord Bot")
print(app.user.name)
print(app.user.id)
print(app.user)
print("Made By Jung Ji-Hyo (cord)")
await app.change_presence(status=discord.Status.online, activity=discord.Game(game))
print("==================")
@app.command() # 주식 디스코드 봇 작동
async def 주식(ctx, stock_name=None):
if stock_name is not None:
try:
code = stock_code(stock_name)
embed = discord.Embed(colour=discord.Colour.red())
embed.set_author(name=f"{stock_name}({code})의 주식 정보를 가져오는중..")
msg = await ctx.send(embed=embed)
url = f'https://finance.naver.com/item/sise.nhn?code={code}'
html = urlopen(url)
bs = BeautifulSoup(html, 'html.parser')
price = bs.find('strong', {'class':'tah p11'}, {'id':'_nowVal'}).text.split('</span>')
price = ''.join(price)
rate = bs.find('strong', {'id':'_rate'}).text.split('</span>')
rate = '\n'.join(rate)
rate = rate.replace('\n', '')
rate = rate.replace('\t', '')
compared = bs.find('strong', {'id':'_diff'}).text.split('</span>')
compared = '\n'.join(compared)
compared = compared.replace('\n', '')
compared = compared.replace('\t', '')
if bs.find('em', {'class':'bu_p bu_pdn'}):
check = '🔻'
compared = compared.replace('하락', '')
else:
check = '🔺'
compared = compared.replace('상승', '')
sell = bs.find('span', {'id':'_quant'}).text.split('</span>')
sell = '\n'.join(sell)
sell = sell.replace('\n', '')
sell = sell.replace('\t', '')
amount = bs.find('span', {'id':'_amount'}).text.split('</span>')
amount = '\n'.join(amount)
amount = amount.replace('\n', '')
amount = amount.replace('\t', '')
embed = discord.Embed(colour=discord.Colour.green())
embed.set_author(name=f"{stock_name} ({code})", url=f'https://finance.naver.com/item/sise.nhn?code={code}')
embed.add_field(name="현재가", value=price)
embed.add_field(name="전일대비", value=f"{check}{compared}")
embed.add_field(name="등락률", value=rate)
embed.add_field(name="거래량", value=sell)
embed.add_field(name="거래대금(백만)", value=amount)
embed.set_thumbnail(url=f"https://ssl.pstatic.net/imgfinance/chart/item/area/day/{code}.png")
embed.set_image(url=f"https://ssl.pstatic.net/imgfinance/chart/item/area/day/{code}.png")
await msg.edit(embed=embed)
except Exception as e:
embed = discord.Embed(colour=discord.Colour.red())
embed.add_field(name="Error", value=e)
await ctx.send(embed=embed)
app.run(token)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.