Last active
December 17, 2018 13:00
-
-
Save zxyle/8fb9b61316187529077248773eab2330 to your computer and use it in GitHub Desktop.
query_tingpai.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Author: Zheng <me@zxyle.cn> | |
# Date: 12/15/18 | |
# Desc: 查询股票是否停牌 | |
# install: pip install tornado==5.1.1 | |
import json | |
import tornado.ioloop | |
import tornado.web | |
from tornado.httpclient import AsyncHTTPClient | |
async def f(url): | |
http_client = AsyncHTTPClient() | |
try: | |
response = await http_client.fetch(url) | |
except Exception as e: | |
print("Error: %s" % e) | |
else: | |
return response.body.decode("utf-8") | |
class MainHandler(tornado.web.RequestHandler): | |
async def get(self): | |
# 查询股票是否停牌 | |
self.set_header("Content-Type", "application/json; charset=UTF-8") | |
stock_id = self.get_argument("stock_id") | |
print("股票代码: {}".format(stock_id)) | |
if not stock_id: | |
self.finish(json.dumps({"message": "股票代码不能为空"}, ensure_ascii=False)) | |
else: | |
stock_id = str(stock_id).lower().replace("sh", "").replace("sz", "").replace(".", "") | |
company_id = "" | |
if str(stock_id).startswith("0"): | |
company_id = f"{stock_id}2" | |
elif str(stock_id).startswith("3"): | |
company_id = f"{stock_id}2" | |
elif str(stock_id).startswith("6"): | |
company_id = f"{stock_id}1" | |
token = "4f1862fc3b5e77c150a2b985b12db0fd" | |
url = "http://nuff.eastmoney.com/EM_Finance2015TradeInterface/JS.ashx?id={}&token={}".format(company_id, token) | |
response = await f(url) | |
resp = response[9:-1] | |
json_data = json.loads(resp) | |
c_id = json_data["Value"][44] | |
if int(c_id) == -1: | |
self.write(json.dumps({'status': True}, ensure_ascii=False)) | |
else: | |
self.write(json.dumps({'status': False}, ensure_ascii=False)) | |
if __name__ == "__main__": | |
application = tornado.web.Application([ | |
(r"/api/query_tingpai", MainHandler), | |
], debug=True) | |
application.listen(8888) | |
tornado.ioloop.IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment