Skip to content

Commit

Permalink
push all irc cloud related stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaigridpro committed Dec 14, 2017
1 parent b6e7c0b commit 7bc37ee
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
worker: python irccloud.py

103 changes: 103 additions & 0 deletions irccloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# A Simple script to keep an irc cloud client always alive
__author__ = "S Vijaikumar"
__email__ = "[email protected]"
__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)
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
certifi==2017.11.5
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-2.7.14

0 comments on commit 7bc37ee

Please sign in to comment.