This repository has been archived by the owner on May 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathirccloud.py
126 lines (115 loc) · 4.37 KB
/
irccloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# A Simple script to keep an irc cloud client always alive
__author__ = "S Vijaikumar"
__email__ = "[email protected]"
__copyright__ = "Copyright (C) 2021 S Vijai Kumar"
__license__ = "UNLICENSE"
__version__ = "1.0"
import requests
import sys
import traceback
import logging
import json
import time
from timeloop import Timeloop
from datetime import timedelta
import random
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, debug_mode=False):
self.email = email
self.password = password
self.debugging = debug_mode
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO)
self.log = logging.getLogger(__name__)
def get_authentication_token(self):
url = "https://www.irccloud.com/chat/auth-formtoken"
r = requests.post(url)
response = r.json()
if self.debugging:
self.log.debug(response)
if response["success"]:
self.log.info("Successfully obtained authentication token.")
irccloud.AuthenticationToken = response["token"]
else:
self.log.error("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 self.debugging:
self.log.debug(response)
if response["success"]:
self.log.info("Successfully obtained a session id.")
irccloud.SessionId = response["session"]
else:
self.log.critical("Failed to obtain a session id.")
irccloud.SessionId = "SESSION_FAILURE"
def keep_alive(self):
ua_file = open("user_agents.json", "r+")
user_agents = json.load(ua_file)
ua_file.close()
stream_url = "https://www.irccloud.com/chat/stream"
headers = {"Connection" : "keep-alive",
"Accept-Encoding" : "gzip,deflate,sdch",
"User-Agent" : random.choice(user_agents),
"Cookie": "session={0}".format(irccloud.SessionId),
"Host":"www.irccloud.com"
}
r = requests.post(stream_url, headers = headers)
if self.debugging:
self.log.debug(r.json())
if r.status_code == 200:
irccloud.KeepAliveToken = "KA_ALIVE"
else:
irccloud.KeepAliveToken = "KA_DEAD"
def runner(self):
self.get_session_id()
self.log.debug(" IRC Cloud Keep alive Func ")
if irccloud.SessionId == "SESSION_FAILURE":
self.log.critical("Couldn't get a session initialized. Quitting... :(")
sys.exit(0)
else:
self.keep_alive()
if irccloud.KeepAliveToken == "KA_ALIVE":
self.log.info("IRC Cloud Session is Kept alive.")
else:
self.log.error("IRC Cloud Session could not be Kept alive.")
tl = Timeloop()
@tl.job(interval=timedelta(seconds=3600))
def timed_executor():
try:
email = environ.get("IRCCLOUD_USERNAME")
password = environ.get("IRCCLOUD_PASSWORD")
debug_mode = False
irc = irccloud(email, password, debug_mode)
irc.runner()
except KeyboardInterrupt:
self.log.debug("Shutdown requested. Exiting script. Thank you :)")
sys.exit(0)
except Exception:
traceback.print_exc(file=sys.stdout)
sys.exit(0)
if __name__ == "__main__":
tl.start(block=True)