Skip to content

Commit

Permalink
Add VkStreaming
Browse files Browse the repository at this point in the history
  • Loading branch information
hdk5 authored and python273 committed Jul 16, 2018
1 parent 4d4eaa7 commit c950b6b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
beautifulsoup4
requests
enum34
websocket-client
six
118 changes: 118 additions & 0 deletions vk_api/streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# -*- coding: utf-8 -*-
"""
@author: python273, hdk5
@contact: https://vk.com/python273
@license Apache License, Version 2.0
Copyright (C) 2018
"""

from .exceptions import VkApiError
from enum import Enum
import websocket
import json


URL_TEMPLATE = "{schema}://{server}/{method}?key={key}"


class VkStreaming(object):

__slots__ = ('vk', 'url', 'key', 'server')

def __init__(self, vk):
"""
:param vk: объект VkApi
"""
self.vk = vk

self.url = None
self.key = None
self.server = None

self.update_streaming_server()

def update_streaming_server(self):
response = self.vk.method('streaming.getServerUrl')

self.key = response['key']
self.server = response['endpoint']

def get_rules(self):
response = self.vk.http.get(URL_TEMPLATE.format(
schema="https",
server=self.server,
method="rules",
key=self.key)
).json()

if response["code"] == 200:
return response['rules'] or []
elif response["code"] == 400:
raise VkStreamingError(response['error'])

def add_rule(self, value, tag):
response = self.vk.http.post(URL_TEMPLATE.format(
schema="https",
server=self.server,
method="rules",
key=self.key),
json={"rule": {"value": value, "tag": tag}}
).json()

if response["code"] == 200:
return True
elif response["code"] == 400:
raise VkStreamingError(response['error'])

def delete_rule(self, tag):
response = self.vk.http.delete(URL_TEMPLATE.format(
schema="https",
server=self.server,
method="rules",
key=self.key),
json={"tag": tag}
).json()

if response["code"] == 200:
return True
elif response["code"] == 400:
raise VkStreamingError(response['error'])

def listen(self):
ws = websocket.create_connection(URL_TEMPLATE.format(
schema="wss",
server=self.server,
method="stream",
key=self.key)
)

while True:
response = ws.recv()
response = json.loads(response)
if response["code"] == 100:
yield response["event"]
elif response["code"] == 300:
raise VkStreamingServiceMessage(response['service_message'])


class VkStreamingError(VkApiError):

def __init__(self, error):
self.error_code = error['error_code']
self.message = error['message']

def __str__(self):
return '[{}] {}'.format(self.error_code,
self.message)


class VkStreamingServiceMessage(VkApiError):

def __init__(self, error):
self.service_code = error['service_code']
self.message = error['message']

def __str__(self):
return '[{}] {}'.format(self.service_code,
self.message)

0 comments on commit c950b6b

Please sign in to comment.