Compare commits
3 commits
master
...
dataValida
Author | SHA1 | Date | |
---|---|---|---|
|
ba790c39e2 | ||
|
02bbcefe7c | ||
bebb4947fc |
5 changed files with 72 additions and 28 deletions
|
@ -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
|
||||||
|
|
11
ansible.yml
11
ansible.yml
|
@ -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:
|
||||||
|
|
|
@ -7,4 +7,4 @@ RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
CMD [ "python", "./main.py" ]
|
ENTRYPOINT [ "python", "./main.py" ]
|
||||||
|
|
|
@ -6,23 +6,29 @@ import csv
|
||||||
import itertools
|
import itertools
|
||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
instance = os.environ['MASTODON_INSTANCE']
|
instance = os.environ['MASTODON_INSTANCE']
|
||||||
username = os.environ['MASTODON_USERNAME']
|
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
|
mastodon_api = None
|
||||||
# Create application if it does not exist
|
if not args.debug:
|
||||||
# TODO: store this file in volume
|
# Create application if it does not exist
|
||||||
if not os.path.isfile(instance+'.secret'):
|
# TODO: store this file in volume
|
||||||
if Mastodon.create_app(
|
if not os.path.isfile(instance+'.secret'):
|
||||||
'tootbot',
|
if Mastodon.create_app(
|
||||||
api_base_url='https://'+instance,
|
'tootbot',
|
||||||
to_file = instance+'.secret'
|
api_base_url='https://'+instance,
|
||||||
):
|
to_file = instance+'.secret'
|
||||||
print('tootbot app created on instance '+instance)
|
):
|
||||||
else:
|
print('tootbot app created on instance '+instance)
|
||||||
print('failed to create app on instance '+instance)
|
else:
|
||||||
exit(1)
|
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 +41,50 @@ if not os.path.isfile(instance+'.secret'):
|
||||||
to_file=username+".secret"
|
to_file=username+".secret"
|
||||||
)
|
)
|
||||||
|
|
||||||
with open('marxistquotes.csv') as csvfile:
|
"""
|
||||||
csvreader = csv.reader(csvfile, delimiter=',', quotechar='`', skipinitialspace=True)
|
get_random_quote: selects quotes randomly from a list until it finds one properly formated
|
||||||
row = random.choice(list(csvreader))
|
quotes: a list of comma separated values representing quotes
|
||||||
|
max_attempts: the number of times a random quote should be selected if the previously selectd quote is formatted incorrectly
|
||||||
|
return: the selected quote, or an empty list (if max_attempts was exceeded)
|
||||||
|
"""
|
||||||
|
def get_random_quote (quotes, max_attempts):
|
||||||
|
success = False
|
||||||
|
attempts = 0
|
||||||
|
quote = []
|
||||||
|
while (success == False and attempts < max_attempts):
|
||||||
|
success = True # will be set to false again if any values are bad
|
||||||
|
attempts = attempts + 1
|
||||||
|
print("Selecting a random quote... Attempt #{}".format(attempts))
|
||||||
|
quote = random.choice(quotes)
|
||||||
|
if len(quote) < 4:
|
||||||
|
print("Error: CSV has fewer than 4 columns.")
|
||||||
|
success = False
|
||||||
|
else:
|
||||||
|
for column in quote:
|
||||||
|
if column == "":
|
||||||
|
print("Error: CSV has at least one empty column.")
|
||||||
|
success = False
|
||||||
|
break # there is no need to continue looping over each column after the first failure
|
||||||
|
|
||||||
text = '<p>{}</p> - {}, <a href={}>{}</a>'.format(row[0], row[1], row[2], row[3])
|
# TODO: remove badly formated quote and append it to a csv file with other badly formated quotes
|
||||||
toot = mastodon_api.status_post(text, visibility='public', content_type='text/html')
|
return quote
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
print('Found %d total quotes' % len(quotes))
|
||||||
|
|
||||||
|
row = get_random_quote(quotes, 20)
|
||||||
|
|
||||||
|
text = '<p>{}</p> - {}, <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')
|
||||||
|
|
Loading…
Reference in a new issue