forked from meshchaninov/alfred-translate-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c581db6
commit b5f5f0f
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import sys | ||
import subprocess | ||
import re | ||
|
||
def is_english(string): | ||
# Check if the string contains only English characters | ||
if re.match("^[A-Za-z0-9\s\.\,\!\?\;\'\"]+$", string): | ||
return True | ||
return False | ||
|
||
def download_pronunciation(text): | ||
url = f"http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q={text}&tl=en" | ||
filename = "pronunciation.mp3" | ||
try: | ||
subprocess.run(['curl', '-o', filename, url], check=True) | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error downloading pronunciation: {e}") | ||
sys.exit(1) | ||
|
||
def play_audio(file_path): | ||
try: | ||
subprocess.run(['afplay', file_path], check=True) | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error playing audio: {e}") | ||
sys.exit(1) | ||
|
||
def main(): | ||
if len(sys.argv) != 2: | ||
print("Usage: python pronounce.py 'your string here'") | ||
return | ||
|
||
text = sys.argv[1] | ||
if is_english(text): | ||
download_pronunciation(text) | ||
play_audio('pronunciation.mp3') | ||
os.remove(filename) | ||
else: | ||
print("The string is not in English.") | ||
|
||
if __name__ == "__main__": | ||
main() |