From 2957e52ae0adaa7a878843bd7257ce4cab34e2f5 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 9 Aug 2020 13:24:33 -0500 Subject: [PATCH 1/6] Stub out Python script to generate Markov chains. --- bin/311_ebooks.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 bin/311_ebooks.py diff --git a/bin/311_ebooks.py b/bin/311_ebooks.py new file mode 100644 index 0000000..97aaa18 --- /dev/null +++ b/bin/311_ebooks.py @@ -0,0 +1,23 @@ + + +import markovify #https://github.com/jsvine/markovify +import sqlite3 #to read db + +# Open the db +conn = sqlite3.connect('requests.sqlite') +c = conn.cursor() + +def get_descriptions(): + c.execute('SELECT description FROM requests') + data = c.fetchall() + return(data) + +# Get descriptions and convert to strings from tuples +b = ["".join(str(x)) for x in get_descriptions()] + +# Build the model. +text_model = markovify.Text(b) + +# Print three randomly-generated sentences of no more than 280 characters +for i in range(3): + print(text_model.make_short_sentence(280)) From 89bbf68d7d440b34614c03c3a6510ce25c6e26f4 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 9 Aug 2020 15:41:21 -0500 Subject: [PATCH 2/6] Add default auth.ini. --- auth.ini.default | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 auth.ini.default diff --git a/auth.ini.default b/auth.ini.default new file mode 100644 index 0000000..b09e50b --- /dev/null +++ b/auth.ini.default @@ -0,0 +1,5 @@ +[mastodon] +#needs trailing slash +server = https://botsin.space/ +email = heck@yeah.com +password = ReallyGoodPassword From be3ee8031492b594cc3282cb28e105268326aed3 Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 9 Aug 2020 15:53:04 -0500 Subject: [PATCH 3/6] Finish script to toot from same account. --- bin/311_ebooks.py | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/bin/311_ebooks.py b/bin/311_ebooks.py index 97aaa18..f005354 100644 --- a/bin/311_ebooks.py +++ b/bin/311_ebooks.py @@ -2,6 +2,9 @@ import markovify #https://github.com/jsvine/markovify import sqlite3 #to read db +from mastodon import Mastodon +from configparser import ConfigParser +import os.path # Open the db conn = sqlite3.connect('requests.sqlite') @@ -13,11 +16,44 @@ def get_descriptions(): return(data) # Get descriptions and convert to strings from tuples -b = ["".join(str(x)) for x in get_descriptions()] +b = ["".join(str(x).replace("\\r\\n","")) for x in get_descriptions()] # Build the model. text_model = markovify.Text(b) # Print three randomly-generated sentences of no more than 280 characters -for i in range(3): - print(text_model.make_short_sentence(280)) +#for i in range(3): +# print(text_model.make_short_sentence(280)) + +# Next up use this to toot: https://github.com/halcy/Mastodon.py + +# parse existing file +config = ConfigParser() +config.read('auth.ini') + +# read values from a section +server = config.get('mastodon', 'server') +email = config.get('mastodon', 'email') +password = config.get('mastodon', 'password') + +# Register the app (once) +if not os.path.isfile('hackgfk_311_ebooks_clientcred.secret'): + Mastodon.create_app( + 'hackgfk_311_ebooks', + api_base_url = server, + to_file = 'hackgfk_311_ebooks_clientcred.secret' + ) + +# Log in +mastodon = Mastodon( + client_id = 'hackgfk_311_ebooks_clientcred.secret', + api_base_url = server, +) +mastodon.log_in( + email, + password, + to_file = 'hackgfk_311_ebooks_usercred.secret' +) + +# Send toot +mastodon.toot(text_model.make_short_sentence(280)) From 817073812c5075301360f5d9778412f2524ed3af Mon Sep 17 00:00:00 2001 From: Matt Date: Sun, 9 Aug 2020 15:54:56 -0500 Subject: [PATCH 4/6] Use specified section of auth.ini for each account. --- bin/311_ebooks.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/311_ebooks.py b/bin/311_ebooks.py index f005354..ac584a2 100644 --- a/bin/311_ebooks.py +++ b/bin/311_ebooks.py @@ -32,9 +32,9 @@ config = ConfigParser() config.read('auth.ini') # read values from a section -server = config.get('mastodon', 'server') -email = config.get('mastodon', 'email') -password = config.get('mastodon', 'password') +server = config.get('hackgfk_311_ebooks', 'server') +email = config.get('hackgfk_311_ebooks', 'email') +password = config.get('hackgfk_311_ebooks', 'password') # Register the app (once) if not os.path.isfile('hackgfk_311_ebooks_clientcred.secret'): From 12c975bab19244d7f0efbd9565276badd5b3dca3 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 10 Aug 2020 22:27:13 -0500 Subject: [PATCH 5/6] Print the toot when it toots. --- bin/311_ebooks.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/311_ebooks.py b/bin/311_ebooks.py index ac584a2..23d2f29 100644 --- a/bin/311_ebooks.py +++ b/bin/311_ebooks.py @@ -56,4 +56,6 @@ mastodon.log_in( ) # Send toot -mastodon.toot(text_model.make_short_sentence(280)) +toot = text_model.make_short_sentence(280) +print(toot) +mastodon.toot(toot) From 6483eec38b0cbc32a3ef4ac0e6f70eb5e202cdf3 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 11 Aug 2020 09:41:28 -0500 Subject: [PATCH 6/6] Update readme. --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 97b0530..912c366 100644 --- a/README.md +++ b/README.md @@ -4,13 +4,20 @@ This is a toy I built so I wouldn't have to check the [Grand Forks 311](http://w app/website all the time. It currently toots to Mastodon at [@hackgfk_311@botsin.space](https://botsin.space/@hackgfk_311). -Toots are converted to Tweets ([@hackgfk_311](https://twitter.com/hackgfk_311)) with @renatolond's wonderful +Toots are converted to tweets ([@hackgfk_311](https://twitter.com/hackgfk_311)) with @renatolond's wonderful crossposter: https://crossposter.masto.donte.com.br ([source code](https://github.com/renatolond/mastodon-twitter-poster)). Written in R because I'm much quicker at that than Python, and it seemed like a fun use case. Pull requests always welcome! +# 311_ebooks + +In Python (because why stay consistent?), this script takes the corpus out of the local 311 database +and builds an [ebooks-style](https://en.wikipedia.org/wiki/Horse_ebooks) toot using Markovify. + +Currently tooting at [@hackgfk_311_ebooks@botsin.space](https://botsin.space/@hackgfk_311_ebooks). + ----- # Hack Grand Forks