-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sungmin(오성민)
committed
Jan 20, 2017
0 parents
commit 1123905
Showing
4 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
## 주가 확인 Alfred Workflow | ||
|
||
간편하게 주가를 확인하기 위한 Alfred workflow입니다 | ||
|
||
### 사용법 | ||
|
||
`s` + `검색어` | ||
|
||
- `enter` 를 누르면 [네이버 금융](http://finance.naver.com/) 의 검색결과를 보여줍니다. | ||
- 펀드나, 환율의 경우는 실시간 정보를 보여주지 않습니다. | ||
|
||
|
||
------------- | ||
|
||
## Stock Price Checker | ||
|
||
this is an Alfred workflow for checking realtime stock price of Korean market. | ||
|
||
#### Usage | ||
|
||
`s`+`query` | ||
|
||
- You can see [Naver finance](http://finance.naver.com/) page by `enter` key. | ||
- No detail information for funds and currencies. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>bundleid</key> | ||
<string>com.sungmin.finance</string> | ||
<key>connections</key> | ||
<dict> | ||
<key>65701404-C17E-4B7F-A629-33B885553873</key> | ||
<array> | ||
<dict> | ||
<key>destinationuid</key> | ||
<string>C4387A70-9FCF-4162-B851-EA5534C50892</string> | ||
<key>modifiers</key> | ||
<integer>0</integer> | ||
<key>modifiersubtext</key> | ||
<string></string> | ||
</dict> | ||
</array> | ||
</dict> | ||
<key>createdby</key> | ||
<string>Sungmin Oh</string> | ||
<key>description</key> | ||
<string>check stock price</string> | ||
<key>disabled</key> | ||
<false/> | ||
<key>name</key> | ||
<string>네이버 금융</string> | ||
<key>objects</key> | ||
<array> | ||
<dict> | ||
<key>config</key> | ||
<dict> | ||
<key>plusspaces</key> | ||
<false/> | ||
<key>url</key> | ||
<string>http://{query}</string> | ||
<key>utf8</key> | ||
<true/> | ||
</dict> | ||
<key>type</key> | ||
<string>alfred.workflow.action.openurl</string> | ||
<key>uid</key> | ||
<string>C4387A70-9FCF-4162-B851-EA5534C50892</string> | ||
<key>version</key> | ||
<integer>0</integer> | ||
</dict> | ||
<dict> | ||
<key>config</key> | ||
<dict> | ||
<key>argumenttype</key> | ||
<integer>0</integer> | ||
<key>escaping</key> | ||
<integer>102</integer> | ||
<key>keyword</key> | ||
<string>s</string> | ||
<key>queuedelaycustom</key> | ||
<integer>3</integer> | ||
<key>queuedelayimmediatelyinitially</key> | ||
<true/> | ||
<key>queuedelaymode</key> | ||
<integer>0</integer> | ||
<key>queuemode</key> | ||
<integer>1</integer> | ||
<key>runningsubtext</key> | ||
<string>잠시만 기다려주세요.</string> | ||
<key>script</key> | ||
<string>python naver_stock.py {query}</string> | ||
<key>title</key> | ||
<string>네이버 금융</string> | ||
<key>type</key> | ||
<integer>0</integer> | ||
<key>withspace</key> | ||
<true/> | ||
</dict> | ||
<key>type</key> | ||
<string>alfred.workflow.input.scriptfilter</string> | ||
<key>uid</key> | ||
<string>65701404-C17E-4B7F-A629-33B885553873</string> | ||
<key>version</key> | ||
<integer>0</integer> | ||
</dict> | ||
</array> | ||
<key>readme</key> | ||
<string></string> | ||
<key>uidata</key> | ||
<dict> | ||
<key>65701404-C17E-4B7F-A629-33B885553873</key> | ||
<dict> | ||
<key>ypos</key> | ||
<real>150</real> | ||
</dict> | ||
<key>C4387A70-9FCF-4162-B851-EA5534C50892</key> | ||
<dict> | ||
<key>ypos</key> | ||
<real>150</real> | ||
</dict> | ||
</dict> | ||
<key>webaddress</key> | ||
<string></string> | ||
</dict> | ||
</plist> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
nv: 현재가 | ||
cd: 종목코드 | ||
eps: nv/eps PER(배) | ||
bps: nv/bps PBR(배) | ||
ov: 시가 | ||
sv: 전일? | ||
pcv: 전일 | ||
cv: 전일 대비 | ||
cr: 전일 대비 증감 % | ||
aq: 거래량 | ||
aa: 거래대금 | ||
ms: 장상태 ('OPEN', 'CLOSE') | ||
hv: 고가 | ||
lv: 저가 | ||
ul: 상한가 | ||
ll: 하한가 | ||
nm: 이름 | ||
keps: | ||
dv: | ||
cnsEps: | ||
nav: | ||
rf: | ||
mt: | ||
tyn: | ||
""" | ||
|
||
import urllib2 | ||
import json | ||
import unicodedata | ||
import sys | ||
reload(sys) | ||
sys.setdefaultencoding("utf-8") | ||
|
||
LIST_URL = u'http://ac.finance.naver.com:11002/ac?q=%s&q_enc=euc-kr&st=111&frm=stock&r_format=json&r_enc=euc-kr&r_unicode=0&t_koreng=1&r_lt=111' | ||
POLLING_URL = u'http://polling.finance.naver.com/api/realtime.nhn?query=SERVICE_ITEM:%s&q_enc=utf-8' | ||
|
||
def platten_nested_list(l): | ||
ret = [] | ||
for v in l: | ||
if isinstance(v, list): | ||
if len(v) == 1: | ||
if isinstance(v[0], list): | ||
ret.append(platten_nested_list(v[0])) | ||
else: | ||
ret.append(v[0]) | ||
elif len(v) > 1: | ||
ret.append(platten_nested_list(v)) | ||
else: | ||
ret.append(v) | ||
return ret | ||
|
||
|
||
def make_depth_two(l): | ||
ret = [] | ||
for v in l: | ||
if isinstance(v, list): | ||
if len(v) > 0: | ||
if not isinstance(v[0], list): | ||
ret.append(v) | ||
else: | ||
ret.extend(make_depth_two(v)) | ||
else: | ||
ret.append([v]) | ||
return ret | ||
|
||
|
||
def build_dic(l): | ||
ret = [] | ||
for v in l: | ||
url = 'finance.naver.com%s' % v[3] | ||
if v[3].startswith('/market') or v[3].startswith('/fund'): | ||
url = 'info.' + url | ||
dic = dict(label=v[0], | ||
name=v[1], | ||
market=v[2], | ||
url=url, | ||
code=v[4]) | ||
ret.append(dic) | ||
return ret | ||
|
||
|
||
def get_json(url): | ||
request = urllib2.Request(url) | ||
response = urllib2.urlopen(request) | ||
content = response.read() | ||
try: | ||
data = json.loads(content.decode('utf-8')) | ||
except: | ||
data = json.loads(content.decode('euc-kr')) | ||
return data | ||
|
||
|
||
def format_num(num, decimal=None): | ||
if not decimal: | ||
decimal = 0 | ||
return ('{:,.%sf}' % decimal).format(int(num)) | ||
|
||
|
||
def get_query(): | ||
try: | ||
query = u'%s' % ' '.join(sys.argv[1:]) | ||
query = unicodedata.normalize('NFC', query) | ||
return urllib2.quote(query.encode('utf-8')) | ||
except: | ||
return '' | ||
|
||
|
||
def print_alfred_format(items): | ||
print '<items>' | ||
for item in items: | ||
try: | ||
polling_url = POLLING_URL % item['code'] | ||
detail = get_json(polling_url)['result']['areas'][0]['datas'][0] | ||
# 종목 현재가 (+ 비율% 변동) | ||
title = u'{nm} {nv} ( {sign} {cr}%, {cv})'.format( | ||
nm=detail['nm'], | ||
nv='₩' + format_num(detail['nv']), | ||
sign='+' if detail['nv'] >= detail['pcv'] else '-', | ||
cr=format_num(detail['cr'], 2), | ||
cv=format_num(detail['cv'])) | ||
subtitle = u'거래량: {aq} 고가: {hv} 저가: {lv} PER: {eps} PBR: {bps}'.format( | ||
aq=format_num(detail['aq']), | ||
hv=format_num(detail['hv']), | ||
lv=format_num(detail['lv']), | ||
eps=format_num(int(detail['nv'])/int(detail['eps']), 2) + '배', | ||
bps=format_num(int(detail['nv'])/int(detail['bps']), 2) + '배') | ||
item_text = '<item arg="%s"><title>%s</title><subtitle>%s</subtitle></item>' % (item['url'], title, subtitle) | ||
except Exception as e: | ||
print e | ||
item_text = '<item arg="%s"><title>%s</title><subtitle>%s</subtitle></item>' % (item['url'], item['name'], item['market']) | ||
print item_text.encode('utf-8') | ||
print '</items>' | ||
|
||
|
||
def main(): | ||
query = get_query() | ||
url = LIST_URL % query | ||
data = get_json(url)[u'items'] | ||
items = build_dic(make_depth_two(platten_nested_list(data))) | ||
print_alfred_format(items) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |