Browse Source

glide +pptx (not done)

ming 3 years ago
parent
commit
9158c4e338
8 changed files with 72 additions and 65 deletions
  1. BIN
      api/__pycache__/gSlide.cpython-39.pyc
  2. BIN
      api/__pycache__/main.cpython-39.pyc
  3. 14 26
      api/gSlide.py
  4. 45 21
      api/main.py
  5. BIN
      api/mypic.jpg
  6. 13 0
      api/pyppt.py
  7. BIN
      api/test.pptx
  8. 0 18
      api/test.py

BIN
api/__pycache__/gSlide.cpython-39.pyc


BIN
api/__pycache__/main.cpython-39.pyc


+ 14 - 26
api/gSlide.py

@@ -1,5 +1,3 @@
-
-
 from __future__ import print_function
 import os.path
 from googleapiclient.discovery import build
@@ -16,6 +14,7 @@ import time
 import os
 import shutil
 from distutils.util import strtobool
+from pptx import Presentation
 
 SCOPES = ['https://www.googleapis.com/auth/presentations.readonly',
           'https://www.googleapis.com/auth/drive.metadata.readonly']
@@ -79,29 +78,18 @@ def parse_slide_url(slide_url,eng):
 
     return slide_content['name'],slide_content['text_content'],slide_content['image_urls']
 
-def parse_slide_url(fileanme,img_upload_folder,img_url,eng):
-    
-    notes_list=[]
-    sub_title_list=[]
-    img_list=[]
-    
+#www.choozmo.com:8168/tmp_pttx/1634388575388378.pptx
+#www.choozmo.com:8168/tmp_img/16344598943505244.jpg
+def parse_pttx_url(fileanme,img_upload_folder,img_url,eng):
     prs = Presentation(fileanme)
+    filename = filename[:filename.find('#')]
+    imgurl = []
     for slide in prs.slides:
-        notes_slide = slide.notes_slide
-        text_frame = notes_slide.notes_text_frame
-        print(text_frame.text)
-        shapes = slide.shapes
-        notes_list.append(text_frame.text)
-        for s in shapes:
-            img_name = str(time.time()).replace('.','')
-            image = s.image
-            image_bytes = image.blob
-            # ---make up a name for the file, e.g. 'image.jpg'---
-            image = Image.open(image_bytes)
-            image= image.convert("RGB")
-            image.save(img_upload_folder+img_name+'.jpg')
-            img_list.append(img_url+img_name+'.jpg')
-            #image_filename = ''
-            #with open(image_filename, 'wb') as f:
-            #    f.write(image_bytes)
-    return filename, notes_list, img_list
+        for shape in slide.shapes:
+            with open('mypic.jpg', 'wb') as f:
+                f.write(shape.image.blob)
+    text = []
+    for page, slide in enumerate(prs.slides):
+        textNote = slide.notes_slide.notes_text_frame.text
+        print(textNote)
+    return filename, imgurl, text

+ 45 - 21
api/main.py

@@ -415,10 +415,10 @@ async def create_upload_file(file: UploadFile = File(...)):
 @app.post("/upload_pttx/")
 async def upload_pttx(file: UploadFile = File(...)):
     try:
-        if "_" in file.filename:
-            return {'msg':{'eng':'symbol"_" is not allowed in file name','zh':'檔案無法使用檔名不能含有"_"符號'}}
+        if "#" in file.filename:
+            return {'msg':{'eng':'symbol"#" is not allowed in file name','zh':'檔案無法使用檔名不能含有"#"符號'}}
         else:
-            pttx_name = str(time.time()).replace('.','')+'.pptx'
+            pttx_name = file.filename.replace('.pptx','#')+str(time.time()).replace('.','')+'.pptx'
             with open(pttx_dest+pttx_name, "wb+") as file_object:
                 file_object.write(file.file.read())
             return {"msg": resource_server+pttx_sub_folder+pttx_name}
