Skip to content

Instantly share code, notes, and snippets.

/lnotify.py Secret

Created November 23, 2014 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/7f652c4d69efab5bff2a to your computer and use it in GitHub Desktop.
Save anonymous/7f652c4d69efab5bff2a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import argparse
import requests
import logging
import json
import time
import os
import sys
def LorURL(path=""):
return "https://www.linux.org.ru%s" % (path,)
def ReadConfig(path="~/.lnotify"):
path = os.path.expanduser(path)
with open(path) as f:
return json.load(f)
session = requests.Session()
front_page = session.get(LorURL())
csrf_token = front_page.cookies["CSRF_TOKEN"][1:-1]
config = ReadConfig()
login_data = {
"nick":config["username"],
"passwd":config["password"],
"csrf":csrf_token
}
headers={
"Accept":"application/json"
}
response_text = session.post(LorURL("/ajax_login_process"), data=login_data, headers=headers).text
response = json.loads(response_text)
logged_in = bool(response["loggedIn"])
format_str = config.get("format","LOR: %s")
nomsg_format_str = config.get("nomsg_format", "LOR: no messages")
timeout = int(config.get("timeout_sec", 60))
if logged_in:
while True:
notifications_response = session.get(LorURL("/notifications-count"))
assert notifications_response.status_code==200
notifications_count = int(notifications_response.text)
if notifications_count != 0:
print(format_str % notifications_count)
else:
print(nomsg_format_str)
sys.stdout.flush()
time.sleep(timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment