加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
qzsimi.py 6.76 KB
一键复制 编辑 原始数据 按行查看 历史
#!/usr/bin/python
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import requests
import re
#import md5
import json
import os
import urllib
import time
import random
class qzone():
'''def __init__(self,uin,pwd):
self.uin=uin
payload='bid_code=qzoneLogin&qq=%s&pmd5=%s'%(uin,md5.new(pwd).hexdigest())
url='http://pt.3g.qq.com/login?act=json&format=3'
self.s=requests
self.s.post(url,data=payload)'''
def __init__(self,sid):
self.sid=sid
self.cookies={'sid':sid}
self.s=requests
def get(self,x=0):
if x:
url="http://m.qzone.com/get_feeds?g_tk=1479423254&res_type=0&refresh_type=1&format=json"
else:
url="http://m.qzone.com/get_feeds?g_tk=1479423254&res_type=1&refresh_type=1&format=json"
f=self.s.get(url,cookies=self.cookies)
return f.text
def post(self,res_id,res_uin,cmt_id,cmt_uin,content,res_type,x=0):
headers={'Content-Type':'application/x-www-form-urlencoded'}
url="http://m.qzone.com/operation/publish_addcomment?g_tk=1479423254"
if x:
payload="res_id=%s&res_uin=%s&format=json&res_type=%s&content=%s&opr_type=addcomment&sid=%s"%(res_id,res_uin,res_type,content,self.sid)
#print payload
else:
payload="res_id=%s&res_uin=%s&format=json&content=%s&cmt_id=%s&cmt_uin=%s&res_type=%s&opr_type=addreply&sid=%s"%(res_id,res_uin,content,cmt_id,cmt_uin,res_type,self.sid)
self.s.post(url,data=payload,headers=headers,cookies=self.cookies)
def simi(content):
payload={'parm':content}
url="http://api.mrtimo.com/Simsimi.ashx"
r=requests.post(url,params=payload)
return r.text
def turing(content):
#图灵机器人接口 可以自己注册key,目前作者的是5000条/天
#http://www.tuling123.com/openapi/record.do?channel=338
payload={'key':'9e01d5c29994c579040270b557a99295','info':content}
url="http://www.tuling123.com/openapi/api"
r=requests.post(url,params=payload)
return r.text
def logout(dic):
fp=open('logout.txt','a')
for key in dic:
fp.write('>'+key+'\n')
fp.write(dic[key]+'\n')
fp.close()
def rfilter(s):
s1=[u"亲爱的"]
a=0
for i in s1:
#a=re.findall(s,i)
a=s.find(i)>0
if a:
#print u'关键词过滤',i
break
return a
def rtextreplace(s):
r=[u"喔,好难回到嗷",
u"呃,这个真的不知道呢",
u"真的假哒啊,好厉害的呢",
u"亲爱哒,不要为难银家嘛",
u"哎呀,讨厌拉~~~",
u"是酱紫么?? 你不要骗我喔。"
]
if rfilter(s):
return r[random.randint(0,len(r))]
else:
return s
def passive():
try:
js=json.loads(qz.get())
#print js
jsq=js['data']['vFeeds']
for j in jsq:
title=j['title']['title']
if title==u'评论' or title==u'回复' or title==u'提到我':
if title==u'提到我':
reg=re.compile("@{.+}")
q=reg.sub("",j['summary']['summary'])
if not q.strip():
reg=re.compile(":")
q=reg.sub("",j['cell_left_thumb']['summary'])
else:
q=j['summary']['summary']
cmt_uin=j['userinfo']['user']['uin']
z='[%s]:%s'%(cmt_uin,q)
if not dic.has_key(z):
if 1:#调用相关的机器人 simi 0,turing 1
r=turing(q)
r=json.loads(r)["text"]
else:
r=simi(q)
s = re.compile(u'点击.+$')
r = s.sub('',r)
r=rtextreplace(r)#过滤替换回答内容
dic[z]=r
print 'simi',q,r
try:
#print j['original']['cell_id']['cellid'],j['cell_left_thumb']['user']['uin'],j['id']['cellid'],j['userinfo']['user']['uin']
qz.post(j['original']['cell_id']['cellid'],
j['cell_left_thumb']['user']['uin'],
j['id']['cellid'],
cmt_uin,
urllib.quote(r.encode('utf8')),
j['comm']['appid']
)
except Exception,e:
print 'simi except',e
if len(dic)>20:
logout(dic)
dic.clear()
except Exception,e:
print 'while1',e
def active():
try:
js=json.loads(qz.get(1))
#print js
jsq=js['data']['vFeeds']
select=0
for j in jsq:
appid=j['comm']['appid']
#print appid #appid 202日志 4相册 311说说
if appid!=311:
continue
try:
reg=re.compile("@{.+}")
q=reg.sub("",j['summary']['summary'])
select=0
except Exception,e:
reg=re.compile(":")
try:
q=reg.sub("",j['cell_left_thumb']['summary'])
except Exception,e:
q=reg.sub("",j['original']['cell_summary']['summary'])
select=1
cmt_uin=j['userinfo']['user']['uin']
z='[%s]:%s'%(cmt_uin,q)
if not dic2.has_key(z):
if 1:#调用相关的机器人 simi 0,turing 1
r=turing(q)
r=json.loads(r)["text"]
else:
r=simi(q)
s = re.compile(u'点击.+$')
r = s.sub('',r)
r=rtextreplace(r)#过滤替换回答内容
dic2[z]=r
print 'simi2',q,r
try:
#res_id,res_uin,cmt_id,cmt_uin,content,res_type,x=0
#print j['id']['cellid'],cmt_uin,j['comm']['appid']
qz.post(
j['id']['cellid'],
cmt_uin,
0,
0,
urllib.quote(r.encode('utf8')),
appid,
1)
time.sleep(3)
except Exception,e:
print 'simi2 except',e
if len(dic2)>20:
logout(dic)
dic.clear()
except Exception,e:
print 'while2',e
qz=qzone('AcjDUM9tUkpzowo52djoypZj')#这里改成你的sid
dic,dic2={},{}
while 1:
passive()
#active()
time.sleep(60)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化