监控聚币网行情 并实时发送到微信
生活随笔
收集整理的這篇文章主要介紹了
监控聚币网行情 并实时发送到微信
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近由于好友推薦我入坑了國內的山寨幣,所以順便研究了下聚幣網的API。 不過網頁版的聚幣網和手機版的做的不好,而且因為是7x24 小時交易,自己沒有那么多的精力盯盤,所以寫了python代碼進行監控。
# -*-coding=utf-8-*- __author__ = 'Rocky' ''' http://30daydo.com Contact: weigesysu@qq.com ''' import random import hashlib import hmac,time import smtplib from email.mime.text import MIMEText from email import Utils import threading import requests,datetime,itchatfrom toolkit import Toolkitclass Jubi_web():def __init__(self, send=None):cfg = Toolkit.getUserData('data.cfg')self.public_key = cfg['public_key']self.private_key = cfg['private_key']self.send=sendfrom_mail = cfg['from_mail']password = cfg['password']to_mail = cfg['to_mail']smtp_server = 'smtp.qq.com'self.server = smtp_serverself.username = from_mail.split("@")[0]self.from_mail = from_mailself.password = passwordself.to_mail = to_mailself.coin_list=['IFC','DOGE','EAC','DNC','MET','ZET','SKT','YTC','PLC','LKC','JBC','MRYC','GOOC','QEC','PEB','XRP','NXT','WDC','MAX','ZCC','HLB','RSS','PGC','RIO','XAS','TFC','BLK','FZ','ANS','XPM','VTC','KTC','VRC','XSGS','LSK','PPC','ETC','GAME','LTC','ETH','BTC']# 初始化郵箱設置讀取需要股票信息# 這樣子只登陸一次if self.send == 'msn':try:self.smtp = smtplib.SMTP_SSL(port=465)self.smtp.connect(self.server)self.smtp.login(self.username, self.password)except smtplib.SMTPException, e:print ereturn 0if send=='wechat':self.w_name=u'xxxxx'itchat.auto_login(hotReload=True)account=itchat.get_friends(self.w_name)def send_wechat(self,name,content):w_content=name+' '+contentitchat.send(w_content,toUserName=self.toName)time.sleep(1)itchat.send(w_content,toUserName='filehelper')def send_text(self, name, content):subject = '%s' % nameself.msg = MIMEText(content, 'plain', 'utf-8')self.msg['to'] = self.to_mailself.msg['from'] = self.from_mailself.msg['Subject'] = subjectself.msg['Date'] = Utils.formatdate(localtime=1)try:self.smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())self.smtp.quit()print "sent"except smtplib.SMTPException, e:print ereturn 0def warming(self, coin, up_price, down_price):url = 'https://www.jubi.com/api/v1/ticker/'while 1:time.sleep(5)try:data = requests.post(url, data={'coin': coin}).json()except Exception,e:print eprint "time out. Retry"time.sleep(15)continuecurrent = float(data['last'])if current >= up_price:print "Up to ", up_priceprint "current price ",currentif self.send=='msn':self.send_text(coin,str(current))if self.send=='wechat':self.send_wechat(coin,str(current))time.sleep(1200)if current <= down_price:print "Down to ", down_priceprint "current price ",currentif self.send=='msn':self.send_text(coin,str(current))if self.send=='wechat':self.send_wechat(coin,str(current))time.sleep(1200)#上面的內容盡量不用修改。def getContent(self):url = 'https://www.jubi.com/api/v1/trade_list'params_data = {'key': 'x', 'signature': 'x'}s = requests.get(url=url, params=params_data)def getHash(self, s):m = hashlib.md5()m.update(s)return m.hexdigest()def sha_convert(self, s):return hashlib.sha256(self.getHash(s)).hexdigest()def get_nonce(self):lens = 12return ''.join([str(random.randint(0, 9)) for i in range(lens)])def get_signiture(self):url = 'xxxxxxxxx'coin = 'zet'nonce = self.get_nonce()# sha=self.sha_convert(private_key)md5 = self.getHash(self.private_key)message = 'nonce=' + nonce + '&' + 'key=' + self.public_key# print messagesignature = hmac.new(md5, message, digestmod=hashlib.sha256).digest()# print signature# req=requests.post(url,data={'signature':signature,'key':public_key,'nonce':nonce,'coin':'zet'})req = requests.post(url, data={'coin': coin})print req.status_codeprint req.textdef real_time_ticker(self, coin):url = 'xxxxxxxx'try:data = requests.post(url, data={'coin': coin}).json()#print dataexcept Exception ,e:print ereturn datadef real_time_depth(self, coin):url = 'xxxxxxxxx'data = requests.post(url, data={'coin': coin}).json()print datadata_bids = data['bids']data_asks = data['asks']print "bids"for i in data_bids:print i[0],print ' ',print i[1]print "asks"for j in data_asks:print j[0],print ' ',print j[1]def list_all_price(self):for i in self.coin_list:print i,print " price: ",p=self.real_time_ticker(i.lower())if p is not None:print p[u'last']def getOrder(self,coin):url='https://www.jubi.com/api/v1/orders/'try:req=requests.get(url,params={'coin':coin})except Exception,e:print edata=req.json()return data# recent 100 trade turn overdef turnover(self,coin):i=coin.lower()coins=Toolkit.getUserData('coins.csv')total=long(coins[i]) [i] [/i]p=self.getOrder(i)print pamount=0.00for j in p:t= j[u'amount']amount=float(t)+amount#current=float(self.real_time_ticker(i)[u'last'])turn_over=amount*1.00/total*100print turn_overdef multi_thread(self,coin_list,price_list):thread_num=len(coin_list)thread_list=for i in range(thread_num):t=threading.Thread(target=self.warming, args=(coin_list,price_list[0],price_list[1]),)thread_list.append(t)for j in thread_list:j.start()for k in thread_list:k.join()if __name__ == '__main__':obj = Jubi_web(send='wechat')coin_list=['zet','doge']price_list=[[0.2,0.13],[0.03,0.024]]obj.multi_thread(coin_list,price_list)[/i]程序運行后,使用掃一掃登錄。?
coin_list=['zet','doge'] price_list=[[0.2,0.13],[0.03,0.024]]
通過這個參數,設置你想要監控的幣種和目標價格。
同時程序支持發送給多個用戶。
總結
以上是生活随笔為你收集整理的监控聚币网行情 并实时发送到微信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 余额宝和理财通那个收益高
- 下一篇: 开咖啡厅注意事项 学会这些才算是刚刚入门