__author__ = 'WYL'
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import urllib
import urllib.request
import re
import os
import time
import threading
import sys
orig_url = 'https://www.coincola.com/buy/ETH'
feixiaohao_url = 'https://www.feixiaohao.com/currencies/ethereum/'
def getcoincola_data(__url):
#print(__url)
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
req = urllib.request.Request(url=__url, headers=headers)
response = urllib.request.urlopen(req)
"""
res_text = urllib.request.urlopen(req)
f = open('response.txt','wb')
f.write(res_text.read())
f.close()
"""
#print(response.read().decode('UTF-8'))
#<a class="td-username " href="/user/1000081673">limei0215<br><span class="is-realname">Verified</span></a>
data = response.read().decode('UTF-8')
pattern = re.compile(r'<td class="td-price">\d\d\d\d CNY</td>',re.S)
items = re.findall(pattern,data)
contents = []
for item in items:
#print(item)
item_price = re.findall(r'\d\d\d\d',item)
#print(item_price[0])
contents.append(item_price[0])
#print(len(contents))
#for i in contents:
# print(i)
#old=sys.stdout
log_txt = open('20180319.txt','a')
sys.stdout = log_txt
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
print('coincola buy:',float(contents[0]),' ',float(contents[1]),' ',float(contents[2]),' ', \
'percent %.3f %%' % (float(abs(float(contents[1]) - float(contents[0])))/float(contents[0])) \
)
#============================================================================================================
fxh_req = urllib.request.Request(url=feixiaohao_url, headers=headers)
fxh_response = urllib.request.urlopen(fxh_req)
fxh_data = fxh_response.read().decode('UTF-8')
#print(fxh_data)
fxh_pattern = re.compile(r'<div class="coinprice">¥\d,\d\d\d<span class="',re.S)
fxh_items = re.findall(fxh_pattern,fxh_data)
#print(fxh_items)
for fxh_item in fxh_items:
#print(fxh_item)
fxh_item_price = re.findall(r'\d,\d\d\d',fxh_item)
#print(fxh_item_price)
fxh_price = float(fxh_item_price[0].replace(',',''))
print('feixiaohao eth: ',fxh_price,' 溢价:','percent %.3f %%'%(float(contents[1])/fxh_price))
#============================================================================================================
global timer
timer = threading.Timer(120, getcoincola_data, [orig_url])
timer.start()
if __name__ == '__main__':
#geturllist(orig_url)
timer = threading.Timer(1, getcoincola_data, [orig_url])
timer.start()