Skip to content

Commit

Permalink
Merge pull request shidenggui#34 from wangyu190810/master
Browse files Browse the repository at this point in the history
添加分数图
  • Loading branch information
shidenggui authored Jul 26, 2017
2 parents db3c3b1 + 23895fc commit c587ae2
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,35 @@ quotation.etfindex(index_id="", min_volume=0, max_discount=None, min_discount=No
}
```


##### 分数图


*[腾讯分时图地址](http://data.gtimg.cn/flashdata/hushen/minute/sz000001.js)*

```
quotation = easyquotation.use("timekline")
data = quotation.market_snapshot(prefix=True)
```

**return**

```
{
'sh603828': {
'date': '170721', #日期
'time_data': {
'201707210930': ['0930', '19.42', '61'], # [时间, 当前价, 上一分钟到这一分钟之间的成交数量]
'201707210931': ['0931', '19.42','122'],
'201707210932': ['0932', '19.43', '123'],
'201707210933': ['0933', '19.48', '125'],
'201707210934': ['0934', '19.49', '133'],
'201707210935': ['0935', '19.48', '161'],
...
}
}
```



3 changes: 3 additions & 0 deletions easyquotation/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .leverfun import Leverfun
from .sina import Sina
from .tencent import Tencent
from .timekline import TimeKline

PY_VERSION = sys.version_info[:2]
if PY_VERSION < (3, 5):
Expand All @@ -23,3 +24,5 @@ def use(source):
return Tencent()
if source in ['boc']:
return Boc()
if source in ["timekline"]:
return TimeKline()
113 changes: 113 additions & 0 deletions easyquotation/timekline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# coding:utf8
import re
import time

import aiohttp
import asyncio
import easyutils
import yarl
from .basequotation import BaseQuotation

#url = "http://data.gtimg.cn/flashdata/hushen/minute/sz000001.js?maxage=110&0.28163905744440854"
#url = "http://data.gtimg.cn/flashdata/hushen/minute/%s.js?maxage=110&0.28163905744440854"



class TimeKline(BaseQuotation):
"""腾讯免费行情获取"""
stock_api = 'http://data.gtimg.cn/flashdata/hushen/minute/'
max_num = 1

def format_response_data(self, rep_data, prefix=False):
stocks_detail = ''.join(rep_data)
stock_details = stocks_detail.split(';')
stock_dict = dict()
for stock_detail in stock_details:
# print(stock_detail)
# print(stock_detail.split('~'))
stock_detail_split = stock_detail.split('~')
if len(stock_detail_split) == 2:
stock_code,ktime_line = stock_detail_split
else:
for row in stock_detail_split:
break
ktime_data = ktime_line.split('\n')
# print(ktime_data)
ktime_date = ktime_data[1].split(":")[1][:-3]
ktime_line_data = ktime_data[2:]
time_data_dict = {}
for row in ktime_data[2:]:
point_data = row.split(" ")
if len(point_data) < 3:
continue
else:
k_time = point_data[0]
k_price = point_data[1]
k_volume = point_data[2][:-3]
print_data_dict = {
"20" + ktime_date + k_time: [k_time,k_price,k_volume]
}
time_data_dict.update(print_data_dict)
stock_dict[stock_code] = {
"date" : ktime_date,
"time_data": time_data_dict
}

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'
}
url = yarl.URL(self.stock_api + params +".js", 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 params + "~"+ response_text
except asyncio.TimeoutError:
return None

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

for params in stock_list:
coroutine = self.get_stocks_by_range(params)
coroutines.append(coroutine)
# time.sleep(0.02)
# break
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)

def gen_stock_list(self, stock_codes):
stock_with_exchange_list = [easyutils.stock.get_stock_type(code) + code[-6:] for code in stock_codes]

if len(stock_with_exchange_list) < self.max_num:
request_list = ','.join(stock_with_exchange_list)
return [request_list]

stock_list = []
request_num = len(stock_codes) // self.max_num + 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])
stock_list.append(request_list)
return stock_list



if __name__ == "__main__":
data = TimeKline()
print(data)
4 changes: 4 additions & 0 deletions example/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import easyquotation
quotation = easyquotation.use("timekline")
data = quotation.market_snapshot(prefix=True)
print(data)
Loading

0 comments on commit c587ae2

Please sign in to comment.