yanwii 7 éve
szülő
commit
02b5b9dc9e
8 módosított fájl, 33 hozzáadás és 24 törlés
  1. 1 0
      .gitignore
  2. 5 1
      README.md
  3. BIN
      __pycache__/xiaoiceapi.cpython-36.pyc
  4. 5 6
      headers.txt
  5. 2 1
      test.py
  6. 0 0
      tmp/postpage.txt
  7. 0 0
      tmp/response.txt
  8. 20 16
      xiaoiceapi.py

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+headers.txt

+ 5 - 1
README.md

@@ -1,5 +1,9 @@
 # msxiaoiceapi
-  
+
+## UPDATE
+
+- 支持图片  
+
 ## 说明   
 需要  python3+ BeautifulSoup flask 目前暂时只支持文字  
 

BIN
__pycache__/xiaoiceapi.cpython-36.pyc


+ 5 - 6
headers.txt

@@ -1,11 +1,10 @@
 Accept:*/*
-Accept-Encoding:gzip, deflate
-Accept-Language:en-US,en;q=0.8
+Accept-Encoding:gzip, deflate, br
+Accept-Language:en-US,en;q=0.9
 Connection:keep-alive
 Content-Type:application/x-www-form-urlencoded
-Cookie:SINAGLOBAL=4752855180930.138.1495439495988; wb_publish_fist100_3499352987=1; un=avatarmilk@live.com; wb_publish_fist100_6254606088=1; TC-V5-G0=666db167df2946fecd1ccee47498a93b; TC-Ugrow-G0=370f21725a3b0b57d0baaf8dd6f16a18; SSOLoginState=1496196304; SCF=AtbI6VSY8XVbwtQcbW1RCyVNnjDaVvmVJE8IMoqtu99YMAnTg6eYuxpMDBTdn1j529KvnlhpkTxinKRf79fiP0w.; SUB=_2A250KlSADeThGeBM7lYX8CjMwzSIHXVXXsFIrDV8PUNbmtAKLUfAkW8mdOxsXWgULDe2-SyF98UdEn5LAg..; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5sgQfVzKkZHOD2r2EH5Oqp5JpX5KMhUgL.FoqESKBcehq71hn2dJLoI7p_MNyDqgvDqcHyMs.t; SUHB=04W-FKC6o5WSbG; ALF=1527732300; wvr=6; TC-Page-G0=1ac1bd7677fc7b61611a0c3a9b6aa0b4; _s_tentry=-; Apache=9616211080131.691.1496196339846; ULV=1496196339865:8:8:1:9616211080131.691.1496196339846:1495856969416; UOR=,,login.sina.com.cn
+Cookie:SINAGLOBAL=3844830062504.4917.1511487132238; UOR=,,login.sina.com.cn; wvr=6; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WhEI.Xcp45HMw6jpXrbxZze5JpX5KMhUgL.FoeX1K.0SKz41hM2dJLoI7_29g8ECJvQdbX_TBtt; YF-Ugrow-G0=ad83bc19c1269e709f753b172bddb094; ALF=1547277072; SSOLoginState=1515741072; SCF=Ar1MtzZIw65p4PA4zzzAcQilutQxZA3Lg8wcVrHqur_DT_jvsTT6WZutkTEVWbd7d_XTrVenbroUkO8lRb38UKw.; SUB=_2A253XC_BDeThGeVK4lsS9SzFwzuIHXVUKAYJrDV8PUNbmtBeLWz7kW9NTDiy4Zfd-dV_V12vgvTj4HNyKh1-gSN5; SUHB=0btsF6x1OGuyrI; YF-V5-G0=c6a30e994399473c262710a904cc33c5; _s_tentry=login.sina.com.cn; Apache=7823804863815.256.1515741075657; ULV=1515741075689:19:6:3:7823804863815.256.1515741075657:1515594508689; wb_cusLike_3499352987=N; YF-Page-G0=19f6802eb103b391998cb31325aed3bc
 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
+Referer:https://weibo.com/message/history?uid=5175429989&name=%E5%B0%8F%E5%86%B0
+User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
 X-Requested-With:XMLHttpRequest

+ 2 - 1
test.py

@@ -1,3 +1,4 @@
 import xiaoiceapi 
 xb = xiaoiceapi.xiaoiceApi()
-print(xb.chat("你是谁"))
+print(xb.chat("你的照片"))
+    

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
tmp/postpage.txt


A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
tmp/response.txt


+ 20 - 16
xiaoiceapi.py

@@ -5,7 +5,6 @@ import sys
 from bs4 import BeautifulSoup
 from flask import Flask,request,jsonify
 import re
-from PIL import Image
 
 class xiaoiceApi():
     
@@ -55,18 +54,18 @@ class xiaoiceApi():
             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("succeed", text)
+                code, text, res_type = self.loop(input_strs)
+                return self.dicts(code, res_type, text)
             else:
-                return self.dicts("failed", page.json()['msg'])
+                return self.dicts("500", "failed", page.json()['msg'])
         except Exception as e:
-            return self.dicts("error", e)
+            return self.dicts("500", "error", e)
     
-    def dicts(self, status, text):
+    def dicts(self, status, res_type, text):
         '''
             包装return
         '''
-        return {"status":status, "text":text}
+        return {"status":status, "type":res_type, "text":text}
 
     def loop(self, input_strs):
         '''  
@@ -81,11 +80,14 @@ class xiaoiceApi():
             soup = BeautifulSoup(response.json()['data']['html'], "lxml")           
             text = soup.find("p", class_='page')
             if text:
-                text = text.text
-                if text == input_strs:
+                if text.text == input_strs:
+                    time.sleep(0.3)
                     continue
             elif "收起" in soup.get_text():
+                #返回imgURL
                 imgUrl = soup.find(href=re.compile('msget')).get('href')
+                return 200, imgUrl, "img"
+                '''
                 text = "图片地址: " + imgUrl
                 picRespone = requests.get(imgUrl, headers={"Cookie":self.headers["Cookie"]})
                 if picRespone.status_code == 200:
@@ -93,21 +95,23 @@ class xiaoiceApi():
                         f.write(picRespone.content)
                 img = Image.open('pic.jpg')
                 img.show()
+                '''
+                
             elif "mp3" in soup.get_text():
                 mp3Url = soup.find(href=re.compile('msget')).get('href')
+                return 200, mp3Url,"voice"
+                # 返回语音URL
+
+                '''
                 text = "语音地址: " + imgUrl
                 picRespone = requests.get(imgUrl, headers={"Cookie":self.headers["Cookie"]})
                 if picRespone.status_code == 200:
                     with open('voice.mp3', 'wb') as f:
                         f.write(picRespone.content)
-            else:
-                print("第{}次重试".format(times))
-                continue
-            time.sleep(0.3)
-            return text
+                '''
+            return 200, text.text, "text"
         text = "错误: 已达到最大重试次数"
-        print (text)
-        return text
+        return 500, text, "failed"
             
     def savePage(self, text, file):
         with open(file, "w") as f:

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott