#!/usr/bin/env python "download random text file from gutenberg project into current directory; `pip install Gutenberg` first" import random from gutenberg.acquire import load_etext from gutenberg.cleanup import strip_headers list_generated_random = [] def get_unique_random(lower, upper): choice = random.randrange(lower, upper) while choice in list_generated_random: choice = random.randrange(lower, upper) list_generated_random.append(choice) return choice def main(): while True: doc_id = get_unique_random(1, 53000) print("downloading doc id: %s" % doc_id) text = strip_headers(load_etext(doc_id)).strip() with open('%s.txt' % doc_id, 'w+') as f: f.write(text) print('%s.txt written' % doc_id) if __name__ == '__main__': try: main() except KeyboardInterrupt: print('exiting\ngoodbye :)')