@@ -436,27 +436,20 @@ async def make_anchor_video_gSlide(req:util.models.gSlide_req,token: str = Depen
     
     if len(image_urls) != len(text_content):
         return {'msg':{'eng':'number of subtitles and images(videos) should be the same','zh':'副標題數量、圖片(影片)數量以及台詞數量必須一致'}}
+    
     for idx in range(len(image_urls)):
         if 'http' not in image_urls[idx]:
             image_urls[idx] = 'http://'+image_urls[idx]
-    if req.multiLang==0:
-        for txt in text_content:
-            if re.search('[a-zA-Z]', txt) !=None:
-                print('語言錯誤')
-                return {'msg':{'eng':'English is not allowed in subtitles','zh':'輸入字串不能包含英文字!'}}
-            if re.search(',', txt) !=None:
-                print('包含非法符號')
-                return {'msg':{'eng':'symbol "," is not allowede','zh':'輸入不能含有","符號'}}
+    
+    mulitLang, hasError = Check_text_content(text_content)
+    if hasError is not False :
+        return hasError
+
+    imgError = Check_image_url(image_urls)
+    if imgError is not False :
+        return imgError
+    
     name_hash = str(time.time()).replace('.','')
-    for imgu in image_urls:
-        try:
-            if get_url_type(imgu) =='video/mp4':
-                r=requests.get(imgu)
-            else:
-                im = Image.open(requests.get(imgu, stream=True).raw)
-                im= im.convert("RGB")
-        except:
-            return {'msg':{'eng':req.imgurl+'cant be proccessed','zh':"無法辨別圖片網址"+req.imgurl}}
     user_id = get_user_id(token)
     proto_req = util.models.request_normal()
     proto_req.text_content = text_content
@@ -799,6 +792,37 @@ def notify_group(msg):
         headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
         r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
 
+def notify_choozmo(msg):
+    #'WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD' is ChoozmoTeam
+    glist = ['WekCRfnAirSiSxALiD6gcm0B56EejsoK89zFbIaiZQD']
+    for gid in glist:
+        headers = {"Authorization": "Bearer " + gid,"Content-Type": "application/x-www-form-urlencoded"}
+        r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params={"message": msg})
+
+def Check_text_content(text_content):
+    mulitLang = 0
+    status = False
+    for txt in text_content:
+        if re.search('[a-zA-Z]', txt) !=None:
+            mulitLang = 1
+        if re.search(',', txt) !=None:
+            print('包含非法符號')
+            status = {'msg':{'eng':'symbol "," is not allowede','zh':'輸入不能含有","符號'}}
+    return mulitLang, status
+
+def Check_image_url(image_urls):
+    status =False
+    for imgu in image_urls:
+        try:
+            if get_url_type(imgu) =='video/mp4':
+                r=requests.get(imgu)
+            else:
+                im = Image.open(requests.get(imgu, stream=True).raw)
+                im= im.convert("RGB")
+        except:
+            status = {'msg':{'eng':req.imgurl+'cant be proccessed','zh':"無法辨別圖片網址"+req.imgurl}}
+    return status
+    
 
 def gen_video_long_queue(name_hash,name,text_content, image_urls,avatar,multiLang,video_id,user_id):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
@@ -1068,7 +1092,7 @@ def call_speech(text,speaker):
     fw.close()
     shutil.copy(text+'.wav',speech_dest+text+'.wav')
     os.remove(text+'.wav')
-    notify_group("speech at www.choozmo.com:8168/speech/"+text+".wav")
+    notify_choozmo("speech at www.choozmo.com:8168/speech/"+text+".wav")
 
 
 def verify_jwt_token(token):

BIN
api/mypic.jpg


+ 13 - 0
api/pyppt.py

@@ -0,0 +1,13 @@
+from pptx import Presentation
+
+prs = Presentation('test.pptx')
+
+
+for slide in prs.slides:
+    for shape in slide.shapes:
+        with open('mypic.jpg', 'wb') as f:
+            f.write(shape.image.blob)
+
+for page, slide in enumerate(prs.slides):
+    textNote = slide.notes_slide.notes_text_frame.text
+    print(textNote)

BIN
api/test.pptx


+ 0 - 18
api/test.py

@@ -1,18 +0,0 @@
-import util.user
-import dataset
-import models
-from passlib.context import CryptContext
-import time
-import mailer
-import first
-
-def prof():
-    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
-    user_id = 36
-
-
-    user_obj = next(iter(db.query('SELECT * FROM users where id ="'+str(user_id)+'"')))
-    user_obj['email'] = 'ming'
-    db['users'].update(dict(user_obj), ['id'])
-
-prof()