Skip to content

Commit

Permalink
🐛 fix daykline api error
Browse files Browse the repository at this point in the history
  • Loading branch information
shidenggui committed Apr 9, 2018
1 parent 06b9e53 commit 8422b04
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 132 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: python

python:
- "3.4"
- "3.5"
- "3.6"

env:
- PROJ=${PWD##*/}

install:
- pip install -r test-requirements.txt

script:
- pytest --cov=$PROJ -v tests
19 changes: 13 additions & 6 deletions easyquotation/basequotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ def gen_stock_list(self, stock_codes):
return [request_list]

stock_list = []
request_num = len(stock_codes) // self.max_num + 1
request_num = len(stock_codes) // (self.max_num + 1) + 1
for range_start in range(request_num):
num_start = self.max_num * range_start
num_end = self.max_num * (range_start + 1)
request_list = ','.join(stock_with_exchange_list[num_start:num_end])
request_list = ','.join(
stock_with_exchange_list[num_start:num_end])
stock_list.append(request_list)
return stock_list

def _gen_stock_prefix(self, stock_codes):
return [easyutils.stock.get_stock_type(code) + code[-6:] for code in stock_codes]
return [
easyutils.stock.get_stock_type(code) + code[-6:]
for code in stock_codes
]

@staticmethod
def load_stock_codes():
Expand Down Expand Up @@ -79,8 +83,10 @@ def market_snapshot(self, prefix=False):

def get_stocks_by_range(self, params):
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36'
'Accept-Encoding':
'gzip, deflate, sdch',
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36'
}

r = self._session.get(self.stock_api + params, headers=headers)
Expand All @@ -92,7 +98,8 @@ def get_stock_data(self, stock_list, **kwargs):
res = pool.map(self.get_stocks_by_range, stock_list)
finally:
pool.close()
return self.format_response_data([x for x in res if x is not None], **kwargs)
return self.format_response_data([x for x in res if x is not None],
**kwargs)

def format_response_data(self, rep_data, **kwargs):
pass
84 changes: 14 additions & 70 deletions easyquotation/daykline.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# coding:utf8
import asyncio
import json

import aiohttp
import yarl
import re

from .basequotation import BaseQuotation

