added text_to_speech#3679
added text_to_speech#3679asperduti wants to merge 8 commits intoTheAlgorithms:masterfrom asperduti:text_to_speech
Conversation
dhruvmanila
left a comment
There was a problem hiding this comment.
Please provide tests from the following, whichever you prefer:
doctest, unittest, pytest
dhruvmanila
left a comment
There was a problem hiding this comment.
You would also require to write tests for the function. This would be a bit difficult so if you face any problem, ping me, but before that please try first.
Split text_to_speech function.
Hi @dhruvmanila I structured the code differently, I split it up to make easier the testing. |
|
Hi @dhruvmanila I worked on all the requested changes. |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Hi, @ xcodz-dot , @dhruvmanila . Did you check it? |
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
cc: @cclauss |
|
Hi, I will take a look at this in a day or two. |
| html = "<p>Listen to the MP3 file :<BR></p> <!> \ | ||
| <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' \ | ||
| width='470' height='24' id='single1' name='single1'> \ | ||
| <param name='movie' value='player.swf'> \ | ||
| <param name='allowfullscreen' value='true'> \ | ||
| <param name='allowscriptaccess' value='always'> \ | ||
| <param name='wmode' value='transparent'> \ | ||
| <param name = 'flashvars' \ | ||
| value='file=/output/0360061001606401513/59405670.mp3'> \ | ||
| <embed \ | ||
| id='single2' \ | ||
| name='single2' \ | ||
| src='player.swf' \ | ||
| width='470' \ | ||
| height='24' \ | ||
| bgcolor='#000000' \ | ||
| allowscriptaccess='always' \ | ||
| allowfullscreen='true' \ | ||
| flashvars='file=/output/0360061001606401513/59405670.mp3' \ | ||
| /> \ | ||
| </object>" |
There was a problem hiding this comment.
Triple-quoted multiline strings work in Python and as PEP8 says, backslashes should be avoided in Python code.
| html = "<p>Listen to the MP3 file :<BR></p> <!> \ | |
| <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' \ | |
| width='470' height='24' id='single1' name='single1'> \ | |
| <param name='movie' value='player.swf'> \ | |
| <param name='allowfullscreen' value='true'> \ | |
| <param name='allowscriptaccess' value='always'> \ | |
| <param name='wmode' value='transparent'> \ | |
| <param name = 'flashvars' \ | |
| value='file=/output/0360061001606401513/59405670.mp3'> \ | |
| <embed \ | |
| id='single2' \ | |
| name='single2' \ | |
| src='player.swf' \ | |
| width='470' \ | |
| height='24' \ | |
| bgcolor='#000000' \ | |
| allowscriptaccess='always' \ | |
| allowfullscreen='true' \ | |
| flashvars='file=/output/0360061001606401513/59405670.mp3' \ | |
| /> \ | |
| </object>" | |
| html = """<p>Listen to the MP3 file :<BR></p> <!> | |
| <object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' | |
| width='470' height='24' id='single1' name='single1'> | |
| <param name='movie' value='player.swf'> | |
| <param name='allowfullscreen' value='true'> | |
| <param name='allowscriptaccess' value='always'> | |
| <param name='wmode' value='transparent'> | |
| <param name = 'flashvars' | |
| value='file=/output/0360061001606401513/59405670.mp3'> | |
| <embed | |
| id='single2' | |
| name='single2' | |
| src='player.swf' | |
| width='470' | |
| height='24' | |
| bgcolor='#000000' | |
| allowscriptaccess='always' | |
| allowfullscreen='true' | |
| flashvars='file=/output/0360061001606401513/59405670.mp3' | |
| /> | |
| </object>""" |
| import requests | ||
|
|
||
| LANGUAGES_VOICES = { | ||
| "US English": ["Alice", "Daisy", "George", "Jenna", "John"], |
There was a problem hiding this comment.
| "US English": ["Alice", "Daisy", "George", "Jenna", "John"], | |
| "US English": ("Alice", "Daisy", "George", "Jenna", "John"), |
Tuples take less RAM than lists and they indicate to the reader that the values will not change at runtime.
| if match := re.search(r"\/output\/\d+\/\d+\.mp3", html): | ||
| filename = match.group() | ||
| else: | ||
| filename = None | ||
|
|
||
| return filename |
There was a problem hiding this comment.
| if match := re.search(r"\/output\/\d+\/\d+\.mp3", html): | |
| filename = match.group() | |
| else: | |
| filename = None | |
| return filename | |
| match = re.search(r"\/output\/\d+\/\d+\.mp3", html): | |
| return match.group() if match else None |
| } | ||
|
|
||
|
|
||
| def get_mp3_filename(html: str) -> Optional[str]: |
There was a problem hiding this comment.
Why two different return types? Why not just return an empty str instead of None?
| audio_url = f"http://www.fromtexttospeech.com{mp3_file}" | ||
|
|
||
| mp3data = requests.get(audio_url) |
There was a problem hiding this comment.
| audio_url = f"http://www.fromtexttospeech.com{mp3_file}" | |
| mp3data = requests.get(audio_url) | |
| mp3data = requests.get(f"http://www.fromtexttospeech.com{mp3_file}") |
There was a problem hiding this comment.
This is not really mp3data. Instead, it is a requests.Response so perhaps response would be a better variable name than mp3data.
| # save the data as an mp3 file | ||
| if mp3data.status_code != 404: | ||
| with open(audio_path, "w+b") as f: | ||
| f.write(mp3data.content) | ||
| return True | ||
|
|
||
| return False |
There was a problem hiding this comment.
| # save the data as an mp3 file | |
| if mp3data.status_code != 404: | |
| with open(audio_path, "w+b") as f: | |
| f.write(mp3data.content) | |
| return True | |
| return False | |
| try: | |
| response.raise_for_status() | |
| # save the data as an mp3 file | |
| with open(audio_path, "wb") as f: | |
| f.write(response.content) | |
| return True | |
| except requests.HTTPError: | |
| return False |
| self.assertEqual("/output/0360061001606401513/59405670.mp3", mp3_file_name) | ||
|
|
||
| def test_get_mp3_file_invalid_html(self): | ||
| html = "<p>Listen to the MP3 file :<BR></p> <!> \ |
There was a problem hiding this comment.
Triple-quoted str with no backslashes please.
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Please reopen this pull request once you commit the changes requested or make improvements on the code. If this is not the case and you need some help, feel free to seek help from our Gitter or ping one of the reviewers. Thank you for your contributions! |


Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}.