-
-
Save spolakh/2459c12f3919010b781e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests as req | |
from datetime import timedelta, date | |
from time import sleep | |
id_map = {'Yandex': 1839501, 'Google': 1839502, 'Mail': 1839503} | |
def normalize_name(name): | |
if name.lower().startswith('google chrome'): | |
return 'Google Chrome' | |
return name | |
def add_stats(stats, browser_name, values): | |
browser_name = normalize_name(browser_name) | |
if browser_name in stats: | |
assert(len(stats[browser_name]) == len(values)) | |
for i in range(len(values)): | |
stats[browser_name][i] += values[i] | |
else: | |
stats[browser_name] = values | |
def parse_data(raw_json): | |
stats = {} | |
for section in raw_json['series']: | |
values = [] | |
if section['name'] != "": | |
assert len(section['data']) == 30, "len == %d, name == %s" % (len(section['data']), len(section['name'])) | |
for value in section['data']: | |
if isinstance(value, int): | |
values.append(value) | |
else: | |
values.append(int(value['y'])) | |
add_stats(stats, section['name'], values) | |
#stats[section['name']] = values | |
return stats | |
def append_zeros(stat, n_zeros): | |
for key in stat.keys(): | |
for i in range(n_zeros): | |
stat[key].append(0) | |
return stat | |
def concatenate_stats(stats1, stats2): | |
if stats1 is None: | |
return stats2 | |
if stats2 is None: | |
return stats1 | |
if ' ' in stats1: | |
return append_zeros(stats2, len(stats1[' '])) | |
if ' ' in stats2: | |
return append_zeros(stats1, len(stats2[' '])) | |
for key, value in stats2.items(): | |
stats1[key].extend(value) | |
return stats1 | |
def load_se(se_name, end_date, num_days): | |
se_id = id_map[se_name] | |
params = {'id': se_id, | |
'period': 0, | |
'gender': 0, | |
'agegroup': 0, | |
'aggregation': 'sum', | |
'sids': "chrome/chrome-23,chrome/chrome-50,chrome/chrome-18,chrome/chrome-21,chrome/chrome-15,chrome/chrome-35,chrome/chrome-17,chrome/chrome-31,chrome/chrome-40,chrome/chrome-29,chrome/chrome-38,chrome/chrome-11,chrome/chrome-27,chrome/chrome-39,chrome/chrome-26,chrome/chrome-22,chrome/chrome-25,chrome/chrome-44,chrome/chrome-46,chrome/chrome-45,chrome/chrome-51,chrome/chrome-41,chrome/chrome-47,chrome/chrome-43,chrome/chrome-48,chrome/chrome-49,chrome/chrome-ya,mob,opera,firefox,msie", | |
'ytype': 'value', | |
'gtype': 'line', | |
'legend': 1} | |
full_data = None | |
for cur_date in (end_date - timedelta(n * 30) | |
for n in reversed(range((num_days + 1) // 30))): | |
params['date'] = cur_date.strftime("%Y-%m-%d") | |
resp = req.get("https://top.mail.ru/json/browsersdynamics.hc", params=params) | |
data = resp.json() | |
full_data = concatenate_stats(full_data, parse_data(data)) | |
sleep(1) | |
return full_data | |
def loader(end_date, num_days): | |
search_engine_data = {} | |
for se_name in id_map.keys(): | |
search_engine_data[se_name] = load_se(se_name, end_date, num_days) | |
return search_engine_data | |
if __name__ == "__main__": | |
res = loader(date(2016, 3, 22), 730) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment