This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cb_bot.py
226 lines (199 loc) · 7.26 KB
/
cb_bot.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# -*- coding: utf-8 -*-
import datetime
import logging
import time
from typing import Tuple, List, Dict
import requests
from environs import Env
from telegram.error import (
TelegramError,
Unauthorized,
BadRequest,
TimedOut,
ChatMigrated,
NetworkError,
)
from telegram.ext import (
CallbackContext,
Updater,
CommandHandler,
MessageHandler,
Filters,
)
from telegram.ext.jobqueue import Days
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)
logger = logging.getLogger(__name__)
def get_cb_info() -> Tuple[List, List]:
url = "https://www.jisilu.cn/data/cbnew/pre_list/"
response = requests.post(
url,
data={"cb_type_Y": "Y", "progress": "", "rp": 22},
headers={
"Origin": "https://www.jisilu.cn",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36",
"X-Requested-With": "XMLHttpRequest",
"Referer": "https://www.jisilu.cn/data/cbnew/",
"Accept": "application/json, text/javascript, */*; q=0.01",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept-Language": "en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7",
},
)
apply_cb = []
listed_cb = []
for row in response.json()["rows"]:
cell = row["cell"]
today = datetime.date.today().isoformat()
# 当日可申购债券
if cell["apply_date"] == today:
apply_cb.append(cell)
# 当日上市债券
elif cell["list_date"] == today:
listed_cb.append(cell)
return apply_cb, listed_cb
def start(updater: Updater, context: CallbackContext) -> None:
# context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!")
updater.message.reply_text("I'm a bot, please talk to me!")
def unknown(update: Updater, context: CallbackContext) -> None:
# context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
update.message.reply_text("Sorry, I didn't understand that command.")
def format_cell(cell: Dict[str, str]) -> str:
# text += f'证券代码: {cell["stock_id"]}, 债券代码: {cell["bond_id"]}, 申购代码: {cell["apply_cd"]}, 配售代码: {cell["ration_cd"]}, 名称: {cell["stock_nm"]}, 现价: {cell["price"]}, 发行规模(亿元): {cell["amount"]}, 中签率: {cell["lucky_draw_rt"]}%, '
# text += f'评级: {cell["rating_cd"]}, 申购建议: {cell["jsl_advise_text"]}, 单账户中签(定格): {cell["single_draw"]}\n\n'
lucky_draw_rt = (
escape_text(cell["lucky_draw_rt"]) + "%"
if cell["lucky_draw_rt"]
else escape_text("---")
)
return f"""
名 称: {escape_text(cell["stock_nm"] + '(' + cell["bond_nm"] + ')')}
债券代码: [{cell["bond_id"]}](https://www.jisilu.cn/data/convert_bond_detail/{cell["bond_id"]})
证券代码: [{cell["stock_id"]}](https://www.jisilu.cn/data/stock/{cell["stock_id"]})
现 价: {escape_text(cell["price"])}
中签率: {lucky_draw_rt}
评 级: {escape_text(cell["rating_cd"])}
申购建议: {escape_text(cell["jsl_advise_text"])}
"""
def escape_text(text: str) -> str:
if text:
for keyword in [
"_",
"*",
"[",
"]",
"(",
")",
"~",
"`",
">",
"#",
"+",
"-",
"=",
"|",
"{",
"}",
".",
"!",
]:
text = text.replace(keyword, f"\\{keyword}")
return text
return ""
def get_message_text() -> str:
today = datetime.date.today().isoformat()
logger.info(f"get message at {today}")
apply_cb, listed_cb = get_cb_info()
text = ""
text += f"*日期*: {escape_text(today)}\n\n"
if apply_cb:
text += "*当日可打新债*: \n"
for cell in apply_cb:
text += format_cell(cell)
else:
text += "*当日无可打新债*"
text += "\n\n"
if listed_cb:
text += "*当日上市新债*: \n"
for cell in listed_cb:
text += format_cell(cell)
else:
text += "*当日无上市新债*\n\n"
text += "\n_以上数据来源于互联网,仅供参考,不作为投资建议_ "
return text
def get_cb_trade_data(context: CallbackContext) -> None:
text = get_message_text()
logger.info(text)
retry_count = 0
while True:
try:
context.bot.send_message(
chat_id=context.job.context.get("channel_id"),
# parse_mode=ParseMode.MARKDOWN,
# note: hardcode, 当前SDK不支持MarkdwonV2模式
parse_mode="MarkdownV2",
text=text,
disable_web_page_preview=True,
timeout=5,
)
break
except Exception as e:
logger.exception(e)
if retry_count >= 3:
logger.info("Give up retrying...")
break
logger.info("Retrying after 10 seconds...")
time.sleep(10)
retry_count += 1
def error_callback(update: Updater, context: CallbackContext) -> None:
try:
raise context.error
except Unauthorized as e:
# remove update.message.chat_id from conversation list
logger.error(e)
except BadRequest as e:
# handle malformed requests - read more below!
logger.error(e)
except TimedOut as e:
# handle slow connection problems
logger.error(e)
except NetworkError as e:
# handle other connection problems
logger.error(e)
except ChatMigrated as e:
# the chat_id of a group has changed, use e.new_chat_id instead
logger.error(e)
except TelegramError as e:
# handle all other telegram related errors
logger.error(e)
def main() -> None:
env = Env()
# Read .env into os.environ
env.read_env()
token = env.str("BOT_TOKEN", None)
channel_id = env.str("CHANNEL_ID", None)
assert token is not None, "Please Set Bot Token"
assert channel_id is not None, "Please Set Channel id"
# channel_id必须以@符号开头
if not channel_id.startswith("@"):
channel_id = f"@{channel_id}"
updater = Updater(token=token, use_context=True)
dispatcher = updater.dispatcher
updater.job_queue.run_daily(
get_cb_trade_data,
# 北京时间9点触发
time=datetime.time(9 - 8, 00),
days=(Days.MON, Days.TUE, Days.WED, Days.THU, Days.FRI),
context={"channel_id": channel_id},
)
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)
# This handler must be added last. If you added it sooner, it would be triggered before the CommandHandlers had a chance to look at the update. Once an update is handled, all further handlers are ignored.
unknown_handler = MessageHandler(Filters.command, unknown)
dispatcher.add_handler(unknown_handler)
dispatcher.add_error_handler(error_callback)
updater.start_polling()
logger.info("Started Bot...")
updater.idle()
if __name__ == "__main__":
main()