Add ebooks #22

Merged
mattbk merged 6 commits from 311_ebooks into master 2020-08-11 09:41:49 -05:00
3 changed files with 74 additions and 1 deletions

View File

@ -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

5
auth.ini.default Normal file
View File

@ -0,0 +1,5 @@
[mastodon]
#needs trailing slash
server = https://botsin.space/
email = heck@yeah.com
password = ReallyGoodPassword

61
bin/311_ebooks.py Normal file
View File

@ -0,0 +1,61 @@
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')
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).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))
# 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('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'):
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
toot = text_model.make_short_sentence(280)
print(toot)
mastodon.toot(toot)