-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathYMReader.py
More file actions
229 lines (168 loc) · 6.03 KB
/
YMReader.py
File metadata and controls
229 lines (168 loc) · 6.03 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class YMReader:
'''YMReader - is free redistributable library for work with Yandex Metrics API
Author: GrakovNe (Grakov Maksim)
Licence: GNU GPL v 2
Date: 23.07.13
USAGE:
To work with YMReader you need your ID and Token from Yandex OAuth
When you get ID and Token you need import library and create object of YMReader with your ID and Token as arguments
EXAMPLE:
from YMR import *
new = YMReader("ID","TOKEN")
Then you can work with your Metrics! As you can see it's simply!
How Library can:
Return the list of your counters at account ( GetCounters(() )
Return your login Data (ID and Token)
Return Summary Statistic by today or total statistic ( GetSummary(ID,["Today"|"Total"]) )
Return list of search phrases in which users come to your site ( GetSearchPhrases() )
Check YandexMetrics code on your site ( CheckCode(ID) )
Return load on your site today or total ( GetSiteLoad(ID,["Today"|"Total"]) )
Return list of links in which users come to your site ( GetLinkSites(ID) )
For examle, let give the number of visitors in http://grakovne.org:
from YMR import *
new = YMReader("12345678","123456ABCDEFGHIJKLMNOPRQSTUVWXYZ") # This is not correct data, of course
print new.GetSummary(new.GetCounters()[1]["ID"])
#That's all :)
CONTACTS:
ICQ: 5628310
E-MAIL: grakovne@yandex.ru
Twitter: http://twitter.com/grakovne
Web: http://grakovne.org
With best wishes from cold Russia
'''
def __init__(self,id,token):
self.ID = str(id)
self.Token = str(token)
self.LoginStr = "?id=%s&pretty=1&oauth_token=%s" % (id,token) # and making full string from previous dictonary
self.SummaryStr = "&pretty=1&oauth_token=%s" % (token)
self.URLPrefix = "http://api-metrika.yandex.ru/" # setting shared for all request prefix
def GetLoginData(self):
return self.ID, self.Token
def GetCounters(self): #Getting list of counters on your account
import urllib as net
try:
Source = net.urlopen(self.URLPrefix+"counters/"+self.LoginStr).read()
except:
return None
Result=[]
Sites=[]
while not Source.find('<counter>')==-1: # Splitting all XML response to lists "per site"
IDS = Source.find("<counter>")
IDE = Source.find("</counter>")
Sites.append(Source[IDS+10:IDE])
Source = Source[IDE+9:]
for site in Sites:
IDS = site.find('<site>')
IDE = site.find('</site>')
Host = site[IDS+6:IDE]
IDS = site.find('<id>')
IDE = site.find('</id>')
ID = site[IDS+4:IDE]
Result.append({"URL":Host,"ID":ID})
return Result
def GetSummary(self,ID,Segment="Today"): # Get Summary daily or total activity on site
import urllib as net
Result = {}
try:
Source = net.urlopen(self.URLPrefix+"stat/traffic/summary/?id="+ID+self.SummaryStr).read()
except:
return None
if Segment == "Today": # Different fragments for different segments
IDS = Source.find('<data count="7">')
IDE = Source.find("</row>")
if Segment == "Total":
IDS = Source.find("<totals>")
IDE = Source.find("</totals>")
Summary = Source[IDS:IDE]
IDS = Summary.find("<denial>")+8
IDE = Summary.find("</denial>")
Result["Denial"] = Summary[IDS:IDE]
IDS = Summary.find("<visits>")+8
IDE = Summary.find("</visits>")
Result["Visits"] = Summary[IDS:IDE]
IDS = Summary.find("<page_views>")+12
IDE = Summary.find("</page_views>")
Result["Views"] = Summary[IDS:IDE]
IDS = Summary.find("<depth>")+7
IDE = Summary.find("</depth>")
Result["Depth"] = Summary[IDS:IDE]
IDS = Summary.find("<visitors>")+10
IDE = Summary.find("</visitors>")
Result["Visitors"] = Summary[IDS:IDE]
return Result
def GetSearchPhrases(self,ID):
import urllib as net
List = []
Result = []
try:
Source = net.urlopen(self.URLPrefix+"stat/sources/phrases/"+self.LoginStr).read()
except:
return None
while not Source.find("<phrase>")==-1:
IDS = Source.find("<phrase>")
IDE = Source.find("</phrase>")
List.append(Source[IDS+8:IDE])
Source = Source[IDE+9:]
for each in List:
Result.append(each[9:-3])
return Result
def CheckCode(self,ID):
import urllib as net
try:
Source = net.urlopen(self.URLPrefix+"counter/"+ID+"/check/"+self.LoginStr).read()
except:
None
IDS = Source.find("<http_msg>")+10
IDE = Source.find("</http_msg>")
return Source[IDS:IDE]
def GetSiteLoad(self,ID,Segment="Today"):
import urllib as net
Result = {"Max-RPS-Date":"","Max-Users-Date":"","Max-RPS-Time":"","Max-Users-Time":"","Max-Users":"","Max-RPS":""}
try:
Source = net.urlopen(self.URLPrefix+"stat/traffic/load?id="+ID+self.SummaryStr).read()
except:
return None
if Segment == "Today":
IDS = Source.find('<data count="7">')
IDE = Source.find("</row>")
if Segment == "Total":
IDS = Source.find("<totals>")
IDE = Source.find("</totals>")
Load = Source[IDS:IDE]
IDS = Load.find("<max_rps_date>")+14
IDE = Load.find("</max_rps_date>")
Result["Max-RPS-Date"] = Load[IDS:IDE]
IDS = Load.find("<max_rps_time>")+14
IDE = Load.find("</max_rps_time>")
Result["Max-RPS-Time"] = Load[IDS:IDE]
IDS = Load.find("<max_users_date>")+16
IDE = Load.find("</max_users_date>")
Result["Max-Users-Date"] = Load[IDS:IDE]
IDS = Load.find("<max_users_time>")+16
IDE = Load.find("</max_users_time>")
Result["Max-Users-Time"] = Load[IDS:IDE]
IDS = Load.find("<max_rps>")+9
IDE = Load.find("</max_rps>")
Result["Max-RPS"] = Load[IDS:IDE]
IDS = Load.find("<max_users>")+11
IDE = Load.find("</max_users>")
Result["Max-Users"] = Load[IDS:IDE]
return Result
def GetLinkSites(self,ID):
Result = []
Links = []
import urllib as net
try:
Source = net.urlopen(self.URLPrefix+ "stat/sources/sites/?id="+ID+self.SummaryStr).read()
except:
return None
while not Source.find("<url>")==-1:
IDS = Source.find("<url>")+5
IDE = Source.find("</url>")
Links.append(Source[IDS:IDE])
Source = Source[IDE+5:]
for each in Links:
Result.append(each[9:-3])
return Result