Skip to content

Commit

Permalink
sinaapi
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuehui Ye committed Oct 14, 2019
1 parent 85e18ac commit 5e9a6f1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
9 changes: 6 additions & 3 deletions api/sinaApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def getProfitsByCode(ts_code='000007.SZ', yearNum=1):
headers = {'Referer': f'http://quotes.sina.cn/cn/view/finance_app_detail.php?symbol={symbol}' }
res = requests.get(url,headers=headers).text
json_str = res.split('\n')[1][len(callback)+1:]
rows = json.loads(json_str).result.data.report_list
rows = json.loads(json_str)['result']['data']['report_list']
for end_date,row in rows.items():
ann_date = row['publish_date']
for item in row['data']:
Expand All @@ -28,11 +28,14 @@ def getLevel(ts_code='000007.SZ', yearNum=1):
url = f'http://stock.finance.sina.com.cn/stock/api/openapi.php/StockMixService.khdGetStockComment?extra=mbj,ylyc,ndpj&chwm=32040_0002&device_id_fake=250213b90b10239d&imei=355212349711234&wm=b122&device_id_spns=250213b90b10239d&version=4.7.0.2&device_id_old=250213b90b10239d&from=7047095012&deviceid=209049f05b094d65&symbol={symbol}'
res = requests.get(url).json()
jgpg = res['result']['data']['jgpj']
mbj = res.result.data.mbj
avg = mbj.avg
mbj= res['result']['data']['mbj']
avg = mbj['avg']
level = 0
print(jgpg)
if len(jgpg)>0:
jgpg = jgpg[0]
for k in ['sell_counts','reduce_counts','neutral_counts']:
jgpg[k] = int(jgpg[k])
if jgpg['sell_counts']+jgpg['reduce_counts']>0:
level = jgpg['buy_counts']+jgpg['hold_counts']-jgpg['neutral_counts']
return {
Expand Down
2 changes: 1 addition & 1 deletion db/keyvDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def getKey(key, expire=86400):

def addKey(key,v):
v = json.dumps(v)
cursor.insertUpdate('keyvdb', {'key':key, 'v':v,'time':datetime.now()})
cursor.insertUpdate('keyvdb', {'key':key, 'v':v,'time':datetime.now()}, 'key')

'''
永久存储型Cache
Expand Down
6 changes: 3 additions & 3 deletions db/priceDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def addBatch(price_list):
cursor.insertBatch("prices", price_list)

def getPriceMean(code, trade_date, nday=60):
cursor.execute(f"select price from prices where code=%s, trade_date=%s limit {nday}", [code, trade_date, ])
rows = cursor.fetchall()
cursor.execute(f"select close from prices where code=%s and trade_date<=%s limit {nday}", [code, trade_date, ])
rows = [dict(row) for row in cursor.fetchall()]
if rows:
df = pd.DataFrame(rows)
return df['price'].sum()/nday
return df['close'].sum()/nday

def getLatestPriceRow(code):
cursor.execute("select close from prices where code=%s order by trade_date desc limit 1", [code])
Expand Down
38 changes: 22 additions & 16 deletions db/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def parse_basic_by_day(df, ann_date):
from db import preprofitDb
def listen_disclosure(code):
df = pro.disclosure_date(ts_code=code, end_date=TODAY)
if df:
if not df.empty:
row = df.iloc[0,['ts_code','end_date','pre_date',]].rename(index={'ts_code':'code','pre_date':'ann_date'});
if row.end_date >= next_end_date:
row = row.to_dict()
Expand Down Expand Up @@ -273,11 +273,11 @@ def pullProfitCode(ts_code):

# metaDb.addMeta(df[profitCols])



def showCode():
def parseCodes():
ts_codes = Args.code
codes = []
if len(ts_codes)==0:
return codes
for ts_code in ts_codes.split(","):
if re.match(r"\d{6}", ts_code):
if "." not in ts_code:
Expand All @@ -287,18 +287,23 @@ def showCode():
if ts_code:
pullProfitCode(ts_code)
codes.append(ts_code)
return codes

def showCode():
codes = parseCodes()
for ts_code in codes:
profitDb.showCode(ts_code)
df = pro.express(ts_code=ts_code, start_date='20180101', end_date='20191201', fields='ts_code,ann_date,end_date,revenue,operate_profit,total_profit,n_income,total_assets')
# df = pro.express(ts_code='600000.SH', start_date='20190101', end_date='20191201', fields='ts_code,ann_date,end_date,revenue,operate_profit,total_profit,n_income,total_assets')
print('6000',df)
# df = pro.express(ts_code=ts_code, start_date='20180101', end_date='20191201', fields='ts_code,ann_date,end_date,revenue,operate_profit,total_profit,n_income,total_assets')
# print('6000',df)

def getGood():
def getGood(codes=[]):
from db.conn import cursor
sql = f"select p.*,metas.name from (select distinct on (code) code,end_date,pe,peg,dny,q_dtprofit_yoy,dtprofit_yoy,buy from profits order by code,end_date desc) p join metas on metas.code=p.code where p.peg<0.5 and p.peg>0 and p.buy=1 and q_dtprofit_yoy>0 order by p.peg"
where_codes = ''
if len(codes):
where_codes = ' where code in (%s) ' % ','.join(["'"+code+"'" for code in codes])
sql = f"select p.*,metas.name from (select distinct on (code) code,end_date,pe,peg,dny,q_dtprofit_yoy,dtprofit_yoy,buy from profits {where_codes} order by code,end_date desc) p join metas on metas.code=p.code where p.peg<0.5 and p.peg>0 and p.buy=1 and q_dtprofit_yoy>0 order by p.peg"
cursor.execute(sql)
rows = list(cursor)
rows = [dict(row) for row in cursor]
for row in rows:
code = row['code']
# 财报(未来数据 )
Expand All @@ -310,25 +315,26 @@ def getGood():
row['price'] = priceDb.getLatestPrice(code)
# mean 60
row['mean'] =priceDb.getPriceMean(row['code'], TODAY)
print(pd.DataFrame(rows))
print('goodp',pd.DataFrame(rows))

if __name__ == "__main__":
print(Args)
# if Args.code: single()
if Args.code:
showCode()
elif Args.cmd == "genlist":
if Args.cmd == "genlist":
# exec(Args.cmd)
# globals()[Args.cmd]()
genlist()
elif Args.cmd == "genprice":
genprice("601318.SH")
elif Args.cmd == "pullProfit":
pullProfit()
elif Args.cmd == "getGood":
getGood()
elif Args.cmd == "good":
codes = parseCodes()
getGood(codes)
elif Args.cmd == "sync":
sync()
# pull()
elif Args.code:
showCode()
quit("done")

0 comments on commit 5e9a6f1

Please sign in to comment.