Skip to content

Commit

Permalink
Merge pull request #222 from pistachiostudio/daily-stock
Browse files Browse the repository at this point in the history
朝の投稿整え
  • Loading branch information
quojama authored Jan 31, 2024
2 parents 8a1e9c0 + 80ea521 commit a34698c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies = [
"spotipy~=2.23.0",
"httpx~=0.26.0",
"tenacity~=8.2.2",
"yfinance>=0.2.36",
]
readme = "README.md"
license = "MIT"
Expand Down
13 changes: 13 additions & 0 deletions requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
aiohttp==3.8.4
aiosignal==1.3.1
anyio==3.7.0
appdirs==1.4.4
async-timeout==4.0.2
attrs==23.1.0
beautifulsoup4==4.12.2
Expand All @@ -24,6 +25,7 @@ cryptography==41.0.1
discord-py==2.3.1
distlib==0.3.6
filelock==3.12.0
frozendict==2.4.0
frozenlist==1.3.3
google-api-core==2.11.0
google-api-python-client==2.88.0
Expand All @@ -33,17 +35,23 @@ google-auth-oauthlib==1.0.0
googleapis-common-protos==1.59.0
gspread==5.12.3
h11==0.14.0
html5lib==1.1
httpcore==1.0.2
httplib2==0.22.0
httpx==0.26.0
identify==2.5.24
idna==3.4
iniconfig==2.0.0
lxml==5.1.0
multidict==6.0.4
multitasking==0.0.11
nodeenv==1.8.0
numpy==1.26.3
oauth2client==4.1.3
oauthlib==3.2.2
packaging==23.1
pandas==2.2.0
peewee==3.17.0
platformdirs==3.5.1
pluggy==1.0.0
pre-commit==3.3.2
Expand All @@ -57,6 +65,8 @@ pyopenssl==23.2.0
pyparsing==3.0.9
pyproject-api==1.5.1
pytest==7.3.1
python-dateutil==2.8.2
pytz==2023.4
pyyaml==6.0
redis==4.5.5
requests==2.31.0
Expand All @@ -69,9 +79,12 @@ spotipy==2.23.0
tenacity==8.2.2
tox==4.6.0
tox-gh==1.2.0
tzdata==2023.4
uritemplate==4.1.1
urllib3==1.26.16
virtualenv==20.23.0
webencodings==0.5.1
yarl==1.9.2
yfinance==0.2.36
# The following packages are considered to be unsafe in a requirements file:
setuptools==67.8.0
13 changes: 13 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
aiohttp==3.8.4
aiosignal==1.3.1
anyio==3.7.0
appdirs==1.4.4
async-timeout==4.0.2
attrs==23.1.0
beautifulsoup4==4.12.2
Expand All @@ -19,6 +20,7 @@ cffi==1.15.1
charset-normalizer==3.1.0
cryptography==41.0.1
discord-py==2.3.1
frozendict==2.4.0
frozenlist==1.3.3
google-api-core==2.11.0
google-api-python-client==2.88.0
Expand All @@ -28,13 +30,19 @@ google-auth-oauthlib==1.0.0
googleapis-common-protos==1.59.0
gspread==5.12.3
h11==0.14.0
html5lib==1.1
httpcore==1.0.2
httplib2==0.22.0
httpx==0.26.0
idna==3.4
lxml==5.1.0
multidict==6.0.4
multitasking==0.0.11
numpy==1.26.3
oauth2client==4.1.3
oauthlib==3.2.2
pandas==2.2.0
peewee==3.17.0
protobuf==4.23.2
pyasn1==0.5.0
pyasn1-modules==0.3.0
Expand All @@ -43,6 +51,8 @@ pydrive2==1.18.1
pynacl==1.5.0
pyopenssl==23.2.0
pyparsing==3.0.9
python-dateutil==2.8.2
pytz==2023.4
pyyaml==6.0
redis==4.5.5
requests==2.31.0
Expand All @@ -53,6 +63,9 @@ sniffio==1.3.0
soupsieve==2.4.1
spotipy==2.23.0
tenacity==8.2.2
tzdata==2023.4
uritemplate==4.1.1
urllib3==1.26.16
webencodings==0.5.1
yarl==1.9.2
yfinance==0.2.36
25 changes: 21 additions & 4 deletions src/cogs/wt_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import discord
from discord.ext import commands, tasks

