commit 15efeb086a777975f574a04fe7c4fa333bba8dca Author: Alexander Popov Date: Sat May 4 02:09:46 2019 +0300 1.0 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d344ba6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.json diff --git a/.preview.png b/.preview.png new file mode 100644 index 0000000..58c8285 Binary files /dev/null and b/.preview.png differ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf1ab25 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/README.md b/README.md new file mode 100644 index 0000000..5221117 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# vk.com set online status service +Набор инструментов для поддержания твоего статуса в социальной сети Vk.com __"Онлайн"__ +![preview](https://raw.githubusercontent.com/iiiypuk/vk0nline/master/.preview.png) +Набор включает в себя скрипт на `Python` и юнит `systemd`. + +## Установка: ++ Скачать и распаковать набор инструметов, например в `/home/user_name/.vk0nline` ++ Создать директорию для юнитов systemd: `mkdir -p ~/.config/systemd/user` +и перейти в неё: `cd ~/.config/systemd/user` ++ Сделать символические ссылки на сервис и таймер: +``` +ln -s /home/user_name/.vk0nline/systemd/user/vk0nline.service . +ln -s /home/user_name/.vk0nline/systemd/user/vk0nline.timer . +``` ++ Изменить настройки скрипта и сделать его исполняемым: +``` +cd ~/.vk0nline/ +mv config.json.example config.json +chmod 755 ./vk0nline.py +edit config.json +``` ++ Прописать полный путь к файлу конфигурации в скрипте: +```python +# EDIT IT +with open('./config.json', 'r', encoding='utf-8') as f: +``` ++ Прописать полный путь к файлу скрипта в сервисе systemd: +`edit ~/.config/systemd/user/vk0nline.service` +`ExecStart=/full/path/to/vk0nline.py` ++ Активируем и запускаем скрипт: +``` +systemctl start vk0nline.service --user +systemctl enable vk0nline.timer --user +systemctl start vk0nline.timer --user +``` diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..2e544cd --- /dev/null +++ b/config.json.example @@ -0,0 +1,7 @@ +{ + "clientId": 1, + "secureKey": "", + "serviceKey": "", + "accessToken": "", + "userIds": "" +} diff --git a/systemd/user/vk0nline.service b/systemd/user/vk0nline.service new file mode 100644 index 0000000..ad09490 --- /dev/null +++ b/systemd/user/vk0nline.service @@ -0,0 +1,6 @@ +[Unit] +Description=vk.com set online status service + +[Service] +Type=simple +ExecStart=/full/path/to/vk0nline.py diff --git a/systemd/user/vk0nline.timer b/systemd/user/vk0nline.timer new file mode 100644 index 0000000..99628c5 --- /dev/null +++ b/systemd/user/vk0nline.timer @@ -0,0 +1,11 @@ +[Unit] +Description=vk.com set online status timer + +[Timer] +OnBootSec=1min +OnUnitActiveSec=4min +Persistent=true +Unit=vk0nline.service + +[Install] +WantedBy=multi-user.target diff --git a/vk0nline.py b/vk0nline.py new file mode 100644 index 0000000..e8ce062 --- /dev/null +++ b/vk0nline.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import json +import requests +from datetime import datetime + +__author__ = 'Alexander Popov' +__copyright__ = '2019 by iiiypuk' +__credits__ = ['Alexander Popov'] +__license__ = 'Unlicense' +__version__ = '1.0.0' +__maintainer__ = 'Alexander Popov' +__email__ = 'iiiypuk@fastmail.fm' +__status__ = 'Production' + +# EDIT IT +with open('./config.json', 'r', encoding='utf-8') as f: + _C = json.load(f) + +def getStatus(): + response = requests.get('https://api.vk.com/method/users.get?' + \ + 'fields=online,last_seen&v=5.95' + \ + '&access_token={key}&user_ids={user}'.format( + key=_C['serviceKey'], user=_C['userIds'])) + + userLoginTime = json.loads(response.content.decode('utf-8'))['response'][0]['last_seen']['time'] + userOnline = json.loads(response.content.decode('utf-8'))['response'][0]['online'] + + return([userOnline, userLoginTime]) + +def setOnline(): + response = requests.get('https://api.vk.com/method/account.setOnline' + \ + '?voip=0&v=5.95&access_token={key}'.format(key=_C['accessToken']) + \ + '&user_ids={user}'.format(user=_C['userIds'])) + content = json.loads(response.content.decode('utf-8')) + + if 'error' in content: + if 5 == content['error']['error_code']: # User authorization failed + print('User authorization failed.\nOpen this link in browser, and copy access token.') + print('https://oauth.vk.com/authorize?client_id={appid}'.format(appid=_C['clientId']) + \ + '&redirect_uri=vk.com&display=mobile&response_type=' + \ + 'token&v=5.95&revoke=1&state=01010&scope=offline') + else: + print('Error: {}.'.format(content['error']['error_code'])) + elif 'response' in content: + if 1 == content['response']: + print('Ok!') + onlineStatus, lastSeen = getStatus() + print(onlineStatus, datetime.fromtimestamp(lastSeen).strftime('%Y-%m-%d %H:%M:%S')) + +if __name__ == "__main__": setOnline()