From a8235de1ef85c93d648ed2de903478bc00d0d151 Mon Sep 17 00:00:00 2001 From: radical coder Date: Fri, 20 Sep 2019 09:03:44 -0700 Subject: [PATCH] Parse csv files recursively from folder, add debug mode (ref #7) --- README.md | 6 +-- ansible.yml | 11 ++--- templates/Dockerfile | 2 +- templates/main.py | 53 ++++++++++++++++-------- templates/{ => quotes}/marxistquotes.csv | 0 5 files changed, 44 insertions(+), 28 deletions(-) rename templates/{ => quotes}/marxistquotes.csv (100%) diff --git a/README.md b/README.md index e5d8f3a..b88ba8e 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,9 @@ First, copy `env.example` to `env`, then enter your actual login information. To execute the following commands: ```bash -cd communistquotes -sudo docker run -it --rm --name communistquotes --env-file env communistquotes -sudo docker run -it --rm --name communistquotes --env-file ../env communistquotes +cd communistquotes/templates/ +sudo docker build . -t communistquotes +sudo docker run -it --rm --name communistquotes --env-file ../env communistquotes --debug ``` ## Deploy diff --git a/ansible.yml b/ansible.yml index eb1ec33..a930ecd 100644 --- a/ansible.yml +++ b/ansible.yml @@ -17,13 +17,10 @@ - name: create folder for build files file: path=/tmp/communistquotes state=directory - - name: copy build files - copy: src=templates/{{item.filename}} dest=/tmp/communistquotes/{{item.filename}} - with_items: - - { filename: 'Dockerfile' } - - { filename: 'main.py' } - - { filename: 'marxistquotes.csv' } - - { filename: 'requirements.txt' } + # NOTE: It doesnt really make sense to call the folder "templates", when we actually dont use a + # template task... + - name: copy all build files recursively + copy: src=templates/ dest=/tmp/communistquotes - name: Build image using cache source docker_image: diff --git a/templates/Dockerfile b/templates/Dockerfile index a525d0e..e8e5281 100644 --- a/templates/Dockerfile +++ b/templates/Dockerfile @@ -7,4 +7,4 @@ RUN pip install --no-cache-dir -r requirements.txt COPY . . -CMD [ "python", "./main.py" ] +ENTRYPOINT [ "python", "./main.py" ] diff --git a/templates/main.py b/templates/main.py index be5af8e..2519a0d 100644 --- a/templates/main.py +++ b/templates/main.py @@ -6,23 +6,29 @@ import csv import itertools import random import os +import argparse instance = os.environ['MASTODON_INSTANCE'] username = os.environ['MASTODON_USERNAME'] +parser = argparse.ArgumentParser(description='Parse quotes from .csv files, and post a random quote to Mastodon API') +parser.add_argument('--debug', action='store_true', help='Dont actually login or post to the remote API') +args = parser.parse_args() + mastodon_api = None -# Create application if it does not exist -# TODO: store this file in volume -if not os.path.isfile(instance+'.secret'): - if Mastodon.create_app( - 'tootbot', - api_base_url='https://'+instance, - to_file = instance+'.secret' - ): - print('tootbot app created on instance '+instance) - else: - print('failed to create app on instance '+instance) - exit(1) +if not args.debug: + # Create application if it does not exist + # TODO: store this file in volume + if not os.path.isfile(instance+'.secret'): + if Mastodon.create_app( + 'tootbot', + api_base_url='https://'+instance, + to_file = instance+'.secret' + ): + print('tootbot app created on instance '+instance) + else: + print('failed to create app on instance '+instance) + exit(1) mastodon_api = Mastodon( client_id=instance+'.secret', @@ -35,9 +41,22 @@ if not os.path.isfile(instance+'.secret'): to_file=username+".secret" ) -with open('marxistquotes.csv') as csvfile: - csvreader = csv.reader(csvfile, delimiter=',', quotechar='`', skipinitialspace=True) - row = random.choice(list(csvreader)) +quotes = [] +for root, dirs, files in os.walk('quotes/'): + for name in files: + (base, ext) = os.path.splitext(name) + if ext == '.csv': + full_name = os.path.join(root, name) + with open(full_name) as csvfile: + csvreader = csv.reader(csvfile, delimiter=',', quotechar='`', skipinitialspace=True) + quotes += list(csvreader) - text = '

{}

    - {}, {}'.format(row[0], row[1], row[2], row[3]) - toot = mastodon_api.status_post(text, visibility='public', content_type='text/html') +print('Found %d total quotes' % len(quotes)) + +row = random.choice(quotes) + +text = '

{}

    - {}, {}'.format(row[0], row[1], row[2], row[3]) +print(text) + +if not args.debug: + mastodon_api.status_post(text, visibility='public', content_type='text/html') diff --git a/templates/marxistquotes.csv b/templates/quotes/marxistquotes.csv similarity index 100% rename from templates/marxistquotes.csv rename to templates/quotes/marxistquotes.csv