Compare commits

...

5 commits

Author SHA1 Message Date
Jessica L. Hacker
1e5c9b5554 Remove references to nomastodon flag 2019-09-20 08:55:25 -07:00
Jessica L. Hacker
141fbaf9c4 Combine debug and nomastodon into just debug 2019-09-20 08:53:45 -07:00
Jessica L. Hacker
3fb00c7a8a Update README.md 2019-09-19 09:49:12 -07:00
Jessica L. Hacker
3ce1fd3603 Add commandline argument to disable Mastodon 2019-09-19 09:44:52 -07:00
bebb4947fc Parse csv files recursively from folder, add debug mode (ref #7) 2019-09-17 23:03:15 +02:00
5 changed files with 56 additions and 32 deletions

View file

@ -15,9 +15,9 @@ First, copy `env.example` to `env`, then enter your actual login information. To
execute the following commands: execute the following commands:
```bash ```bash
cd communistquotes cd communistquotes/templates/
sudo docker run -it --rm --name communistquotes --env-file env communistquotes sudo docker build . -t communistquotes
sudo docker run -it --rm --name communistquotes --env-file ../env communistquotes sudo docker run -it --rm --name communistquotes --env-file ../env communistquotes --debug
``` ```
## Deploy ## Deploy
@ -25,4 +25,10 @@ sudo docker run -it --rm --name communistquotes --env-file ../env communistquote
Copy `inventory.example` to `inventory`, and enter your server's ssh address. Then run the following command: Copy `inventory.example` to `inventory`, and enter your server's ssh address. Then run the following command:
``` ```
ansible-playbook ansible.yml --become ansible-playbook ansible.yml --become
``` ```
## Options
Custom flags can be used when running `main.py`:
* `--debug`: Dont actually login or post to the remote API

View file

@ -17,13 +17,10 @@
- name: create folder for build files - name: create folder for build files
file: path=/tmp/communistquotes state=directory file: path=/tmp/communistquotes state=directory
- name: copy build files # NOTE: It doesnt really make sense to call the folder "templates", when we actually dont use a
copy: src=templates/{{item.filename}} dest=/tmp/communistquotes/{{item.filename}} # template task...
with_items: - name: copy all build files recursively
- { filename: 'Dockerfile' } copy: src=templates/ dest=/tmp/communistquotes
- { filename: 'main.py' }
- { filename: 'marxistquotes.csv' }
- { filename: 'requirements.txt' }
- name: Build image using cache source - name: Build image using cache source
docker_image: docker_image:

View file

@ -7,4 +7,4 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY . . COPY . .
CMD [ "python", "./main.py" ] ENTRYPOINT [ "python", "./main.py" ]

View file

@ -1,28 +1,36 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os import os
from mastodon import Mastodon
import csv import csv
import itertools import itertools
import random import random
import os import os
import argparse
instance = os.environ['MASTODON_INSTANCE'] parser = argparse.ArgumentParser(description='Parse quotes from .csv files, and post a random quote to Mastodon API')
username = os.environ['MASTODON_USERNAME'] parser.add_argument('--debug', action='store_true', help='Dont actually login or post to the remote API')
args = parser.parse_args()
instance = ""
username = ""
mastodon_api = None mastodon_api = None
# Create application if it does not exist if not args.debug:
# TODO: store this file in volume from mastodon import Mastodon
if not os.path.isfile(instance+'.secret'):
if Mastodon.create_app( instance = os.environ['MASTODON_INSTANCE']
'tootbot', username = os.environ['MASTODON_USERNAME']
api_base_url='https://'+instance, # Create application if it does not exist
to_file = instance+'.secret' # TODO: store this file in volume
): if not os.path.isfile(instance+'.secret'):
print('tootbot app created on instance '+instance) if Mastodon.create_app(
else: 'tootbot',
print('failed to create app on instance '+instance) api_base_url='https://'+instance,
exit(1) 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( mastodon_api = Mastodon(
client_id=instance+'.secret', client_id=instance+'.secret',
@ -35,9 +43,22 @@ if not os.path.isfile(instance+'.secret'):
to_file=username+".secret" to_file=username+".secret"
) )
with open('marxistquotes.csv') as csvfile: quotes = []
csvreader = csv.reader(csvfile, delimiter=',', quotechar='`', skipinitialspace=True) for root, dirs, files in os.walk('quotes/'):
row = random.choice(list(csvreader)) 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 = '<p>{}</p>&nbsp;&nbsp;&nbsp;&nbsp;- {}, <a href={}>{}</a>'.format(row[0], row[1], row[2], row[3]) print('Found %d total quotes' % len(quotes))
toot = mastodon_api.status_post(text, visibility='public', content_type='text/html')
row = random.choice(quotes)
text = '<p>{}</p>&nbsp;&nbsp;&nbsp;&nbsp;- {}, <a href={}>{}</a>'.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')