Skip to content

Commit

Permalink
Add plugin moex financeyahoo stock sipnet
Browse files Browse the repository at this point in the history
  • Loading branch information
artyl committed Jun 7, 2020
1 parent 3408a6a commit efc2538
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
_TKINTER_install.zip
_HLAM/*
plugin/personalsetting.py
mbplugin.zip
dllplugin
httpserver
Expand Down
4 changes: 2 additions & 2 deletions plugin/eur.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def get_balance(login, password, storename=None):
result = {}
session = requests.Session()
url = time.strftime("http://cbrates.rbc.ru/tsv/978/%Y/%m/%d.tsv")
response2 = session.get(url)
result['Balance'] = response2.text.split()[-1]
response = session.get(url)
result['Balance'] = response.text.split()[-1]
result['userName'] = 'Курс евро от РБК'
return result

Expand Down
22 changes: 22 additions & 0 deletions plugin/financeyahoo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf8 -*-
''' Получение котировок с finance.yahoo.com в логин код ценной бумаги
https://finance.yahoo.com/quote/AAPL
https://query1.finance.yahoo.com/v8/finance/chart/AAPL '''
import sys;sys.dont_write_bytecode = True
import os, sys, re, time, logging
import requests
import store, settings, personalsetting

def get_balance(login, password, storename=None):
result = {}
session = requests.Session()
url = time.strftime(f'https://query1.finance.yahoo.com/v8/finance/chart/{login}')
response = session.get(url)
meta = response.json()['chart']['result'][0]['meta']
meta['regularMarketPrice']
result['Balance'] = meta['regularMarketPrice']
return result


if __name__ == '__main__':
print('This is module finance.yahoo.com')
24 changes: 24 additions & 0 deletions plugin/moex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# -*- coding: utf8 -*-
''' Получение котировок с moex.com в логин код ценной бумаги
https://iss.moex.com/iss/engines/stock/markets/shares/securities/TATNP
https://iss.moex.com/iss/securities/TATNP.xml'''
import sys;sys.dont_write_bytecode = True
import os, sys, re, time, logging
import requests
import xml.etree.ElementTree as etree
import store, settings, personalsetting


def get_balance(login, password, storename=None):
result = {}
session = requests.Session()
url = time.strftime(f'https://iss.moex.com/iss/engines/stock/markets/shares/securities/{login}')
response = session.get(url)
root=etree.fromstring(response.text)
rows = root.findall('*[@id="marketdata"]/rows')[0]
result['Balance'] = [c.get('LAST') for c in rows.getchildren() if c.get('LAST')!=''][0]
return result


if __name__ == '__main__':
print('This is module moex')
48 changes: 48 additions & 0 deletions plugin/sipnet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf8 -*-
import sys;sys.dont_write_bytecode = True
import os, sys, re, logging
import requests
import store

# Строка для поиска баланса на странице
re_balance = r'(?usi)Баланс.*?>.*?>.*?>(.*?) '
# Строка для поиска тарифа
re_tariff = r'(?usi)status-work.*?>.*?>.*?>(.*?)<'
re_sipid = '(?usi)SIP ID.*?>.*?>(.*?)<' # SIP ID (лицевой счет)


def get_balance(login, password, storename=None):
logging.info(f'start get_balance {login}')
result = {}
url = 'https://www.sipnet.ru/cabinet/index'
session = store.load_session(storename)
if session is None:
logging.info(f'No saved session {login}')
session = requests.Session()
response1 = session.get(url)
if re.search(re_balance, response1.text):
logging.info(f'Already logoned {login}')
else:
# Логинимся
logging.info(f'relogon {login}')
data = {'CabinetAction': 'login','view': 'ru','Name': login,'Password':password,}
response1 = session.post(url, data=data)
if response1.status_code != 200:
raise RuntimeError(f'POST Login page {url} error: status_code {response2.status_code}')

result['Balance'] = re.search(re_balance, response1.text).group(1).replace(',', '.').strip()
try:
result['TarifPlan'] = re.search(re_tariff, response1.text).group(1).replace('&nbsp;', '').strip()
except Exception:
logging.info(f'Not found {TarifPlan}')
try:
result['licSchet'] = re.search(re_sipid, response1.text).group(1).replace('&nbsp;', '').strip()
except Exception:
logging.info(f'Not found {licSchet}')

store.save_session(storename, session)
return result


if __name__ == '__main__':
print('This is module sipnet')
88 changes: 88 additions & 0 deletions plugin/stock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf8 -*-
''' Сумма по всем стокам
Стоки прописаны в personalsetting.py
словарь stock {имя набора':{'stocks': список_стоков, 'remain': остатки_на_счетах} }
имя набора берется из login
пример
stocks = {'BROKER_RU': {'STOCKS':(('AAPL',1,'Y'),('TATNP',16,'M'),('FXIT',1,'M')), 'REMAIN': {'USD':5, 'RUB':536}, 'CURRENC': 'USD'}}
Каждый сток - код стока, количество акций, источник данных (Y yahoo, M moex)
REMAIN - остатки на долларовом и рублевом счетах
CURRENC - в какой валюте считать итог
Проверить что код стока корректный можно подставив в ссылку:
https://finance.yahoo.com/quote/AAPL
https://query1.finance.yahoo.com/v8/finance/chart/AAPL
https://iss.moex.com/iss/engines/stock/markets/shares/securities/TATNP
https://iss.moex.com/iss/securities/TATNP.xml
'''
import sys;sys.dont_write_bytecode = True
import os, sys, re, time, logging, threading, queue
import xml.etree.ElementTree as etree
import requests
import store, settings, personalsetting

def get_usd_moex():
session = requests.Session()
response = session.get('https://iss.moex.com/iss/statistics/engines/futures/markets/indicativerates/securities')
res = re.findall(r'secid="USD/RUB" rate="(.*?)"',response.text)[0]
return float(res)

def get_yahoo(security, cnt, qu=None):
session = requests.Session()
url = time.strftime(f'https://query1.finance.yahoo.com/v8/finance/chart/{security}')
response = session.get(url)
meta = response.json()['chart']['result'][0]['meta']
meta['regularMarketPrice']
res = meta['regularMarketPrice']*cnt, security, 'USD'
if qu:
qu.put(res)
return res

def get_moex(security, cnt, qu=None):
session = requests.Session()
url = time.strftime(f'https://iss.moex.com/iss/engines/stock/markets/shares/securities/{security}')
response = session.get(url)
root=etree.fromstring(response.text)
rows = root.findall('*[@id="marketdata"]/rows')[0]
res = float([c.get('LAST') for c in list(rows) if c.get('LAST')!=''][0])*cnt, security, 'RUB'
if qu:
qu.put(res)
return res

def count_all_scocks_multithread(stocks, remain, currenc):
usd = get_usd_moex()
k = {'USD': 1, 'RUB': 1} # Коэффициенты для приведения к одной валюте
if currenc == 'USD':
k['RUB'] = 1/usd
else:
k['USD'] = usd
qu = queue.Queue() # Очередь для данных из thread [val, sec_code, currency]
# Каждое получение данных в отдельном трэде
for sec,cnt,market in stocks:
target = get_yahoo if market=='Y' else get_moex
threading.Thread(target=target, name='stock', args=(sec,cnt,qu)).start()
# Ждем завершения всех трэдов с получением данных
while [1 for i in threading.enumerate() if i.name=='stock']:
time.sleep(0.01)
data = []
# Забираем данные от трэдов
while qu.qsize()>0:
data.append(qu.get_nowait())
data.sort(key=lambda i:(i[2],i[1])) # Сортируем сначала по валюте. затем по коду бумаги
res_full = '\n'.join([f'{sec:5} : {round(val*k[curr],2):7.2f} {curr}' for val,sec,curr in data])+'\n'
res_balance = round(sum([val*k[curr] for val,sec,curr in data]) + remain['USD']*k['USD'] + remain['RUB']*k['RUB'],2)
return res_balance, res_full

def get_balance(login, password, storename=None):
result = {}
stocks = personalsetting.stocks[login]['STOCKS']
remain = personalsetting.stocks[login]['REMAIN']
currenc = personalsetting.stocks[login]['CURRENC']
res_balance, res_full = count_all_scocks_multithread(stocks, remain, currenc)
result['Stock'] = res_full # Полная информация по стокам
result['Balance'] = res_balance # Сумма в заданной валюте
result['Currenc'] = currenc # Валюта
return result


if __name__ == '__main__':
print('This is module finance.yahoo.com')
4 changes: 2 additions & 2 deletions plugin/usd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def get_balance(login, password, storename=None):
result = {}
session = requests.Session()
url = time.strftime("http://cbrates.rbc.ru/tsv/840/%Y/%m/%d.tsv")
response2 = session.get(url)
result['Balance'] = response2.text.split()[-1]
response = session.get(url)
result['Balance'] = response.text.split()[-1]
result['userName'] = 'Курс доллара от РБК'
return result

Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ sodexo - Получение баланса карты Sodexo (подарочны
strelka - Баланс карты стрелка
tele2 - ТЕЛЕ2
zadarma - Zadarma.com (IP телефония)
usd - Курс доллара с RBC
eur - Курс евро с RBC
financeyahoo - Курс ценных бумаг с finance.yahoo.com
moex - Курс ценных бумаг с moex.com
stock - Рассчет цены портфеля ценных бумаг
sipnet - Sipnet (IP телефония)

## Как проверить вручную
запустите из C:\mbplugin\plugin:
Expand Down

0 comments on commit efc2538

Please sign in to comment.