Browse Source

add English version API

j7891618123 3 years ago
parent
commit
fa97e67746
2 changed files with 52 additions and 20 deletions
  1. 4 3
      README.md
  2. 48 17
      slides_API_AI_anchor.py

+ 4 - 3
README.md

@@ -22,16 +22,17 @@ Create a google slide to be converted into a video.
 
 ### Step 2
 
-Add or share permissions to **service@dstest-1-292707.iam.gserviceaccount.com**
+Add or share permissions to **service@dstest-1-292707.iam.gserviceaccount.com** or set the access permission to **public**.
 
 ### Step 3
 
 Find the **presentation id** in the address bar and execute the following command.
 ```bash
-python slides_API_AI_anchor.py --presentation_id {presentation_id} --save_to_local True
+python slides_API_AI_anchor.py --presentation_id {presentation_id} --save_to_local True --eng False
 ```
 
 * `--presentation_id` *(string, required)* – Choose which slide to convert to video.
-* `--save_to_local` *(bool, optional)* – Save the memo locally. If `True`, the img and memo file will be saved in a folder named as the title of the slide (Default: `True`)
+* `--save_to_local` *(bool, optional)* – Save the memo locally. If `True`, the img and memo file will be saved in a folder named as the title of the slide. (Default: `True`)
+* `--eng` *(bool, optional)* –Choose to use the English version. (Default: `False`)
 
 Note: presentation id ![](https://i.imgur.com/3ew0aZF.png)

+ 48 - 17
slides_API_AI_anchor.py

@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 # coding: utf-8
 
-# In[13]:
+# In[39]:
 
 
 from __future__ import print_function
@@ -25,7 +25,7 @@ from distutils.util import strtobool
 SCOPES = ['https://www.googleapis.com/auth/presentations.readonly',
           'https://www.googleapis.com/auth/drive.metadata.readonly']
 
-def main(PRESENTATION_ID, save_to_local):
+def main(PRESENTATION_ID, save_to_local,eng):
     """Shows basic usage of the Slides API.
     Prints the number of slides and elments in a sample presentation.
     """
@@ -51,7 +51,8 @@ def main(PRESENTATION_ID, save_to_local):
     scoped_credentials = credentials.with_scopes(SCOPES)
     creds = credentials
 
-    memo_list=[]
+    notes_list=[]
+    sub_title_list=[]
     img_list=[]
 
     def save_to_img(name, url): 
@@ -89,20 +90,41 @@ def main(PRESENTATION_ID, save_to_local):
             print('-'*80)
             print('Page #', i, ' ',slide['objectId'])
 
-            # Check if the memo exists
+            # Check if the notes exists
             if 'text' in slide['slideProperties']['notesPage']['pageElements'][1]['shape'].keys():
-                print('Memo:',end='')
                 
-                memo = slide['slideProperties']['notesPage']['pageElements'][1]['shape']['text']['textElements'][1]['textRun']['content']
-                pprint.pprint(memo)
-                memo_list.append(memo)
+                notes = slide['slideProperties']['notesPage']['pageElements'][1]['shape']['text']['textElements'][1]['textRun']['content']
+                
+                # Check if the sub_title exists
+                print(notes)
+                print('[sub_title]' in notes)
+                if '[sub_title]' in notes:
+                    sub_title = notes.split('[sub_title]')[1].strip()
+                    print('Sub_title:',end='')
+                    pprint.pprint(sub_title)
+                    sub_title_list.append(sub_title)
+                    
+                    # save to local txt file
+                    if '[sub_title]' in notes:
+                        with open('./'+dir+'/'+presentation['title']+'_sub_title_'+str(i+1)+'.txt', "w") as text_file:
+                            text_file.write(sub_title)
+                        print('\t----> save to',presentation['title']+'_sub_title_'+str(i+1)+'.txt')
+                        
+                    notes = notes.split('[sub_title]')[0].strip()
+                
+                print('Notes:',end='')
+                pprint.pprint(notes)
+                notes_list.append(notes)
+                    
                 # save to local txt file
                 if save_to_local:
-                    with open('./'+dir+'/'+presentation['title']+'_'+str(i+1)+'.txt', "w") as text_file:
-                        text_file.write(memo)
-                    print('\t----> save to',presentation['title']+'_'+str(i+1)+'.txt')
+                    with open('./'+dir+'/'+presentation['title']+'_lines_'+str(i+1)+'.txt', "w") as text_file:
+                        text_file.write(notes)
+                    print('\t----> save to',presentation['title']+'_lines_'+str(i+1)+'.txt')
+                    
             else:
-                memo_list.append("")
+                notes_list.append("")
+                
             # Convert the content of the presentation to png
             thumbnail = service.presentations().pages().getThumbnail(presentationId=PRESENTATION_ID, pageObjectId=slide['objectId']).execute()
             pprint.pprint(thumbnail)
@@ -114,7 +136,11 @@ def main(PRESENTATION_ID, save_to_local):
             
             
         # data
-        my_data = { "name": presentation['title'], "text_content": memo_list, "image_urls": img_list, "avatar": "7", "client_id": calendar.timegm(time.gmtime()) }
+        my_data = { "name": presentation['title'], "text_content": notes_list, "image_urls": img_list, "avatar": "7", "client_id": calendar.timegm(time.gmtime()) }
+        if eng:
+            my_data['sub_titles'] = sub_title_list
+        
+        
         
         # headers
         my_headers = {'accept': 'application/json',
@@ -123,8 +149,12 @@ def main(PRESENTATION_ID, save_to_local):
         # Serializing json   
         json_object = json.dumps(my_data, indent = 4)
         
+        
         # post
-        r = requests.post('http://www.choozmo.com:8888/make_anchor_video_v2', data = json_object, headers = my_headers)
+        if eng:
+            r = requests.post('http://www.choozmo.com:8888/make_anchor_video_eng', data = json_object, headers = my_headers)
+        else:
+            r = requests.post('http://www.choozmo.com:8888/make_anchor_video_v2', data = json_object, headers = my_headers)
         print(r)
         
 
@@ -147,8 +177,9 @@ if __name__ == '__main__':
     parser = argparse.ArgumentParser()
     parser.add_argument('--presentation_id', required=True)
     parser.add_argument('--save_to_local', required=False, default=True)
+    parser.add_argument('--eng', required=False, default=False)
     args = parser.parse_args()
-    main(str(args.presentation_id), strtobool(args.save_to_local))
-    # for test
-#     main('10KhVZes8N6ibHsrM8ZU2NuMDsl8AWk_PSdrtvdXr3Kw', True)
+    main(str(args.presentation_id), strtobool(args.save_to_local), strtobool(args.eng))
+#     for test
+#     main('1FWiSRq150AIEDx9uXWRi9Fj9FhyJAWQWt8iRu_h8KPc', True, True)