def sendRoutineMail() : while True : time.sleep(1) [Year, Month, Day, Weekday, Time] = getCurrentTime() # everyday 00:00:00 if (Time == '00:00:00') : sendDirectMail()
def getHTMLhitsun(hosturl, posturl, subposturl, username, password) : try : # 1. set a cookie proceesor cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) # 2. get cookie from host URL h = urllib2.urlopen(hosturl) # 3. camouflage as the Mozilla browser headers = {'User-Agent':'Mozilla/5.0'} # 4. fill in form to be posted (page source, <input> label) postData = {'fr':'00', 'id_ip':username, 'pass':password, 'set':'进入'} # 5. encode postData postData = urllib.urlencode(postData) # 6. request post URL with post data, headers and cookie request = urllib2.Request(posturl, postData, headers) page = urllib2.urlopen(request) html = page.read() # 7. support Chinese html = unicode(html, 'gb2312').encode('utf-8') # enter sub-page (post) (normal : page = opener.open(URL) [Year, Month, Day, Weekday, Time] = getCurrentTime() subpostData = {'start_year':Year, 'start_month':Month, 'start_day':1, 'end_year':Year, 'end_month':Month, 'end_day':Day, 'ip':username, 'fr':'11', 'set':'查询'} subpostData = urllib.urlencode(subpostData) subrequest = urllib2.Request(subposturl, subpostData, headers) subpage = urllib2.urlopen(subrequest) subhtml = subpage.read() subhtml = unicode(subhtml, 'gb2312').encode('utf-8') return [html, subhtml] except Exception, e : print str(e)
def sendRoutineMail(): while True: time.sleep(1) [Year, Month, Day, Weekday, Time] = getCurrentTime() # everyday 00:00:00 if (Time == '00:00:00'): sendDirectMail()
def printDebug() : print "debug information\n--------------------" print "host URL \t: %s\npost URL \t: %s" %(logInfo.hostURL, logInfo.postURL) print "IP \t\t: %s\nPassword \t: %s" %(logInfo.username, logInfo.password) print "Mailsender \t: %s\nMailpasswd \t: %s" %(mailInfo.sender, mailInfo.password) print "Tolist \t\t: %s\nCclist \t\t: %s" %(mailInfo.tolist, mailInfo.cclist) print "Subject \t: %s\nAttachments \t: %s" %(mailInfo.subject, mailInfo.filespath) [Year, Month, Day, Weekday, Time] = getCurrentTime() print "Date \t\t: %d-%d-%d\nWeekday \t: %s\nTime \t\t: %s" %(Year, Month, Day, Weekday, Time) print "--------------------"
def getContent() : [balance, deposit, netflow] = getInfohitsun() [total, free, nofree, indoor] = getNetflow(netflow) [Year, Month, Day, Weekday, Time] = getCurrentTime() content = '''Hi, all,\n Until %d-%d-%d %s in this month, the total netflow has reached [%.2f GB]. The free, nofree and indoor netflow are [%.2f GB], [%.2f GB] and [%.2f GB] respectively. Our lab's net balance is [%.2f] and the deposit is [%.2f].\n --------------- This is sended by checkNetflow daemon automaticlally, please don't reply.\n Thanks, checkNetflow''' %(Year, Month, Day, Time, total, free, nofree, indoor, balance, deposit) return content
def getContent(): [balance, deposit, netflow] = getInfohitsun() [total, free, nofree, indoor] = getNetflow(netflow) [Year, Month, Day, Weekday, Time] = getCurrentTime() content = '''Hi, all,\n Until %d-%d-%d %s in this month, the total netflow has reached [%.2f GB]. The free, nofree and indoor netflow are [%.2f GB], [%.2f GB] and [%.2f GB] respectively. Our lab's net balance is [%.2f] and the deposit is [%.2f].\n --------------- This is sended by checkNetflow daemon automaticlally, please don't reply.\n Thanks, checkNetflow''' % (Year, Month, Day, Time, total, free, nofree, indoor, balance, deposit) return content
def printAccount() : [balance, deposit, netflow] = getInfohitsun() [total, free, nofree, indoor] = getNetflow(netflow) print "account infomation\n--------------------" print "balance \t\t: %.2f\ndeposit \t\t: %.2f\nnetflow (total)\t\t: %.4f GB" %(balance, deposit, total) print "netflow (free) \t\t: %.4f GB\nnetflow (nofree) \t: %.4f GB\nnetflow (indoor) \t: %.4f GB" %(free, nofree, indoor) print "--------------------" weekdayName = ['Mon', 'Tue', 'Wes', 'Thu', 'Fri', 'Sat', 'Sun'] [Year, Month, Day, Weekday, Time] = getCurrentTime() print " Date free nofree indoor" print "------------------------------------------------------------------" for day in range(Day) : weekday = weekdayName[calendar.weekday(Year, Month, day + 1)] if (netflow.has_key(day + 1)) : print "%04d-%02d-%02d (%s) \t %.4f GB\t %0.4f GB\t %0.4f GB" %(Year, Month, day + 1,weekday, netflow[day+1][0], netflow[day+1][1], netflow[day+1][2])
def main(): #data = createJson() # addStocks(data, 'tsla', '90', '100') # addStocks(data, 'spyerdo','100', '12312') # printJson() print("Welcome to Kent's Stock Portfolio Tracker.") if (getTime.isMarketOpen() == True): #TODO: Delete print("Looks like the market is open right now!") print(getTime.getCurrentTime()) else: print("The market is currently closed.") print(getTime.getCurrentTime()) #check to see if data file exists if (os.path.isfile("data.json") == False): initCreateJson() data = stockTracker.loadJson() while (1): update = Thread(target=stockTracker.updateStocks, args=(data, )) update.start() if update.isAlive(): menu(data)
def printDebug(): print "debug information\n--------------------" print "host URL \t: %s\npost URL \t: %s" % (logInfo.hostURL, logInfo.postURL) print "IP \t\t: %s\nPassword \t: %s" % (logInfo.username, logInfo.password) print "Mailsender \t: %s\nMailpasswd \t: %s" % (mailInfo.sender, mailInfo.password) print "Tolist \t\t: %s\nCclist \t\t: %s" % (mailInfo.tolist, mailInfo.cclist) print "Subject \t: %s\nAttachments \t: %s" % (mailInfo.subject, mailInfo.filespath) [Year, Month, Day, Weekday, Time] = getCurrentTime() print "Date \t\t: %d-%d-%d\nWeekday \t: %s\nTime \t\t: %s" % ( Year, Month, Day, Weekday, Time) print "--------------------"
def getHTMLhitsun(hosturl, posturl, subposturl, username, password): try: # 1. set a cookie proceesor cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) # 2. get cookie from host URL h = urllib2.urlopen(hosturl) # 3. camouflage as the Mozilla browser headers = {'User-Agent': 'Mozilla/5.0'} # 4. fill in form to be posted (page source, <input> label) postData = { 'fr': '00', 'id_ip': username, 'pass': password, 'set': '进入' } # 5. encode postData postData = urllib.urlencode(postData) # 6. request post URL with post data, headers and cookie request = urllib2.Request(posturl, postData, headers) page = urllib2.urlopen(request) html = page.read() # 7. support Chinese html = unicode(html, 'gb2312').encode('utf-8') # enter sub-page (post) (normal : page = opener.open(URL) [Year, Month, Day, Weekday, Time] = getCurrentTime() subpostData = { 'start_year': Year, 'start_month': Month, 'start_day': 1, 'end_year': Year, 'end_month': Month, 'end_day': Day, 'ip': username, 'fr': '11', 'set': '查询' } subpostData = urllib.urlencode(subpostData) subrequest = urllib2.Request(subposturl, subpostData, headers) subpage = urllib2.urlopen(subrequest) subhtml = subpage.read() subhtml = unicode(subhtml, 'gb2312').encode('utf-8') return [html, subhtml] except Exception, e: print str(e)
def printAccount(): [balance, deposit, netflow] = getInfohitsun() [total, free, nofree, indoor] = getNetflow(netflow) print "account infomation\n--------------------" print "balance \t\t: %.2f\ndeposit \t\t: %.2f\nnetflow (total)\t\t: %.4f GB" % ( balance, deposit, total) print "netflow (free) \t\t: %.4f GB\nnetflow (nofree) \t: %.4f GB\nnetflow (indoor) \t: %.4f GB" % ( free, nofree, indoor) print "--------------------" weekdayName = ['Mon', 'Tue', 'Wes', 'Thu', 'Fri', 'Sat', 'Sun'] [Year, Month, Day, Weekday, Time] = getCurrentTime() print " Date free nofree indoor" print "------------------------------------------------------------------" for day in range(Day): weekday = weekdayName[calendar.weekday(Year, Month, day + 1)] if (netflow.has_key(day + 1)): print "%04d-%02d-%02d (%s) \t %.4f GB\t %0.4f GB\t %0.4f GB" % ( Year, Month, day + 1, weekday, netflow[day + 1][0], netflow[day + 1][1], netflow[day + 1][2])
def updateStocks(data): time.sleep(30) currentPrice ="" percentChange = "" if getTime.isMarketOpen() == False: currentTime = getTime.getCurrentDate() + " 16:00:00" currentTime = "2018-07-03 13:00:00" #TODO REMOVE #print(closeTime) else: currentTime = getTime.getCurrentTime() for i in range(len(data["stocks"])): IntradayData = alpha.getIntraday(data["stocks"][i]["name"]) currentPrice = float(IntradayData["Time Series (1min)"][currentTime]["1. open"]) currentPrice = "%.2f" % currentPrice data["stocks"][i]["current price"] = str(currentPrice) #modify json data "current price" print("Done updating Stocks.") saveJson(data)
def printStockData(data): if getTime.isMarketOpen() == False: currentTime = getTime.getCurrentDate() + " 16:00:00" currentTime = "2018-07-03 13:00:00" #TODO REMOVE #print(closeTime) else: currentTime = getTime.getCurrentTime() print("Todays stock data. ") for i in range(len(data["stocks"])): #IntradayData = alpha.getIntraday(data["stocks"][i]["name"]) #TODO make this automatic totalAmount = float(data["stocks"][i]["current price"]) * float(data["stocks"][i]["quantity"]) print("---------------------------") print("Stock name: "+ data["stocks"][i]["name"]) print("Current Price: "+ data["stocks"][i]["current price"]) print("Bought Price: " + data["stocks"][i]["bought price"]) print("quantity: " + data["stocks"][i]["quantity"]) print("total amount: " + "%.2f" % totalAmount) print("---------------------------")
def getInfohitsun() : [htmlCtx, subhtmlCtx] = getHTMLhitsun(hostURL, postURL, subpostURL, username, password) r_balance = re.compile(r">所剩余额:.*?(\d{1,3}\.\d{2}).*?</td>", re.S) balance = re.findall(r_balance, htmlCtx) balance = string.atof(balance[0]) r_deposit = re.compile(r">包月存钱罐余额\(元\):.*?(\d{1,3}\.\d{2}).*?</td>", re.S) deposit = re.findall(r_deposit, htmlCtx) deposit = string.atof(deposit[0]) # get netflow dict ( format, day:(free, nofree, indoor) ) netflow = {} [Year, Month, Day, Weekday, Time] = getCurrentTime() for day in range(Day) : r_string = r'%04d-%02d-%02d 00:00:00</td>.*?>(\d.*?)</td>.*?>(\d.*?)</td>.*?>(\d.*?)</td>' %(Year, Month, day + 1) r_netflow = re.compile(r_string, re.S) _netflow = re.findall(r_netflow, subhtmlCtx) if (_netflow != []) : free = string.atof(_netflow[0][0].replace(',','')) / 1000 / 1000 nofree = string.atof(_netflow[0][1].replace(',','')) / 1000 / 1000 indoor = string.atof(_netflow[0][2].replace(',','')) / 1000 /1000 netflow[day + 1] = (free, nofree, indoor) return [balance, deposit, netflow]
def updateOneStock(data,name): count = -1 for i in range(len(data["stocks"])): if name == data["stocks"][i]["name"]: count = i if count == -1: return else: currentPrice ="" percentChange ="" if getTime.isMarketOpen() == False: currentTime = getTime.getCurrentDate() + " 16:00:00" currentTime = "2018-07-03 13:00:00" #TODO REMOVE #print(closeTime) else: currentTime = getTime.getCurrentTime() IntradayData = alpha.getIntraday(data["stocks"][count]["name"]) currentPrice = float(IntradayData["Time Series (1min)"][currentTime]["1. open"]) data["stocks"][count]["current price"] = str(currentPrice)
def getInfohitsun(): [htmlCtx, subhtmlCtx] = getHTMLhitsun(hostURL, postURL, subpostURL, username, password) r_balance = re.compile(r">所剩余额:.*?(\d{1,3}\.\d{2}).*?</td>", re.S) balance = re.findall(r_balance, htmlCtx) balance = string.atof(balance[0]) r_deposit = re.compile(r">包月存钱罐余额\(元\):.*?(\d{1,3}\.\d{2}).*?</td>", re.S) deposit = re.findall(r_deposit, htmlCtx) deposit = string.atof(deposit[0]) # get netflow dict ( format, day:(free, nofree, indoor) ) netflow = {} [Year, Month, Day, Weekday, Time] = getCurrentTime() for day in range(Day): r_string = r'%04d-%02d-%02d 00:00:00</td>.*?>(\d.*?)</td>.*?>(\d.*?)</td>.*?>(\d.*?)</td>' % ( Year, Month, day + 1) r_netflow = re.compile(r_string, re.S) _netflow = re.findall(r_netflow, subhtmlCtx) if (_netflow != []): free = string.atof(_netflow[0][0].replace(',', '')) / 1000 / 1000 nofree = string.atof(_netflow[0][1].replace(',', '')) / 1000 / 1000 indoor = string.atof(_netflow[0][2].replace(',', '')) / 1000 / 1000 netflow[day + 1] = (free, nofree, indoor) return [balance, deposit, netflow]