-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdemo.py
153 lines (142 loc) · 6.14 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import json
import time
import logging
import datetime
import redis
import urllib.parse
import urllib.request
# 日期开始
today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
fifteenago = today - datetime.timedelta(days=11)
print(str(yesterday).replace("-", ""), str(fifteenago).replace("-", ""))
end, start = str(yesterday).replace("-", ""), str(fifteenago).replace("-", "")
# 日期结束
base_url = "https://api.baidu.com/json/tongji/v1/ReportService/getData"
pool = redis.ConnectionPool(host='47.95.10.139', port=6379, password='root') # TODO redis地址
r = redis.Redis(connection_pool=pool)
# logging
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='myapp.log',
filemode='w')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
class Baidu(object):
def __init__(self, siteId, username, password, token):
self.siteId = siteId
self.username = username
self.password = password
self.token = token
def getresult(self, start_date, end_date, method, metrics, **kw):
base_url = "https://api.baidu.com/json/tongji/v1/ReportService/getData"
body = {"header": {"account_type": 1, "password": self.password, "token": self.token,
"username": self.username},
"body": {"siteId": self.siteId, "method": method, "start_date": start_date,
"end_date": end_date, "metrics": metrics}}
for key in kw:
body['body'][key] = kw[key]
data = bytes(json.dumps(body), 'utf8')
req = urllib.request.Request(base_url, data)
response = urllib.request.urlopen(req)
the_page = response.read()
logging.info("从百度返回结果")
return the_page.decode("utf-8")
def getPvUvAvgTime(self): # 获取PV、UV、AvgTime
result = self.getresult(start, end, "overview/getTimeTrendRpt",
"pv_count,visitor_count,ip_count,bounce_ratio,avg_visit_time")
result = json.loads(result)["body"]["data"][0]["result"]["items"]
data = result[0]
daterange = [str(x[0]).replace("2017/", "") for x in data]
pv_count = [x[0] if x[0] != '--' else 0 for x in result[1]]
visitor_count = [x[1] if x[1] != '--' else 0 for x in result[1]]
ip_count = [x[2] if x[2] != '--' else 0 for x in result[1]]
bounce_ratio = [x[3] if x[3] != '--' else 0 for x in result[1]]
avg_visit_time = [round(x[4] / 60, 2) if x[4] != '--' else 0 for x in result[1]]
r.set("daterange", str(daterange))
r.set("pv_count", str(pv_count))
r.set("visitor_count", str(visitor_count))
r.set("ip_count", str(ip_count))
r.set("bounce_ratio", str(bounce_ratio))
r.set("avg_visit_time", str(avg_visit_time))
logging.info("PV,UV,AvgTime")
def getRukouYeMian(self): # 前十入口页面
result = self.getresult(start, end, "source/all/a",
"pv_count,visitor_count,avg_visit_time", viewType='visitor')
data = json.loads(result)["body"]["data"][0]["result"]["items"]
name = [item[0]['name'] for item in data[0]]
count = 0
tojson = []
for item in data[1]:
temp = {}
temp["name"] = name[count]
temp["pv_count"] = item[0]
temp["visitor_count"] = item[1]
temp["average_stay_time"] = item[2]
tojson.append(temp)
count = count + 1
r.set("rukouyemian", json.dumps(tojson[:5]))
logging.info("前十入口页面")
def getAllSource(self): # 获取所有来源
result = self.getresult(start, end, "source/all/a",
"pv_count,pv_ratio,visitor_count")
base = json.loads(result)["body"]["data"][0]["result"]["items"]
source = [item[0]['name'] for item in base[0]]
count = 0
detail = []
for item in base[1]:
tojson = {}
tojson['name'] = source[count]
tojson['pv_count'] = item[0]
tojson['pv_ratio'] = item[1]
tojson['visitor_count'] = item[2]
count = count + 1
detail.append(tojson)
r.set("source", json.dumps(detail))
logging.info("所有来源")
def getDiYu(self): # 地域
result = self.getresult(start, end, "visit/district/a",
"pv_count,visitor_count,avg_visit_time")
base = json.loads(result)["body"]["data"][0]["result"]["items"]
source = [item[0]['name'] for item in base[0]]
count = 0
detail = []
for item in base[1]:
tojson = {}
tojson['name'] = source[count]
tojson['pv_count'] = item[0]
tojson['pv_ratio'] = item[1]
tojson['visitor_count'] = item[2]
count = count + 1
detail.append(tojson)
r.set("diyu", json.dumps(detail))
logging.info("地域")
def getTopTen(self): # 前十
result = self.getresult(start, end, "visit/toppage/a",
"pv_count,visitor_count,average_stay_time")
base = json.loads(result)["body"]["data"][0]["result"]["items"]
name = [item[0]['name'] for item in base[0]]
count = 0
tojson = []
for item in base[1]:
temp = {}
temp["name"] = name[count]
temp["pv_count"] = item[0]
temp["visitor_count"] = item[1]
temp["average_stay_time"] = item[2]
tojson.append(temp)
count = count + 1
r.set("top_ten", json.dumps(tojson[:5]))
logging.info("前十访问页面")
if __name__ == '__main__':
bd = Baidu(yoursiteid, "your usernmae", "your password", "token")
bd.getPvUvAvgTime()
bd.getRukouYeMian()
bd.getAllSource()
bd.getDiYu()
bd.getTopTen()
print("finish")