📜 ⬆️ ⬇️

Working with the Yandex.Metrica API in Python

To all fans of Python and Yandex. Metrics of a good day!

Some people know that for some time now I have become a Web developer, however, this is loudly said. My site with interesting puzzles, by the way, if anyone is interested, you can get there through this link , slowly but surely, roll to the top of search engines, since there are not so many alternatives. I find out about the attendance on the site from the reports of the Yandex.metrics I already love. Basically, there is most of what I need there. But every time to go to from the site in order to see the number of visitors is somehow not a fountain. And here I again, just in time, remembered that in general I am a programmer and I am learning a python. “Okay,” I thought, “I will practice with the network at the same time.” No sooner said than done. Through the oAuth protocol, we register the application and get a debugging token for it (it lives indefinitely and does not need to ask for it each time. So, for our needs, this is the most!). For all interested, under the cut quote the library code, which can be tied to any GUI - interface:

YMReader.py
')
And now, briefly, that she still knows how.


Liba itself, of course, does nothing by default. In order to use it, you need to import it into your project:

import YMReader

Then pass it the login data using the SetLoginData method in the parameters specifying your ID and your token. How do you get the token - your business, but I decided to use a static debug (there must be at least something through the ass)
For example, like this:

SetLoginData("21535707","49985f415a9a46aab45b70304a557cab") # , - 403 401

Well, now, you can use all the functions. Type print dir (YMReader) to see what they are called. As a rule, functions return data to me either as dictionaries (for example, GetSummary) or as a string (CheckCode). Therefore, to take advantage of the results, no parsing is required. Let's take, for example, the number of visitors for the site ru-brains.org:

import YMReader

YMReader.SetLoginData("21535707","49985f415a9a46aab45b70304a557cab")

print YMReader.GetSummary((YMReader.GetCounters()[1]["URL"]))["Visitors"]

As you can see, everything is simple!
Liba is distributed under the GPL license - so use as much as you want!
Good luck to everyone and buggy code!

Source: https://habr.com/ru/post/187384/


All Articles