"""
url = "http://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_dayqfq&param=hk00001,day,,,660,qfq&r=0.7773272375526847"
Expand All @@ -23,77 +19,25 @@

class DayKline(BaseQuotation):
"""腾讯免费行情获取"""
stock_api = "http://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_dayqfq&param=hk%s,day,,,%s,qfq&r=0.7773272375526847"
stock_api = 'http://web.ifzq.gtimg.cn/appstock/app/hkfqkline/get?_var=kline_dayqfq&param='
max_num = 1

def format_response_data(self, rep_data, prefix=False):
stocks_detail = ''.join(rep_data)
stock_detail_split = stocks_detail.split('kline_dayqfq=')
stock_dict = dict()
for daykline in stock_detail_split:
try:
daykline = json.loads(daykline)
except Exception as e:
print(e)
continue

status_code = daykline.get("code")
if status_code not in ("0", 0):
# 当返回错误状态码时候,不做处理
continue
daykline = daykline.get("data")
for key, value in daykline.items():
stock_code = key[2:]

_stmt = value.get('qfqday')
if _stmt is None:
_stmt = value.get('day')
if _stmt is None:
continue
def _gen_stock_prefix(self, stock_codes, day=1500):
return ['hk{},day,,,{},qfq'.format(code, day) for code in stock_codes]

stock_dict[stock_code] = _stmt
def format_response_data(self, rep_data, prefix=False):
stock_dict = {}
for raw_quotation in rep_data:
raw_stocks_detail = re.search(r'=(.*)', raw_quotation).group(1)
stock_details = json.loads(raw_stocks_detail)
for stock, value in stock_details['data'].items():
stock_code = stock[2:]
stock_detail = value['qfqday']
stock_dict[stock_code] = stock_detail
break

return stock_dict

async def get_stocks_by_range(self, *params):
if self._session is None:
self._session = aiohttp.ClientSession()
headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36'
}
print(params)
if len(params) > 1:
stock_code = params[0]
days = params[1]
else:
stock_code = params
days = 360
url = yarl.URL(self.stock_api % (stock_code, days), encoded=True)
print(url)
try:
async with self._session.get(url, timeout=10, headers=headers) as r:
asyncio.sleep(0.1)
response_text = await r.text()
# print(response_text)
return response_text
except asyncio.TimeoutError:
return ''

def get_stock_data(self, stock_list, days=360, **kwargs):
coroutines = []

for params in stock_list:
coroutine = self.get_stocks_by_range(params, days)
coroutines.append(coroutine)
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
res = loop.run_until_complete(asyncio.gather(*coroutines))
return self.format_response_data([x for x in res if x is not None], **kwargs)


if __name__ == "__main__":
pass
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
six
requests
yarl
aiohttp>=1.1.1
aiohttp==1.1.1
easyutils
55 changes: 0 additions & 55 deletions test_easyquotation.py

This file was deleted.

1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# coding:utf8
79 changes: 79 additions & 0 deletions tests/test_easyquotation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# coding:utf8
import unittest

import easyquotation


class TestEasyquotation(unittest.TestCase):
def test_stock_code_with_prefix(self):
cases = ['sina', 'qq']
for src in cases:
q = easyquotation.use(src)
data = q.all_market
for k in data.keys():
self.assertRegex(k, r'(sh|sz)\d{6}')

def test_all(self):
cases = ['sina', 'qq']
for src in cases:
q = easyquotation.use(src)
data = q.all
for k in data.keys():
self.assertRegex(k, r'\d{6}')


class TestHqouteQuotatin(unittest.TestCase):
MOCK_RESPONSE_DATA = 'v_r_hk00700="100~腾讯控股~00700~409.600~412.200~414.000~41115421.0~0~0~409.600~0~0~0~0~0~0~0~0~0~409.600~0~0~0~0~0~0~0~0~0~41115421.0~2018/03/29 16:08:11~-2.600~-0.63~417.000~405.200~409.600~41115421.0~16899465578.300~0~44.97~~0~0~2.86~38909.443~38909.443~TENCENT~0.21~476.600~222.400~0.40~0~0~0~0~0~0~42.25~12.70~"; v_r_hk00980="100~联华超市~00980~2.390~2.380~2.380~825000.0~0~0~2.390~0~0~0~0~0~0~0~0~0~2.390~0~0~0~0~0~0~0~0~0~825000.0~2018/03/29 16:08:11~0.010~0.42~2.440~2.330~2.390~825000.0~1949820.000~0~-5.38~~0~0~4.62~8.905~26.758~LIANHUA~0.00~4.530~2.330~1.94~0~0~0~0~0~0~-0.01~0.94~";'

def setUp(self):
self._obj = easyquotation.use('hkquote')

def test_format_response_data(self):
excepted = {
'00700': {
'amount': 41115421.0,
'high': 417.0,
'lastPrice': 412.2,
'lotSize': 100.0,
'low': 405.2,
'name': '腾讯控股',
'openPrice': 414.0,
'price': 409.6,
'time': '2018/03/29 16:08:11'
},
'00980': {
'amount': 825000.0,
'high': 2.44,
'lastPrice': 2.38,
'lotSize': 100.0,
'low': 2.33,
'name': '联华超市',
'openPrice': 2.38,
'price': 2.39,
'time': '2018/03/29 16:08:11'
}
}
result = self._obj.format_response_data(self.MOCK_RESPONSE_DATA)
self.assertDictEqual(result, excepted)


class TestDayklineQuotatin(unittest.TestCase):
MOCK_RESPONSE_DATA = [
'kline_dayqfq={"code":0,"msg":"","data":{"hk00001":{"qfqday":[["2018-04-09","91.00","91.85","93.50","91.00","8497462.00"]],"qt":{"hk00001":["100","\u957f\u548c","00001","91.850","91.500","91.000","8497462.0","0","0","91.850","0","0","0","0","0","0","0","0","0","91.850","0","0","0","0","0","0","0","0","0","8497462.0","2018\/04\/09 16:08:10","0.350","0.38","93.500","91.000","91.850","8497462.0","781628889.560","0","10.09","","0","0","2.73","3543.278","3543.278","CKH HOLDINGS","3.10","108.900","91.000","1.89","0","0","0","0","0","0","7.67","0.10",""],"market":["2018-04-09 22:36:01|HK_close_\u5df2\u6536\u76d8|SH_close_\u5df2\u6536\u76d8|SZ_close_\u5df2\u6536\u76d8|US_open_\u4ea4\u6613\u4e2d|SQ_close_\u5df2\u4f11\u5e02|DS_close_\u5df2\u4f11\u5e02|ZS_close_\u5df2\u4f11\u5e02"]},"prec":"91.50","vcm":"","version":"4"}}}'
]

def setUp(self):
self._obj = easyquotation.use('daykline')

def test_format_response_data(self):
excepted = {
"00001":
[["2018-04-09", "91.00", "91.85", "93.50", "91.00", "8497462.00"]]
}

result = self._obj.format_response_data(self.MOCK_RESPONSE_DATA)
self.assertDictEqual(result, excepted)


if __name__ == '__main__':
unittest.main()

0 comments on commit 8422b04

Please sign in to comment.