最近在准备学习实时流处理的开源框架,用flink加kafka加flume去实现,然后缺点数据,作为wow玩家,索性就直接去请求了时光徽章的API去作为原数据,直接上代码
import datetime
import requests
from requests.auth import HTTPBasicAuth
def get_access_token(region):
client_secret = "client_secret" #从战网官网申请的client secret 和client id放进来
client_id = "client_id"
path = '/oauth/token'
data = {'grant_type': 'client_credentials'}
auth = HTTPBasicAuth(client_id, client_secret)
url = 'https://{0}.battle.net{1}'.format(region, path)
if region == 'cn':
url = 'https://www.battlenet.com.cn{0}'.format(path)
req = requests.Session().post(url, data=data, auth=auth)
access_token = req.json()['access_token']
return access_token
if __name__ == '__main__':
url2 = "https://gateway.battlenet.com.cn/data/wow/token/index?namespace=dynamic-classic-cn&locale=zh_CN&access_token="
res = requests.Session().get(url2 + get_access_token("cn"))
now = datetime.datetime.fromtimestamp(res.json()['last_updated_timestamp']/1000).strftime("%Y-%m-%d %H:%M:%S")
gold_price = res.json()['price']/10000
#print(res.json())
with open("/home/hadoop/py_sc/gold.txt","a") as f:
f.write('%s %d\n'%(now,gold_price))
print(now, gold_price)
执行结果如图:
目前的金价是1931 真的贵呀