A module for monitoring message amount in VK

This commit is contained in:
w8u 2015-11-25 17:49:31 +03:00 committed by enkore
parent fd708e078e
commit 1680ef4f17

74
i3pystatus/vk.py Normal file
View File

@ -0,0 +1,74 @@
from i3pystatus import Status, IntervalModule
from i3pystatus.core.util import internet, require, user_open
import vk
class Vk(IntervalModule):
"""
Display amount of unread messages in VK social network.
Creating your own VK API app is highly recommended for your own privacy, though there is a default one provided.
Reference vk.com/dev for instructions on creating VK API app.
If access_token is not specified, the module will try to open a request page in browser.
You will need to manually copy obtained acess token to your config file.
Requires the PyPI package `vk`.
"""
API_LINK = "https://oauth.vk.com/authorize?client_id={id}&display=page&revoke=1&scope=messages,offline&response_type=token&v=5.40"
app_id = 5160484
access_token = None
session = None
token_error = "Vk: token error"
format = '{unread}/{total}'
interval = 1
color = "#ffffff"
color_unread = "#ffffff"
color_bad = "#ff0000"
settings = (
("app_id", "Id of your VK API app"),
("access_token", "Your access token. You must have `messages` and `offline` access permissions"),
("token_error", "Message to be shown if there's some problem with your token"),
("color",),
("color_bad",),
("color_unread",),
)
@require(internet)
def token_request(self, func):
user_open(self.API_LINK.format(id=self.app_id))
self.run = func
@require(internet)
def init(self):
if self.access_token:
self.session = vk.AuthSession(app_id=self.app_id, access_token=self.access_token)
self.api = vk.API(self.session, v='5.40', lang='en', timeout=10)
try:
permissions = int(self.api.account.getAppPermissions())
assert((permissions & 65536 == 65536) and (permissions & 4096 == 4096))
except:
self.token_request(self.error)
else:
self.token_request(lambda: None)
@require(internet)
def run(self):
total = self.api.messages.getDialogs()['count']
unread = self.api.messages.getDialogs(unread=1)['count']
if unread > 0:
color = self.color_unread
else:
color = self.color
self.output = {
"full_text": self.format.format(
total=total,
unread=unread
),
"color": color
}
def error(self):
self.output = {"full_text": self.token_error,
"color": self.color_bad}