|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "02e294a0", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "## Want to try a bulk download example?\n", |
| 11 | + "\n", |
| 12 | + "## This is just to get random words generated. \n", |
| 13 | + "from nltk.corpus import words\n", |
| 14 | + "import random\n", |
| 15 | + "\n", |
| 16 | + "\n", |
| 17 | + "\n", |
| 18 | + "import requests\n", |
| 19 | + "from tqdm import tqdm\n", |
| 20 | + "import datetime\n", |
| 21 | + "\n", |
| 22 | + "import os\n", |
| 23 | + "\n", |
| 24 | + "payload = {\n", |
| 25 | + " \"maintext1\": \"This is a test. Let's see Tyler's dog! Indianapolis-area homes for sale drop to $259,450 median price\",\n", |
| 26 | + " \"shape1\": \"facebook\",\n", |
| 27 | + " \"textcolor1\": \"black\",\n", |
| 28 | + " \"transparency1\": \"medium\" \n", |
| 29 | + "}\n", |
| 30 | + "\n", |
| 31 | + "# It takes a while to generate the sample texts. This is where you want to bring in your texts.\n", |
| 32 | + "sampletexts = []\n", |
| 33 | + "for i in range(0, 100):\n", |
| 34 | + " wordswanted = random.randrange(3, 30)\n", |
| 35 | + " rand_words = ' '.join(random.sample(words.words(), wordswanted))\n", |
| 36 | + " sampletexts.append(rand_words)\n", |
| 37 | + "\n", |
| 38 | + " \n", |
| 39 | + "# Normal program stuff starts here \n", |
| 40 | + "baseurl = \"http://localhost:5000/generate/\"\n", |
| 41 | + "\n", |
| 42 | + "downloaddir = \"downloads/\"\n", |
| 43 | + "os.makedirs(downloaddir, exist_ok=True)\n", |
| 44 | + "\n", |
| 45 | + "textlog = {}\n", |
| 46 | + "for sampletext in tqdm(sampletexts):\n", |
| 47 | + " payload['maintext1'] = sampletext\n", |
| 48 | + " r = requests.get(baseurl, data=payload)\n", |
| 49 | + " filename = downloaddir + r.headers['Content-Disposition'].split(\"filename=\")[-1]\n", |
| 50 | + " if os.path.exists(filename): # Do not clobber an existing file, for all sorts of reasons\n", |
| 51 | + " print(f\"Will NOT overwrite {filename}\")\n", |
| 52 | + " else:\n", |
| 53 | + " with open(filename, \"wb\") as outfile:\n", |
| 54 | + " outfile.write(r.content)\n", |
| 55 | + " # print(f\"Saved file as {filename}\")\n", |
| 56 | + " textlog[filename] = payload" |
| 57 | + ] |
| 58 | + } |
| 59 | + ], |
| 60 | + "metadata": { |
| 61 | + "kernelspec": { |
| 62 | + "display_name": "Python 3", |
| 63 | + "language": "python", |
| 64 | + "name": "python3" |
| 65 | + }, |
| 66 | + "language_info": { |
| 67 | + "codemirror_mode": { |
| 68 | + "name": "ipython", |
| 69 | + "version": 3 |
| 70 | + }, |
| 71 | + "file_extension": ".py", |
| 72 | + "mimetype": "text/x-python", |
| 73 | + "name": "python", |
| 74 | + "nbconvert_exporter": "python", |
| 75 | + "pygments_lexer": "ipython3", |
| 76 | + "version": "3.7.2" |
| 77 | + } |
| 78 | + }, |
| 79 | + "nbformat": 4, |
| 80 | + "nbformat_minor": 5 |
| 81 | +} |
0 commit comments