def getLatestMarketHistoryAndCreateSentences(manager:MongoDBManager, marketHistory:str, companyList:dict) -> str: '''create sentences to be inserted in an email''' portfolio = getPriceAtPurchase(manager, companyList) history = manager.getCollection(marketHistory) today = datetime.datetime.today() weekday = today.weekday() # print(weekday) #debug if marketHistory == 'japanStockHistory' and weekday == 0: queryDate = today - datetime.timedelta(days=3) #get info of Friday when querying on Monday elif marketHistory == 'usStockHistory' and weekday == 0: queryDate = today - datetime.timedelta(days=2) #get info of Saturday when querying on Monday else: queryDate = today - datetime.timedelta(days=1) stocks = manager.getSpecificDocs({'date':{'$gte':queryDate}}) sentences = '' for each in stocks: # print(each.get('symbol')) #debug symbol = each.get('symbol') purchsaeInfo = portfolio.get(symbol) # print(purchsaeInfo) #debug if purchsaeInfo is not None: s = '{:>14}:{:>8},{:>16}\n{:>14}\n'.format(companyList.get(symbol),each.get('price') , each.get('comparison'),purchsaeInfo[0] + '/' + purchsaeInfo[1]) sentences += s # print(sentences) return sentences
def getCompanyList(manager:MongoDBManager, market:str) -> dict: '''get {'symbol':'companyName'} dict of the given market.''' market = manager.getCollection(market) symbols = manager.getAllDocs() companyList = {} for t in symbols: companyList[t.get('symbol')] = t.get('company') # print(companyList) #debug return companyList
def getPriceAtPurchase(manager:MongoDBManager, companyList:dict) -> dict: '''get order record from db and calculate stock price when purchasing''' '''companyList as dict is like {'symbol':'companyName'}.Returns {'symbol':[price,units] as list} as dict''' record = manager.getCollection('orderRecord') dictToReturn = {} for key in companyList.keys(): companyOrderRecord = manager.getSpecificDocs({'symbol':key}) for order in companyOrderRecord: if order.get('sold') == 'false': purchasePrices = [order.get('price'), order.get('units')] dictToReturn[key] = purchasePrices # print(dictToReturn) #debug return dictToReturn
def updatePurchaseRecord(): # query parameter取得 market = request.args.get('market') #'us' or 'japan' symbol = request.args.get('symbol') buyPrice = request.args.get('price') exchange = request.args.get('exchange') # '1' if japan units = request.args.get('units') date = request.args.get('date') # 'yyyyMMdd' purchaseDate = datetime.datetime(int(date[0:4]), int(date[4:6]), int(date[6:])) doc = { 'market': market, 'symbol': symbol, 'price': buyPrice, 'exchange': exchange, 'units': units, 'sold': "false", 'date': purchaseDate } manager = MongoDBManager.MongoDBManager() stockDB = manager.getDB('stock') record = manager.getCollection('orderRecord') id = manager.insertOneDoc(doc) print(str(id)) return (str(id))
def addCompanyList(): market = request.args.get('market') #'us' or 'japan' company = request.args.get('company') symbol = request.args.get('symbol') manager = MongoDBManager.MongoDBManager() stockDB = manager.getDB('stock') companyList = manager.getCollection(market) doc = {'company': company, 'symbol': symbol} id = manager.insertOneDoc(doc) print(id) return (id)
def generateText() -> str: '''get stock price info from usStockHistory and japanStockHistory. And create a text of an email''' manager = MongoDBManager.MongoDBManager() stockDB = manager.getDB('stock') #create US stock info usCompanyList = getCompanyList(manager, 'us') usSentences = getLatestMarketHistoryAndCreateSentences(manager, 'usStockHistory', usCompanyList) #create Japan stock info japanCompanyList = getCompanyList(manager, 'japan') japanSentences = getLatestMarketHistoryAndCreateSentences(manager, 'japanStockHistory', japanCompanyList) text = 'US market\n' + usSentences + '---------------\nJapan market\n' + japanSentences # print(text) #debug return text
def getFromNikkeiScraping(JapaneseStocks: dict) -> str: '''JapaneseStocks is like {'symbol':'companyName', 'symbol':'company'}''' http = urllib3.PoolManager() text = 'JPN stocks\n' manager = MongoDBManager.MongoDBManager() stockDB = manager.getDB("stock") japan = manager.getCollection('japanStockHistory') date = datetime.datetime.today() for key in JapaneseStocks.keys(): code = key name = JapaneseStocks[key] url = 'https://www.nikkei.com/nkd/company/?scode=' + code response = http.request('GET', url) html = response.data.decode('utf-8') soup = BeautifulSoup(html, 'html.parser') comparedToYesterday = '' presentPrice = soup.find(class_='m-stockPriceElm_value now').text presentPrice = presentPrice.replace(' 円', '') PlusComparedToYesterday = soup.find( class_='m-stockPriceElm_value comparison plus') MinusComparedToYesterday = soup.find( class_='m-stockPriceElm_value comparison minus') if MinusComparedToYesterday: comparedToYesterday = MinusComparedToYesterday.text if PlusComparedToYesterday: comparedToYesterday = PlusComparedToYesterday.text # TODO: 日本語で出力すると2bit使うのでバラバラになる s = '{:>12}:{:>10},{:>16}\n'.format(name, presentPrice, comparedToYesterday) # print(s) #debug text += s post = { 'symbol': code, 'price': presentPrice, 'comparison': comparedToYesterday, 'date': date } inserted_id = manager.insertOneDoc(post) print(inserted_id) #debug return text
def getFromYFUSScraping(USStocks: dict) -> str: '''USStocks is like {'symbol':'company', 'symbol':'company'}''' http = urllib3.PoolManager() text = 'US stocks\n' manager = MongoDBManager.MongoDBManager() stockDB = manager.getDB("stock") us = manager.getCollection('usStockHistory') date = datetime.datetime.today() for key in USStocks.keys(): ticker = key name = USStocks[key] url = 'https://finance.yahoo.com/quote/' + ticker + '?p=T&.tsrc=fin-srch' response = http.request('GET', url) html = response.data.decode('utf-8') soup = BeautifulSoup(html, 'html.parser') comparedToYesterday = '' presentPrice = soup.find( class_='Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)').text comparison = soup.find( class_='Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($dataGreen)') if comparison is None: comparison = soup.find( class_='Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($dataRed)') comparisonText = comparison.text s = '{:>14}:{:>8},{:>16}\n'.format(name, presentPrice, comparisonText) print(s) #debug text += s post = { 'symbol': ticker, 'price': presentPrice, 'comparison': comparisonText, 'date': date } inserted_id = manager.insertOneDoc(post) # print(inserted_id) #debug return text
import MongoDBManager from getInfo import getFromNikkeiScraping, getFromYFUSScraping from mainProcess import * manager = MongoDBManager.MongoDBManager() stockDB = manager.getDB("stock") usCompanyList = getCompanyList(manager, 'us') result = getLatestMarketHistoryAndCreateSentences(manager, 'usStockHistory', usCompanyList) print(result)