Skip to content

Instantly share code, notes, and snippets.

@onlurking
Created December 26, 2016 22:51
Show Gist options
  • Save onlurking/5185abd22f89592057ce3b962f89ad72 to your computer and use it in GitHub Desktop.
Save onlurking/5185abd22f89592057ce3b962f89ad72 to your computer and use it in GitHub Desktop.
import asyncio
from os import devnull
from urllib.request import urlopen
from urllib.parse import quote
from subprocess import call
async def produce_lines(filename):
try:
with open(filename, 'r') as file:
for line in file:
yield line.rstrip('\n')
await asyncio.sleep(1)
except FileNotFoundError:
with open("poe.txt", 'w', encoding='utf8') as file:
file.write(
(urlopen("http://www.gutenberg.org/dirs/2/1/4/2147/2147-8.txt")
.read()
.decode("iso8859")))
print("Hi, I've downloaded the The Works of Edgar Allan Poe (Volume 1) for you.\nPlease, re-run this program, shall you..?")
async def print_lines(filename):
async for line in produce_lines(filename):
if line:
with open(devnull, 'wb'):
call(['mpv', 'http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q={}&tl=En-gb'.format(
quote(line, safe=""))], stdout=open(devnull, 'wb'))
def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(print_lines("poe.txt"))
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment