My mom is always bugging me to see what I'm laughing at on Reddit. Since she doesn't really want a Reddit account herself, I thought it would be a good idea to make a program that finds things she likes (cats) and sends them to her automatically via iMessage. I have the program send my Mom a cat a day via a cron job. Once I finished this project, I posted it to Reddit and was the top post for a day on r/Python, and was able to get 20 stars on the project GitHub page, which I am kinda stoked about; nothing better than some fake internet points to boost your self-esteem! On top of that, people's reaction on the subreddit was awesome - I got some very nice comments and even had a user DM me to learn how I did everything!
This project incorporates a Python script and an Applescript to get the job done. Below is an example of the whole thing in action:
get_cats.py
get_cats.py
is a Python script that uses requests
to search among a handful of cat subreddits. Thankfully, Reddit provides a JSON format for loaded content on a given
page for a subreddit (check out this example from r/Cats), so finding
the image, gif, or video associated with a post is not too difficult. Once I had a random post from a random choice of
a list of cat subreddits, the next step is downloading the media associated with that post. The three sources the script
is looking for are from gfycat.com, imgur.com, and of course Reddit itself. One element of the JSON format for a Reddit
page is the source, so the script simply checks this source and edits the download URL appropriately. From there, the
script uses a request to get the raw content of this URL and downloads this content to the local directory.
cat_msg.scpt
cat_msg.scpt
is an AppleScript I wrote to send the image or gif to my mom via iMessage. I had not
had any experience with AppleScript before, but I found it to be fairly straighforward. I did get stuck on the
pathing that AppleScripts and MacOS use for a bit - in all my time using a Mac, I never knew that paths in MacOS
used ':' as the separator rather than '/'. Thanks to some people on AppleScript related forums, I was able to write
this script to do what I wanted. The script in its entirety is below:
# to customize: change the things in all caps
# PATH_TO_REPOSITORY, RECIPIENT_PHONE_NUMBER, YOUR_APPLE_ID
# ginglis
on run argv
# find the cat file
set path_to_file to do shell script "find PATH_TO_REPOSITORY -mindepth 1 -name 'cat*'"
# set it as a POSIX file
set my_file to (path_to_file as POSIX file)
set post_title to (item 1 of argv)
# send stuff
tell application "Messages"
set theBuddy to buddy "RECIPIENT_PHONE_NUMBER" of service "E:YOUR_APPLE_ID"
send post_title to theBuddy
send my_file to theBuddy
end tell
end run
Cron Job
Similarly to my Magic Formula project, this project uses a cron job. I have the job executing daily so my Mom can get a cat a day.
I had a lot of fun with this project, and my mom has been enjoying getting a cat a day!