Skip to content

Commit c950b6b

Browse files
hdk5python273
authored andcommitted
Add VkStreaming
1 parent 4d4eaa7 commit c950b6b

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
beautifulsoup4
22
requests
33
enum34
4+
websocket-client
45
six

vk_api/streaming.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
@author: python273, hdk5
4+
@contact: https://vk.com/python273
5+
@license Apache License, Version 2.0
6+
7+
Copyright (C) 2018
8+
"""
9+
10+
from .exceptions import VkApiError
11+
from enum import Enum
12+
import websocket
13+
import json
14+
15+
16+
URL_TEMPLATE = "{schema}://{server}/{method}?key={key}"
17+
18+
19+
class VkStreaming(object):
20+
21+
__slots__ = ('vk', 'url', 'key', 'server')
22+
23+
def __init__(self, vk):
24+
"""
25+
:param vk: объект VkApi
26+
"""
27+
self.vk = vk
28+
29+
self.url = None
30+
self.key = None
31+
self.server = None
32+
33+
self.update_streaming_server()
34+
35+
def update_streaming_server(self):
36+
response = self.vk.method('streaming.getServerUrl')
37+
38+
self.key = response['key']
39+
self.server = response['endpoint']
40+
41+
def get_rules(self):
42+
response = self.vk.http.get(URL_TEMPLATE.format(
43+
schema="https",
44+
server=self.server,
45+
method="rules",
46+
key=self.key)
47+
).json()
48+
49+
if response["code"] == 200:
50+
return response['rules'] or []
51+
elif response["code"] == 400:
52+
raise VkStreamingError(response['error'])
53+
54+
def add_rule(self, value, tag):
55+
response = self.vk.http.post(URL_TEMPLATE.format(
56+
schema="https",
57+
server=self.server,
58+
method="rules",
59+
key=self.key),
60+
json={"rule": {"value": value, "tag": tag}}
61+
).json()
62+
63+
if response["code"] == 200:
64+
return True
65+
elif response["code"] == 400:
66+
raise VkStreamingError(response['error'])
67+
68+
def delete_rule(self, tag):
69+
response = self.vk.http.delete(URL_TEMPLATE.format(
70+
schema="https",
71+
server=self.server,
72+
method="rules",
73+
key=self.key),
74+
json={"tag": tag}
75+
).json()
76+
77+
if response["code"] == 200:
78+
return True
79+
elif response["code"] == 400:
80+
raise VkStreamingError(response['error'])
81+
82+
def listen(self):
83+
ws = websocket.create_connection(URL_TEMPLATE.format(
84+
schema="wss",
85+
server=self.server,
86+
method="stream",
87+
key=self.key)
88+
)
89+
90+
while True:
91+
response = ws.recv()
92+
response = json.loads(response)
93+
if response["code"] == 100:
94+
yield response["event"]
95+
elif response["code"] == 300:
96+
raise VkStreamingServiceMessage(response['service_message'])
97+
98+
99+
class VkStreamingError(VkApiError):
100+
101+
def __init__(self, error):
102+
self.error_code = error['error_code']
103+
self.message = error['message']
104+
105+
def __str__(self):
106+
return '[{}] {}'.format(self.error_code,
107+
self.message)
108+
109+
110+
class VkStreamingServiceMessage(VkApiError):
111+
112+
def __init__(self, error):
113+
self.service_code = error['service_code']
114+
self.message = error['message']
115+
116+
def __str__(self):
117+
return '[{}] {}'.format(self.service_code,
118+
self.message)

0 commit comments

Comments
 (0)