Merge branch 'w8u-w8u-vk'

This commit is contained in:
enkore 2016-02-02 16:42:29 +01:00
commit 1bdeaa5f6d
3 changed files with 77 additions and 1 deletions

View File

@ -19,6 +19,7 @@ master branch
- :py:mod:`.sge`: Sun Grid Engine (SGE) monitor - :py:mod:`.sge`: Sun Grid Engine (SGE) monitor
- :py:mod:`.timer`: Timer - :py:mod:`.timer`: Timer
- :py:mod:`.syncthing`: Syncthing monitor and control - :py:mod:`.syncthing`: Syncthing monitor and control
- :py:mpd:`.vk`: Displays number of messages in VKontakte
* Applications started from click events don't block other click events now * Applications started from click events don't block other click events now
* Fixed crash with desktop notifications when python-gobject is installed, but no notification daemon is running * Fixed crash with desktop notifications when python-gobject is installed, but no notification daemon is running
* Log file name is now an option (``logfile`` of :py:class:`.Status`) * Log file name is now an option (``logfile`` of :py:class:`.Status`)

View File

@ -33,7 +33,8 @@ MOCK_MODULES = [
"dota2py", "dota2py",
"novaclient.v2", "novaclient.v2",
"speedtest_cli", "speedtest_cli",
"pyzabbix" "pyzabbix",
"vk",
] ]
for mod_name in MOCK_MODULES: for mod_name in MOCK_MODULES:

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", "General color of the output"),
("color_bad", "Color of the output in case of access token error"),
("color_unread", "Color of the output if there are unread messages"),
)
@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}