yanwii 8 anni fa
parent
commit
0047f4f4bc
4 ha cambiato i file con 111 aggiunte e 0 eliminazioni
  1. 11 0
      headers.txt
  2. 0 0
      postpage.txt
  3. 0 0
      response.txt
  4. 100 0
      xiaoiceapi.py

+ 11 - 0
headers.txt

@@ -0,0 +1,11 @@
+Accept:*/*
+Accept-Encoding:gzip, deflate
+Accept-Language:en-US,en;q=0.8
+Connection:keep-alive
+Content-Type:application/x-www-form-urlencoded
+Cookie:SINAGLOBAL=4752855180930.138.1495439495988; wb_publish_fist100_3499352987=1; TC-Ugrow-G0=e66b2e50a7e7f417f6cc12eec600f517; TC-V5-G0=06f20d05fbf5170830ff70a1e1f1bcae; _s_tentry=login.sina.com.cn; Apache=5142324397556.712.1495502985732; ULV=1495502985784:2:2:2:5142324397556.712.1495502985732:1495439496010; TC-Page-G0=9183dd4bc08eff0c7e422b0d2f4eeaec; login_sid_t=4fa5e246cdb7e8bd7b9af2cf6d9bc29e; un=avatarmilk@live.com; WB_register_version=4641949e9f3439df; WBStorage=02e13baf68409715|undefined; UOR=,,login.sina.com.cn; appkey=; SSOLoginState=1495509872; SCF=AtbI6VSY8XVbwtQcbW1RCyVNnjDaVvmVJE8IMoqtu99Yxq58SYLCDAJL95ZuZVKfIqv3UsM34YbEbFIZin3H0eY.; SUB=_2A250J9sgDeThGeBM7lYX8CjMwzSIHXVXVUvorDV8PUNbmtAKLUTlkW-D1Wx2SH1J30I2Kc0_yv9bI8wYAg..; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5sgQfVzKkZHOD2r2EH5Oqp5JpX5KzhUgL.FoqESKBcehq71hn2dJLoIp7LxKML1KBLBKnLxKqL1hnLBoMfSK541hM7eKz4; SUHB=0JvwOe6fZHgJl1; ALF=1527045871; wvr=6; WBtopGlobal_register_version=4641949e9f3439df
+Host:weibo.com
+Origin:http://weibo.com
+Referer:http://weibo.com/message/history?uid=5175429989&name=%E5%B0%8F%E5%86%B0
+User-Agent:Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
+X-Requested-With:XMLHttpRequest

File diff suppressed because it is too large
+ 0 - 0
postpage.txt


File diff suppressed because it is too large
+ 0 - 0
response.txt


+ 100 - 0
xiaoiceapi.py

@@ -0,0 +1,100 @@
+import requests
+import json
+import time
+from bs4 import BeautifulSoup
+from flask import Flask,request,jsonify
+
+
+class xiaoiceApi():
+    
+    def __init__(self):
+        self.headers = {}
+        self.loadheaders()
+
+    def loadheaders(self):
+        '''
+            导入headers
+        '''
+        with open("./headers.txt") as headers:
+            line = headers.readline().strip()
+            while line:
+                key = line.split(":")[0]
+                self.headers[key] = line[len(key)+1:]
+                line = headers.readline().strip()            
+
+    def chat(self, input_strs):
+        if not self.headers:
+            return self.dicts("error", "请打开浏览器 复制并将headers放入headers.txt中")
+        '''
+        聊天
+        
+            args (str):   
+                input_strs  问题  
+            return (dict):  
+                status      状态  
+                text        内容        
+        '''
+        data = {
+            'location':'msgdialog',
+            'module':'msgissue',
+            'style_id':1,
+            'text':input_strs,
+            'uid':5175429989,
+            'tovfids':'',
+            'fids':'',
+            'el':'[object HTMLDivElement]',
+            '_t':0,
+        }
+        
+        try:
+            url = 'http://weibo.com/aj/message/add?ajwvr=6'
+            page = requests.post(url, data=data, headers=self.headers)
+            self.savePage(page.text, "./tmp/postpage.txt")
+            if page.json()['code'] == '100000':
+                text = self.loop(input_strs)
+                return self.dicts("sucess", text)
+            else:
+                return self.dicts("failed", page.json()['msg'])
+        except JSONDecodeError:
+            return self.dicts("error", "请查看tmp下页面的内容")
+        except Exception as e:
+            return self.dicts("error", e)
+    
+    def dicts(self, status, text):
+        '''
+            包装return
+        '''
+        return {"status":status, "text":text}
+
+    def loop(self, input_strs):
+        '''  
+            刷新直到获取到回答
+        '''
+        times = 1
+        while times:
+            response = requests.get("http://weibo.com/aj/message/getbyid?ajwvr=6&uid=5175429989&count=1&_t=0" , headers=self.headers)
+            self.savePage(response.text, "./tmp/response.txt")
+            soup = BeautifulSoup(response.json()['data']['html'], "lxml")
+            text = soup.find("p", class_='page').text
+            if text != input_strs or times > 20:
+                break
+            time.sleep(0.5)
+        return text
+            
+    def savePage(self, text, file):
+        with open(file, "w") as f:
+            f.write(text)
+    
+    def api(self):
+        app = Flask(__name__)
+
+        @app.route("/")
+        def index():
+            que = request.args.get("que")
+            ans = self.chat(que)
+            return jsonify(ans)
+        app.run()
+
+xb = xiaoiceApi()
+xb.api()
+    

Some files were not shown because too many files changed in this diff