Creating a simple Reddit Bot!
Context:
This was a reddit bot that I created for a website that would automatically pick up a change in the directory structure on a server I was using, and would then post the contents of that directory to Reddit through a link.
Objective:
Build a reddit bot that once I add a new manual page, it will automatically pick that up and post it to Reddit in a specific period of time so that it is not spamming Reddit.
Technologies used:
- Python
- Reddit API
- Curl
- BASH
Process:
So firstly, I thought that I would start the Reddit bot off in Bash. I wanted to make sure I had the mechanism to detect the change in the man (manual) directory. For this, I got all the folders in the manual directory with no trailing /
like so:
ls -d */ | sed 's#/##
I assigned this statement to a variable, and I cycled through this variable:
for directory in $allDirectories ; doecho $directorydone
So this will get all the directories and cycle through them. However, the problem is that it always cycles through them all, and will always print them all. Regardless of how we do this, we’re going to have to cycle through all of the directories to get the change (we can potentially fix this), but what we want to change is we don’t want all of the directories to be outputted, we only want the one that is new to be outputted, so we need some logic for this.
The logic I though of to do this was to introduce a text file, “posts.txt”. In introducing this text file, I was thinking that I would be able to cycle through the directories, and check if they are present in the posts.txt file, if they are present, continue over to the next directory, and if they are not present, place the name in the posts.txt file, and continue on with the Reddit posting logic.
The following grep was used to perform this:
if grep -Fxq "$directory" posts.txt
then
continue
else
echo "$d" >> posts.txt
fi
Due to not caring the order in which the directories are posted at the start, I don’t have any type of sorting. Currently, there are about 9 folders in the manual directory, and once they have all posted, it will only detect the latest ones that are in there.
For the Reddit logic:
curl -X POST -d 'grant_type=password&username=username&password=password' --user 'clientId:clientSecret'
https://www.reddit.com/api/v1/access_token --user-agent "name of your bot 1.0"
This will then get you the access token that you’ll require for your account to be able to post to your account. Something worth mentioning is that this is a reddit bot using the script bot type. This means that it will post from the account that you registered the bot with.
This posts the output to the terminal console, so what we can do to automate this is the following:
curl -X POST -d 'grant_type=password&username=username&password=password' --user 'clientId:clientSecret'
https://www.reddit.com/api/v1/access_token --user-agent "name of your bot 1.0" > credentials
we can put the result into a file, then we can further manipulate the file:
sed -i 's/,.*$//' credentials
sed -i 's/{"access_token": "//' credentials
sed -i 's/.$//' credentials
This will make sure that all we get in the end is the access token itself without any padding around it.
Then you’ll want to read this back into a variable like so:
token=$(<credentials)
now we can post it using the reddit api:
curl -H "Authorization: bearer $token" -A "name of your bot 1.0" https://oauth.reddit.com/api/submit
-d "api_type=json&extension=json&sendreplies=true&
resubmit=true&kind=link&sr=NameOfYourSubreddit&title=$directory&url=http://YourWebsite.com"