From 7bc37ee857aa8942cf8a1efce7619958fa57e297 Mon Sep 17 00:00:00 2001 From: Vijai Kumar S Date: Thu, 14 Dec 2017 12:07:04 +0530 Subject: [PATCH] push all irc cloud related stuff --- Procfile | 2 + irccloud.py | 103 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 5 +++ runtime.txt | 1 + 4 files changed, 111 insertions(+) create mode 100644 Procfile create mode 100644 irccloud.py create mode 100644 requirements.txt create mode 100644 runtime.txt diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..9a75ab6 --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +worker: python irccloud.py + diff --git a/irccloud.py b/irccloud.py new file mode 100644 index 0000000..0c8ae6a --- /dev/null +++ b/irccloud.py @@ -0,0 +1,103 @@ +# A Simple script to keep an irc cloud client always alive +__author__ = "S Vijaikumar" +__email__ = "vijai@vijaikumar.in" +__copyright__ = "Copyright (C) 2017 S Vijai Kumar" +__license__ = "MIT" +__version__ = "1.0" + +import requests +import time +import sys +import traceback +from os import environ + +class irccloud: + """ + This is a very simple class that takes an user's irc user name + and password as input and keeps the connection alive to emulate + the hearbeat produced due to real browser activity + """ + AuthenticationToken = "" + SessionId = "" + KeepAliveToken = "" + + def __init__(self, email, password): + self.email = email + self.password = password + + def get_authentication_token(self): + url = "https://www.irccloud.com/chat/auth-formtoken" + r = requests.post(url) + response = r.json() + if response["success"]: + print "------> Successfully obtained authentication token." + irccloud.AuthenticationToken = response["token"] + else: + print "------> Failed to obtain an authentication token." + irccloud.AuthenticationToken = "AUTH_FAILURE" + + def get_session_id(self): + self.get_authentication_token() + if irccloud.AuthenticationToken == "AUTH_FAILURE": + return irccloud.AuthenticationToken + else: + login_url = "https://www.irccloud.com/chat/login" + headers = { + "content-type": "application/x-www-form-urlencoded", + "x-auth-formtoken":irccloud.AuthenticationToken + } + login_data = { + "email" : self.email, + "password" : self.password, + "token" : irccloud.AuthenticationToken + } + r = requests.post(login_url, data = login_data, headers = headers) + response = r.json() + if response["success"]: + print "------> Successfully obtained a session id." + irccloud.SessionId = response["session"] + else: + print "------> Failed to obtain a session id." + irccloud.SessionId = "SESSION_FAILURE" + + def keep_alive(self): + stream_url = "https://www.irccloud.com/chat/stream" + headers = {"Connection" : "keep-alive", + "Accept-Encoding" : "gzip,deflate,sdch", + "User-Agent" : "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.53 Safari/525.19", + "Cookie": "session={0}".format(irccloud.SessionId), + "Host":"www.irccloud.com" + } + r = requests.post(stream_url, headers = headers) + if r.status_code == 200: + irccloud.KeepAliveToken = "KA_ALIVE" + else: + irccloud.KeepAliveToken = "KA_DEAD" + + def runner(self): + self.get_session_id() + if irccloud.SessionId == "SESSION_FAILURE": + print "------> Couldn't get a session initialized. Quitting.. :)" + sys.exit(0) + else: + while True: + self.keep_alive() + if irccloud.KeepAliveToken == "KA_ALIVE": + print "------> IRC Cloud Session is Kept alive. Next check in 10 minutes." + else: + print "------> IRC Cloud Session could not be Kept alive. Next check in 10 minutes." + time.sleep(600) + + +if __name__ == "__main__": + try: + email = environ.get("IRCCLOUD_USERNAME") + password = environ.get("IRCCLOUD_PASSWORD") + irc = irccloud(email, password) + irc.runner() + except KeyboardInterrupt: + print "----> Shutdown requested. Exiting script. Thank you :)" + sys.exit(0) + except Exception: + traceback.print_exc(file=sys.stdout) + sys.exit(0) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2a7931e --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +certifi==2017.11.5 +chardet==3.0.4 +idna==2.6 +requests==2.18.4 +urllib3==1.22 \ No newline at end of file diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..5385682 --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-2.7.14