Skip to content

Instantly share code, notes, and snippets.

@butaji
Created November 16, 2012 17:20
Show Gist options
  • Save butaji/4089152 to your computer and use it in GitHub Desktop.
Save butaji/4089152 to your computer and use it in GitHub Desktop.
Turing-test N2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import urllib2
import time
import random
from multiprocessing import Pool
h1 = {
"Host" : "www.simsimi.com",
"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.9",
"Accept": "text/plain",
"Referer" : "http://www.simsimi.com/talk.htm?lc=ru"
}
s1 = requests.session(headers=h1)
r = s1.get("http://www.simsimi.com/talk.htm?lc=ru")
def ask_simsimi(msg):
url = u'http://www.simsimi.com/func/req?lc=ru&msg=' + urllib2.quote(msg.encode("utf8"))
r = s1.get(url,hooks=dict(pre_request=log_request, response=log_response))
resp = r.json["response"]
return resp
def log_response(response):
#print response.text
return response
def log_request(request):
#print 'HTTP {} {} {}'.format(request.method, request.url, request.data)
return request
h2 = {
"X-Requested-With": "XMLHttpRequest",
"Host": "chatvdvoem.ru",
"Origin": "http://chatvdvoem.ru",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4",
"Referer": "http://chatvdvoem.ru/",
}
s2 = requests.session(headers=h2)
st = s2.get("http://chatvdvoem.ru",
hooks=dict(pre_request=log_request, response=log_response))
print st.status_code
def chat_wait_opponent(cid):
#print 'waiting for opponent ', cid
r2 = s2.post("http://chatvdvoem.ru/send", data = {"action" : "wait_opponent", "uid" : str(cid)},
hooks=dict(pre_request=log_request, response=log_response))
#print "opponent: ", r2.text
def chat_start():
#print 'connection'
r2 = s2.post("http://chatvdvoem.ru/send", data = {"action" : "get_uid"},
hooks=dict(pre_request=log_request, response=log_response))
#print r2.text
if r2.json["result"] == 'ok':
chat_wait_opponent(r2.json["uid"])
pool.apply_async(chat_get_events,[r2.json["uid"]])
chat_get_events(r2.json["uid"])
def send_hello(uid, cid, msg):
#print "send_hello", msg
try:
r2 = s2.post("http://chatvdvoem.ru/send", data = {"action" : "send_message", "uid" : uid, "cid" : cid, "message" : msg }, timeout=10, hooks=dict(pre_request=log_request, response=log_response))
#print "chat_send_ready result ", r2.text
except Exception as e:
print e
cid = ''
chat_status = 'disconnect'
def chat_get_events(chat_id):
global chat_status
global cid
global last_message
while True:
#print 'chat_get_events'
try:
time.sleep(random.randint(1, 3))
url = str("http://chatvdvoem.ru/events?"+ chat_id)
#print "chat_status ", chat_status
r2 = s2.post(url, data = {"action" : 'get'}, timeout=20,
hooks=dict(pre_request=log_request, response=log_response))
#print "event", r2.text
action = r2.json["action"]
if action == "new_message":
print r2.json["user"], ": ", r2.json["message"]
if (len(r2.json["user"]) > 2):
sim = ask_simsimi(r2.json["message"])
#print "sim:",sim
time.sleep(4)
send_hello(chat_id, cid, sim)
if action == "get_ready":
chat_status = 'get_ready'
cid = r2.json["cid"]
chat_send_ready(chat_id, cid)
if action == "start_chat":
print "================================================="
chat_status = 'connect'
#send_hello(chat_id, r2.json["cid"], u'привет!')
if action == "stop_chat":
chat_status = 'disconnect'
chat_new_opponent(chat_id, cid)
#print "new chat"
except Exception as e:
if chat_status != 'disconnect':
chat_new_opponent(chat_id, cid)
else:
chat_wait_opponent(chat_id)
chat_status = 'disconnect'
print e
def chat_new_opponent(uid, cid):
#print "chat_new_opponent uid ", uid, " cid ", cid
try:
r2 = s2.post("http://chatvdvoem.ru/send", data = {"action" : "wait_new_opponent", "uid" : uid, "cid" : cid}, timeout=10, hooks=dict(pre_request=log_request, response=log_response))
#print "chat_send_ready result ", r2.text
except Exception as e:
print e
def chat_send_ready(uid, cid):
#print "chat_send_ready uid ", uid, " cid ", cid
try:
r2 = s2.post("http://chatvdvoem.ru/send", data = {"action" : "set_ready", "uid" : uid, "cid" : cid}, timeout=10, hooks=dict(pre_request=log_request, response=log_response))
except Exception as e:
print e
def chat_send_message(message, uid, cid):
#print "chat_send_message ", message
r2 = s2.post("http://chatvdvoem.ru/send", data = {"action" : "send_message", "uid" : uid, "cid" : cid, "message": message}, timeout=10,
hooks=dict(pre_request=log_request, response=log_response))
#print "chat_send_message result ", r2.text
pool = Pool(5)
chat_start()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import urllib2
import time
h1 = {
"Host" : "www.simsimi.com",
"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.9",
"Accept": "text/plain",
"Referer" : "http://www.simsimi.com/talk.htm?lc=ru"
}
s1 = requests.session(headers=h1)
r = s1.get("http://www.simsimi.com/talk.htm?lc=ru")
def ask_simsimi(msg):
url = u'http://www.simsimi.com/func/req?msg=' + urllib2.quote(msg.encode("utf8"))
r = s1.get(url,hooks=dict(pre_request=log_request, response=log_response))
resp = r.json["response"]
return resp
def log_response(response):
#print response.text
return response
def log_request(request):
#print 'HTTP {} {} {}'.format(request.method, request.url, request.data)
return request
h2 = {
"Host" : "front4.omegle.com",
"User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17",
"Accept" : "application/json",
"Origin": "http://omegle.com",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cache-Control" : "max-age=0",
"Referer" : "http://omegle.com/",
"Accept-Language" : "en-us",
"Pragma" : "no-cache",
"Connection" : "keep-alive"
}
s2 = requests.session(headers=h2)
st = s2.get("http://omegle.com",
hooks=dict(pre_request=log_request, response=log_response))
print st.status_code
cid = ''
def chat_start():
global cid
r= s2.post("http://front4.omegle.com/start?rcs=1&firstevents=1&spid=",
hooks=dict(pre_request=log_request, response=log_response))
cid = r.json["clientID"]
print "======================"
def get_events():
while True:
try:
r = s2.post("http://front4.omegle.com/events",
data = {"id" : str(cid)},
hooks=dict(pre_request=log_request, response=log_response), timeout=20)
for item in r.json:
action = item[0]
if (action == 'gotMessage'):
print "stranger: ", item[1]
sim = ask_simsimi(item[1])
time.sleep(4)
send_msg(sim)
if (action == 'strangerDisconnected'):
chat_start()
time.sleep(4)
except Exception as e:
chat_start()
def send_msg(msg):
r= s2.post("http://front4.omegle.com/send",
data = {"id" : str(cid), "msg" : msg},
hooks=dict(pre_request=log_request, response=log_response))
print "me: ", msg
chat_start()
get_events()
@butaji
Copy link
Author

butaji commented Nov 17, 2012

Showcase how chatbot can pretend its human

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment