/
livechanapi.py
123 lines (102 loc) · 3.6 KB
/
livechanapi.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
import urllib
import urllib2
import cookielib
import re
from socketIO_client import SocketIO
import json
import thread
cookies = cookielib.LWPCookieJar()
handlers = [
urllib2.HTTPHandler(),
urllib2.HTTPSHandler(),
urllib2.HTTPCookieProcessor(cookies)
]
opener = urllib2.build_opener(*handlers)
curr_chat_room = None
socketIO = None
mesg_queue = []
def fetch(uri):
req = urllib2.Request(uri)
return opener.open(req)
def post(uri, params):
data = urllib.urlencode(params)
req = urllib2.Request(uri, data)
return opener.open(req)
def get_password():
for cookie in cookies:
if cookie.name == "password_livechan":
print cookie
cookie.value = "somepassword" #compatible with the hacky implementation in lib/utils/check-ip-validity.js
return cookie.value
return ""
def post_chat(body, chat, name="Anonymous", convo="General", trip=""):
post_params = {}
post_params["name"] = name if trip == "" else name+"##"+trip
post_params["trip"] = trip
post_params["body"] = body
post_params["convo"] = convo
post_params["chat"] = chat
return post('https://livechan.net/chat/'+chat, post_params)
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://livechan.net/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
#print mainresp.read()
def on_chat(*args):
if (args[0]["name"] == "IRCBot"):
return
msg = args[0]["name"]+"~ "
if ("image" in args[0]):
filename = "https://livechan.net/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"
print
def on_request_location(*args):
global chat_room
socketIO.emit('subscribe', curr_chat_room);
def login(callback=on_chat):
#image_response = fetch('https://livechan.net/captcha.jpg')
#image_data = image_response.read()
#with open('captcha.jpg', 'w') as f:
# f.write(image_data)
#os.system("open captcha.jpg")
#digits = int(raw_input("enter the captcha: "))
#post_params = {}
#post_params["digits"] = digits
#login_response = post('https://livechan.net/login', post_params)
#login_html = login_response.read()
cookie = cookielib.Cookie(version=0, name='password_livechan', value='somepassword', port=None, port_specified=False, domain='livechan.net',
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://livechan.net',
cookies={'password_livechan': livechan_pass})
socketIO.on('chat', callback)
socketIO.on('request_location', on_request_location)
thread.start_new_thread ( socketIO.wait, () )