-
Notifications
You must be signed in to change notification settings - Fork 324
/
execute_functions.py
50 lines (35 loc) · 1.39 KB
/
execute_functions.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
# -*- coding: utf-8 -*-
""" Пример использования VkFunction - обертки над методом execute
Описание VKScript (языка execute): https://vk.com/dev/execute
"""
import vk_api
from vk_api.execute import VkFunction
vk_add = VkFunction(args=('x', 'y'), code='''
return %(x)s + %(y)s;
''')
vk_get_wall_post_ids = VkFunction(args=('values',), code='''
return API.wall.get(%(values)s)["items"]@.id;
''')
vk_get_filtered = VkFunction(
args=('method', 'values', 'key'),
clean_args=('method', 'key'), # аргументы будут сконвертированы через str
# остальные через json.dumps
code='''
return API.%(method)s(%(values)s)["items"]@.%(key)s;
''')
def main():
login, password = '[email protected]', 'mypassword'
vk_session = vk_api.VkApi(login, password)
try:
vk_session.auth(token_only=True)
except vk_api.AuthError as error_msg:
print(error_msg)
return
vk = vk_session.get_api()
print(vk_add(vk, 2, 3)) # 5
print(vk_add(vk, "hello ", "world!")) # hello world!
print(vk_get_wall_post_ids(vk, {'domain': 'python273', 'count': 100}))
print(vk_get_filtered(vk, 'wall.get', {'domain': 'durov'}, 'text'))
print(vk_get_filtered(vk, 'groups.get', {'extended': True}, 'name'))
if __name__ == '__main__':
main()