This repository was archived by the owner on Jul 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.py
More file actions
131 lines (98 loc) · 3.66 KB
/
api.py
File metadata and controls
131 lines (98 loc) · 3.66 KB
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
127
128
129
130
131
import poster
import urllib2
import cookielib
import re
from socketIO_client import SocketIO
import json
import thread
import config
cookies = cookielib.LWPCookieJar()
handlers = [
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.HTTPCookieProcessor(cookies)
]
opener = poster.streaminghttp.register_openers()
for handler in handlers:
opener.add_handler(handler)
curr_chat_room = None
socketIO = None
mesg_queue = []
def fetch(uri):
req = urllib2.Request(uri)
return opener.open(req)
def post(uri, params):
datagen, headers = poster.encode.multipart_encode(params)
req = urllib2.Request(uri, datagen, headers)
return opener.open(req)
def get_password():
for cookie in cookies:
if cookie.name == "password_livechan":
cookie.value = config.nolimitCookie
return cookie.value
return ""
def post_chat(body, chat, name=config.name, convo="General", trip="", file="" ):
post_params = []
name = name if trip == "" else name+"#"+trip
post_params.append(poster.encode.MultipartParam("name", name))
post_params.append(poster.encode.MultipartParam("trip", trip))
post_params.append(poster.encode.MultipartParam("body", body))
post_params.append(poster.encode.MultipartParam("convo", convo))
post_params.append(poster.encode.MultipartParam("chat", chat))
if not file == "":
post_params.append(poster.encode.MultipartParam.from_file("image", file))
datagen, headers = poster.encode.multipart_encode(post_params)
uri = 'https://kotchan.org/chat/'+chat
req = urllib2.Request(uri, datagen, headers)
return opener.open(req)
def join_chat(chat_room):
global curr_chat_room
global socketIO
if (curr_chat_room != None):
socketIO.emit('unsubscribe', curr_chat_room)
socketIO.emit('subscribe', chat_room)
def display_chat(chat_obj):
print(chat_obj["name"])
print(chat_obj["body"])
print
def get_data(chat):
data_response = fetch('https://kotchan.org/data/'+chat)
json_data = json.loads(data_response.read())
for i in json_data[::-1]:
display_chat(i)
def main_chat(chat_room):
chat_body = raw_input("> ")
if (chat_body == "/quit"):
return True # break
mainresp = post_chat(chat_body, chat_room)
return False
def on_chat(*args):
if args[0]["name"] == "IRCBot":
return
msg = args[0]["name"]+"~ "
if "image" in args[0]:
filename = "https://kotchan.org/tmp/uploads/"+re.compile('[\w\-\.]*$').search(args[0]["image"]).group(0)
msg += "file: "+filename+" "
msg += (" ".join(args[0]["body"].splitlines()))
livechanBot.sendMsg(channel, msg)
def on_user_count(*args):
print(args[0], "users online")
def on_request_location(*args):
global chat_room
socketIO.emit('subscribe', curr_chat_room);
def login(callback=on_chat):
cookie = cookielib.Cookie(version=0, name='password_livechan', value=config.nolimitCookie, port=None, port_specified=False, domain='kotchan.org',
domain_specified=False, domain_initial_dot=False,
path='/', path_specified=True, secure=False, expires=None,
discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
cookies.set_cookie(cookie)
livechan_pass = get_password()
if livechan_pass == "":
print("wrong password")
login()
global socketIO
socketIO = SocketIO('https://kotchan.org',
cookies={'password_livechan': livechan_pass})
socketIO.on('chat', callback)
socketIO.on('request_location', on_request_location)
thread.start_new_thread(socketIO.wait, ())