<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import gzip <EOL> import os <EOL> from contextlib import closing <EOL> import requests <EOL> from gutenberg . _domain_model . exceptions import UnknownDownloadUriException <EOL> from gutenberg . _domain_model . persistence import local_path <EOL> from gutenberg . _domain_model . types import validate_etextno <EOL> from gutenberg . _util . os import makedirs <EOL> from gutenberg . _util . os import remove <EOL> _TEXT_CACHE = local_path ( '<STR_LIT:text>' ) <EOL> def _format_download_uri ( etextno ) : <EOL> """<STR_LIT>""" <EOL> uri_root = r'<STR_LIT>' <EOL> if <NUM_LIT:0> < etextno < <NUM_LIT:10> : <EOL> oldstyle_files = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> etextno = int ( etextno ) <EOL> return '<STR_LIT>' . format ( <EOL> root = uri_root , <EOL> name = oldstyle_files [ etextno - <NUM_LIT:1> ] ) <EOL> else : <EOL> etextno = str ( etextno ) <EOL> extensions = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for extension in extensions : <EOL> uri = '<STR_LIT>' . format ( <EOL> root = uri_root , <EOL> path = '<STR_LIT:/>' . join ( etextno [ : len ( etextno ) - <NUM_LIT:1> ] ) , <EOL> etextno = etextno , <EOL> extension = extension ) <EOL> response = requests . head ( uri ) <EOL> if response . ok : <EOL> return uri <EOL> raise UnknownDownloadUriException <EOL> def load_etext ( etextno , refresh_cache = False ) : <EOL> """<STR_LIT>""" <EOL> etextno = validate_etextno ( etextno ) <EOL> cached = os . path . join ( _TEXT_CACHE , '<STR_LIT>' . format ( etextno ) ) <EOL> if refresh_cache : <EOL> remove ( cached ) <EOL> if not os . path . exists ( cached ) : <EOL> makedirs ( os . path . dirname ( cached ) ) <EOL> download_uri = _format_download_uri ( etextno ) <EOL> response = requests . get ( download_uri ) <EOL> response . encoding = '<STR_LIT:utf-8>' <EOL> text = response . text <EOL> with closing ( gzip . open ( cached , '<STR_LIT:w>' ) ) as cache : <EOL> cache . write ( text . encode ( '<STR_LIT:utf-8>' ) ) <EOL> else : <EOL> with closing ( gzip . open ( cached , '<STR_LIT:r>' ) ) as cache : <EOL> text = cache . read ( ) . decode ( '<STR_LIT:utf-8>' ) <EOL> return text <EOL> def _main ( ) : <EOL> """<STR_LIT>""" <EOL> from argparse import ArgumentParser , FileType <EOL> from gutenberg import Error <EOL> from gutenberg . _util . os import reopen_encoded <EOL> parser = ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = int ) <EOL> parser . add_argument ( '<STR_LIT>' , type = FileType ( '<STR_LIT:w>' ) ) <EOL> args = parser . parse_args ( ) <EOL> try : <EOL> text = load_etext ( args . etextno ) <EOL> with reopen_encoded ( args . outfile , '<STR_LIT:w>' , '<STR_LIT:utf8>' ) as outfile : <EOL> outfile . write ( text ) <EOL> except Error as error : <EOL> parser . error ( str ( error ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _main ( ) </s>
