How I post to bear via email
Heya!
Welcome to the grizzly gazette!
This is a collaborative blog between ava, cris, absurdpirate, winther and me! (kami)
Obviously, in order to have a collaborative blog, everyone needs to be able to post to it.
You could just give every member the password, but that's not particularly great for security, and gets more and more dangerous the more people you add.
You can have people just dm their articles to one person thats running the blog, but that adds unnecessary friction, meaning that people will post less.
Both of these solutions kind of suck. But, fret not, there is actually a third solution to this problem!
Submissions via email!
Bearblog doesn't have this by default, but it is very much possible to implement!
Let me explain how I did it:
Alright. So, first up, in order to be able to do submissions via email, we first need to be able to receive emails.
Luckily, there's a python library that lets us do that!
It's called "simplegmail" and it does what it says on the tin: It lets us control a gmail account with python code. The setup is a bit more involved than that though.
I won't go to deep into how to actually get the credentials you need to get started because its really not that interesting and pretty well documented online. Just know that you need to faff around with google cloud for a bit to get a token instead of just being able to use your regular gmail login data.
Alright! The fun part! How do we post something to bear?
Well, how do you post something to bear?
You just log in, go to the post page, type in your post and click the post button, right?
We can do the same thing with code!
Thanks to the library "mechanicalsoup", we can use python code to control a browser to do what we want. So, I just tell it to open bear on the post page, type in the login details, and then click post!
And... that's really all there is to it.
Here's the python code for it!
user = "name_of_your_blog"
browser = mechanicalsoup.StatefulBrowser(user_agent=f'MechanicalSoup;https://{user}.bearblog.dev')
browser.open("https://bearblog.dev/" + user + "/dashboard/posts/new/")
browser.select_form()
browser["login"] = "your email"
browser["password"] = "your password"
browser.submit_selected()
browser.select_form()
browser["body_content"] = content
browser["publish"] = "true"
browser["header_content"] = "title: " + title
browser.submit_selected()
Anyways. Now we just use simplegmail to get all of our unread emails. Then we check whether or not they're from an adress that we want to allow to post on our blog, and then we post the contents of the email. Simple as that.
But that's only a one-time thing, right?
How do we make sure this happens every time we get an email?
That's also fairly easy to do.
First of all: You need a server. I got one from hetzner, but you can use whatever you want. (provided the server is running linux and lets you access the terminal.)
Now, you just have to make a bash script that runs the python script! Mine looks like this:
#!/bin/bash
cd /root/bearmail
source .venv/bin/activate
python mail.py
Once you've got that script set up, you just need to add it to your servers crontab. A crontab is basically a way to schedule tasks to run at a certain interval. In this case, we're going to run the script every 5 minutes.
For that, we just need to add this line to our crontab file:
*/5 * * * * /root/run.sh
This basically tells the operating system to run the script "run.sh" every 5th minute.
So, to summarize: We have a script on our server that gets run every 5 minutes. This script executes a python script which gets all of our unread emails in gmail. It then marks every email as read, checks if they're made by a person we want to allow publishing on our blog, and then posts the content of the email on bearblog by spinning up its own instance of chrome, going to bearblog.dev and making a new post. Aaaand that's it! A bit of a journey, but we got there.
I might publish the source code for this once I've cleaned it up to not be the worst thing ever - We'll see.
Thanks for reading!