Skip to content

Commit 9ee2bd4

Browse files
committed
4.6.4 Fast VkTools.get_all_items
1 parent 2c582cc commit 9ee2bd4

6 files changed

Lines changed: 76 additions & 5 deletions

File tree

jconfig/jconfig.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import json
1313

1414

15-
class config(object):
15+
class Config(object):
1616
def __init__(self, section, filename='config'):
1717
self.section = section # Секция настроек
1818
self.filename = filename # Файл с настройками

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from distutils.core import setup
1313
setup(name='vk_api',
14-
version='4.6.3',
14+
version='4.6.4',
1515
description='Module to use API vk.com',
1616
author='Kirill Python',
1717
author_email='mikeking568@gmail.com',

vk_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'''
1010

1111
__author__ = "Kirill Python"
12-
__version__ = "4.6.3"
12+
__version__ = "4.6.4"
1313
__email__ = "mikeking568@gmail.com"
1414
__contact__ = "https://vk.com/python273"
1515

vk_api/vk_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(self,
4848
self.app_id = app_id
4949
self.scope = scope
5050

51-
self.settings = jconfig.config(login)
51+
self.settings = jconfig.Config(login)
5252

5353
self.http = requests.Session()
5454
self.http.proxies = proxies # Ставим прокси

vk_api/vk_tools.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# encoding: utf-8
2+
import json
23

34

45
class VkTools(object):
@@ -8,10 +9,47 @@ def __init__(self, vk):
89
def get_all_items(self, method, values=None, max_count=200):
910
''' Получить все элементы
1011
Работает в методах, где в ответе есть count и items
12+
За один запрос получает max_count * 25 элементов
1113
1214
:param method: метод
1315
:param values: параметры
14-
:param max_count: максимальное количество,
16+
:param max_count: максимальное количество элементов,
17+
которое можно получить за один раз
18+
'''
19+
20+
if values:
21+
values = values.copy()
22+
else:
23+
values = {}
24+
25+
items = []
26+
offset = 0
27+
28+
while True:
29+
run_code = code_get_all_items % (max_count, offset, json.dumps(values), method, method)
30+
31+
try:
32+
response = self.vk.method('execute', {'code': run_code})
33+
except Exception as e:
34+
print run_code
35+
print e
36+
37+
items += response['items']
38+
39+
if response['end']:
40+
break
41+
42+
offset = response['offset']
43+
44+
return {'count': len(items), 'items': items}
45+
46+
def get_all_items_slow(self, method, values=None, max_count=200):
47+
''' Получить все элементы
48+
Работает в методах, где в ответе есть count и items
49+
50+
:param method: метод
51+
:param values: параметры
52+
:param max_count: максимальное количество элементов,
1553
которое можно получить за один раз
1654
'''
1755

@@ -35,3 +73,6 @@ def get_all_items(self, method, values=None, max_count=200):
3573
items += response['items']
3674

3775
return {'count': len(items), 'items': items}
76+
77+
# Полный код в файле vk_procedures
78+
code_get_all_items = 'var z=%s,x=%s,y=%s,p={"count":z}+y,r=API.%s(p),c=r["count"],j=r["items"],o=z,i=1;while(i<25&&o+z<c){o=i*z+x;p={"count":z,"offset":o}+y;r=API.%s(p);j=j+r["items"];i=i+1;};return{"count":c,"items":j,"offset":o,"end":o+z>=c};'

vk_procedures

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
==========
2+
CODE: get items x25
3+
INPUT: max_count, start_offset, params:in_JSON, method, method
4+
----------
5+
var max_count = %s;
6+
var start_offset = %s;
7+
var my_params = %s;
8+
9+
var params = {"count": max_count} + my_params;
10+
11+
var res = API.%s(params);
12+
var count = res["count"];
13+
var items = res["items"];
14+
15+
var offset = max_count;
16+
17+
var i = 1;
18+
while(i < 25 && offset + max_count < count) {
19+
offset = i * max_count + start_offset;
20+
params = {"count": max_count, "offset": offset};
21+
params = params + my_params;
22+
23+
res = API.%s(params);
24+
items = items + res["items"];
25+
26+
i = i + 1;
27+
}
28+
29+
return {"count": count, "items": items, "offset": offset, "end": offset + max_count >= count};
30+
==========

0 commit comments

Comments
 (0)