浏览代码

Add exception catching for writing content to file

Unknown 8 年之前
父节点
当前提交
21442c71d8
共有 1 个文件被更改,包括 10 次插入3 次删除
  1. 10 3
      tts/voxygen/voxygen.py

+ 10 - 3
tts/voxygen/voxygen.py

@@ -52,9 +52,16 @@ def get_audio(voice, text, filepath, cache):
         r = requests.get("https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php", params=payload, stream=True)
         logging.debug("Trying to get url: %s response code: %s", r.url, r.status_code)
 
-        if r.status_code == 200:
-            with open(os.path.abspath(filepath), "wb") as sound_file:
-                sound_file.write(r.content)
+        try:
+            if r.status_code == 200:
+                with open(os.path.abspath(filepath), "w") as sound_file:
+                    sound_file.write(r.content)
+        except IOError as e:
+            print "I/O error({0}): {1}".format(e.errno, e.strerror)
+        except ValueError:
+            print "Could not convert data to an integer."
+        except:
+            print "Unexpected error:", sys.exc_info()[0]
 
 
 def play_audio(music_file, volume=0.8):