diff --git a/docs/changelog.rst b/docs/changelog.rst index 607d04f..53bb2f9 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -19,6 +19,7 @@ master branch - :py:mod:`.sge`: Sun Grid Engine (SGE) monitor - :py:mod:`.timer`: Timer - :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 * 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`) diff --git a/docs/conf.py b/docs/conf.py index 3eebd68..7da9e13 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -33,7 +33,8 @@ MOCK_MODULES = [ "dota2py", "novaclient.v2", "speedtest_cli", - "pyzabbix" + "pyzabbix", + "vk", ] for mod_name in MOCK_MODULES: diff --git a/i3pystatus/vk.py b/i3pystatus/vk.py new file mode 100644 index 0000000..ba24fcf --- /dev/null +++ b/i3pystatus/vk.py @@ -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}