from libs.utils import get_exchange_rate, get_trivia, get_weather, get_what_today
from libs.utils import get_stock_price, get_trivia, get_weather, get_what_today


class WTTasks(commands.Cog):
Expand Down Expand Up @@ -38,13 +38,30 @@ async def printer(self):

# ChatGPTで雑学を取得
trivia = await get_trivia()
good_morning = random.choice(["おざし。", "おざす。"])
good_morning = random.choice(["おざし。", "おざす。", "お。", "おはようございます。"])

# USD/JPY
ticker_symbol = "USDJPY=X"
usd_jpy_day_before_ratio, usd_jpy_stock_today = get_stock_price(ticker_symbol)

# 日経平均
ticker_symbol = "^N225"
nikkei_day_before_ratio, nikkei_stock_today = get_stock_price(ticker_symbol)

# S&P500
ticker_symbol = "^GSPC"
sp500_day_before_ratio, sp500_stock_today = get_stock_price(ticker_symbol)

# NASDAQ
ticker_symbol = "^IXIC"
nasdaq_day_before_ratio, nasdaq_stock_today = get_stock_price(ticker_symbol)

market_text = f"- **USD/JPY:** {round(usd_jpy_stock_today, 1):,}円 ({usd_jpy_day_before_ratio})\n- **日経225:** {round(nikkei_stock_today, 1):,}円 ({nikkei_day_before_ratio})\n- **S&P500:** {round(sp500_stock_today, 1):,}pt ({sp500_day_before_ratio})\n- **NASDAQ:** {round(nasdaq_stock_today, 1):,}pt ({nasdaq_day_before_ratio})\n※()内は前日比。" # noqa: E501

embed = discord.Embed()
embed.set_footer(text=f"{weather}\n💵USD/JPY = {get_exchange_rate()}")
embed.color = discord.Color.green()
embed.title = f"{good_morning}{this_month}{this_day}日 朝の7時です。"
embed.description = f"**💡今日はなんの日?**\n{result}\n\n**📚今日の雑学**\n{trivia} (Powered by [gpt-4-1106-preview](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo))"
embed.description = f"### 💡 今日はなんの日?\n{result}\n### 📚 今日の雑学\n{trivia}\n(Powered by [gpt-4-1106-preview](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo))\n### 💹 相場\n{market_text}\n### ⛅ 天気\n{weather}" # noqa: E501
await channel.send(embed=embed)

# デプロイ後Botが完全に起動してからタスクを回す
Expand Down
23 changes: 22 additions & 1 deletion src/libs/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import re
from datetime import datetime, timedelta, timezone
Expand All @@ -7,6 +8,7 @@
import httpx
import requests
import tenacity
import yfinance as yf


def get_now_timestamp_jst() -> datetime:
Expand Down Expand Up @@ -60,7 +62,7 @@ def get_weather(citycode: int):
chanceOfRain_evening = json["forecasts"][0]["chanceOfRain"]["T12_18"]
chanceOfRain_night = json["forecasts"][0]["chanceOfRain"]["T18_24"]

result = f"{district}: {weather}\n☔ 朝: {chanceOfRain_morning} | 昼: {chanceOfRain_evening} | 晩: {chanceOfRain_night}"
result = f"- {district}: {weather}\n- ☔ 朝: {chanceOfRain_morning} | 昼: {chanceOfRain_evening} | 晩: {chanceOfRain_night}"

return result

Expand Down Expand Up @@ -120,3 +122,22 @@ async def fetch_data():
json = res.json()
answer = json["choices"][0]["message"]["content"]
return answer


def get_stock_price(ticker_symbol: str):
index = yf.Ticker(ticker_symbol)

data = index.history(period="2d")
data_json_with_date = data.to_json(orient="split", date_format="iso")
data_json = json.loads(data_json_with_date)

stock_yesterday = data_json["data"][0][3]
stock_today = data_json["data"][1][3]
day_before_ratio = round(stock_today - stock_yesterday, 1)

if day_before_ratio > 0:
day_before_ratio = f"+{day_before_ratio:,}"
else:
day_before_ratio = f"{day_before_ratio:,}"

return day_before_ratio, stock_today

0 comments on commit a34698c

Please sign in to comment.