Project, Python, Research

Automating Making Memes & Instagram Posts 5: Moving Over My Code to the Raspberry Pi

Now that I can remote in to my Raspberry Pi, I can finally move over my code. Before I do that, I need to set up my environment the same way so everything works properly. Luckily, I take really great notes and completely stole the following steps from my post Python Virtual Environments 2: Venv, Virtualenv, & Virtualenvwrapper.

Good job, past-me!
sudo apt update
sudo apt upgrade
sudo apt autoremove
sudo apt install python3
sudo apt install python3-pip
sudo pip3 install virtualenvwrapper
which virtualenvwrapper.sh
sudo nano ~/.bashrc

I then copy and pasted the following into the bashrc file and usd Ctrl+X to save and close.

# default location of virtual environment directories
export WORKON๏ปฟ_HOME=$HOME/.virtualenvs
# default python version to use with virtualenv
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS=' -p /usr/bin/python3 '
source `which virtualenvwrapper.sh`

Back in the terminal I ran the command source ~/.bashrc, but I received an error: “bash: export: ‘WORKON_HOME=/home/pi/.virtualenvs’: not a valid identifier”. I went back into the /.bashrc file and retyped “export WORKON_HOME=” since it was likely a syntax error that occurred during the copy and paste (I’m using VNC Viewer at the moment to remotely control my raspberry pi). After the edit, “source ~/.bashrc” worked without any issues.

Why must everything be so difficult?

Next I went back through my post Automating Making Memes & Instagram Posts 1: Meme-Generator-Python to figure out how I got the meme generator installed.

First I had to download and install a specific font that the meme generator uses. I ran into a bit of trouble, but this raspberrytips article helped me navigate, and the following are the commands I did in order to get it working.

mkdir ~/.fonts
cd /home/pi/Downloads
cp *.ttf ~/.fonts/

Then I created my new environment that needs to use Python2 by typing the following commands.

cd .virtualenvs
virtualenv -p $(which python2) memebot_env
workon memebot_env
cd memebot_env
pip install wand

After that I downloaded and copy and pasted the meme-generator-python “memegen.py” file into my virtual environment folder. I also downloaded my David Rose sample photo and saved it into the memebot_env to test it out.

cd /home/pi/.virtualenvs/memebot_env/
python memegen.py "Sample" "Test" photo.jpg

Sadly, this didn’t work and I got the message “Error importing Wand! Try installing it with ‘pip install wand’“. I typed in pip install wand but it said “Requirement already satisfied”, so I have no idea what its problem is. So then I started Googling and trying like crazy to “fix” my pip installation command, hoping that eventually reinstalling wand would fix this error.

sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
sudo python get-pip.py

And FINALLY, I found the simplest solution on this blog post: hash -r. It clears the cache and suddenly everything is working again. Although “working” is a strong word since I STILL can’t import wand. *sigh* I eventually made my way to the wand installation documentation itself, hoping that this will somehow fix this mess.

hash -r
apt-get install libmagickwand-dev
sudo pip install wand
python memegen.py "Sample" "Test" photo.jpg
SUCCESS!!!!!!!!!!!!!!!!!

So yeah, APPARENTLY I didn’t have the other dependencies to let wand function properly, but WHATEVER. MOVING. ON. I’m FINE. It’s FINE.

Why is this how I spend my free time?


Next I skimmed through Automating Making Memes & Instagram Posts 2: Instagram-API-Python to remind myself how to install the Instagram-API-Python code.

workon memebot_env
pip install InstagramAPI

Everything eventually installed correctly but during the “Building wheel for numpy (setup.py)…” process my pi went to 100% CPU usage and its temperature went from 67 to over 80 degrees.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ

Eventually the CPU usage started to decline and so did the temperature, but I’m really glad I put those heatsinks on it to dissipate even a few degrees of heat since supposedly the pi3 begins to throttle the performance starting at 85 degrees. And obviously I don’t want my pi catching on fire, so I might make sure it has plenty of room for ventilation and a good reliable power source.

python
from InstagramAPI import InstagramAPI

I then went into my memebot_env folder and created a text file called “upload_photo.py” and pasted in the following (I edited the username, password, and filepath to match my rpi’s set up).

!/usr/bin/env python
-- coding: utf-8 --
#
Use text editor to edit the script and type in valid Instagram username/password
from InstagramAPI import InstagramAPI
InstagramAPI = InstagramAPI("type_username_here", "type_password_here")
InstagramAPI.login() # login
photo_path = 'filepath/photo.jpg'
caption = "Sample photo"
InstagramAPI.uploadPhoto(photo_path, caption=caption)

I went back to my terminal, used quit() to exit the python interpreter, and then gave permission to the “upload_photo.py” file to be ran as an executable by using the chmod command.

quit()
chmod u+x '/home/pi/.virtualenvs/memebot_env/upload_photo.py' 

Next it was time to copy over the improved code I made and install the google image downloader. I copied my laptop files onto my external harddrive that is now accessible via my network through the pi, and then copied them from there into my virtual environment folder.

After renaming the filepaths, I successfully ran the script. Of course, I had a few problems at first because I’m still a python newbie and I forgot to not only change my working directory to my folder, but also activate my virtual environment.

So for future-me’s sake, here’s a “cheat sheet” of commands on how to run this script (it is located in my “memebot_env” folder and the script name is “meme_poster”).

cd /home/pi/.virtualenvs/memebot_env/
workon memebot_env
python meme_poster.py

My final step before calling it good for the day was to install google-images-download and save some David Rose pictures.

pip install google_images_download
googleimagesdownload --keywords "David Rose Schitts Creek, Schitts Creek David" --limit 20

And with all of the above done, I am officially back to where I was at the end of my post Automating Making Memes & Instagram Posts 3: Improving the Script!

Hooray!

I needed to do one more thing before ending this monster of a post: map my harddrive to my Windows 10 environment. In this case I needed to enable samba so I followed this quick tutorial.

sudo apt-get install samba samba-common-bin
sudo nano /etc/samba/smb.conf

In “smb.conf” file I had to check a few variables and add my shared folder path to the file for sharing. After editing the two top variables and copying and pasting the [PiShare] text, I used Ctrl+X to save and close (Y to confirm changes).

workgroup = WORKGROUP
wins support = yes

[PiShare]
 comment=Raspberry Pi Share
 path=/media/pi/easystore
 browseable=Yes
 writeable=Yes
 only guest=no
 create mask=0777
 directory mask=0777
 public=no
I also had to remove the “#” at the beginning of the “wins support” line so it was not considered a comment and ignored

Next I ran the following command and entered a password for the newly created samba username “pi” .

sudo smbpasswd -a pi

On my Windows 10 desktop I first had to enable Samba Client and then restart my PC. After that “RaspberryPi” and the “PiShare” folder appeared. After I entered my username and password my external drive that I mapped to the “PiShare” samba config file loaded with all of my files!

Yes, it IS amazing!!!

I still have lots of work to do, but now I can access my raspberry pi from my desktop and laptop and not have to worry about managing multiple environments!

So more to come on this project, but for now, more cats from my Nikon pics folder! I can’t wait for this all to be automated so I spend my time taking pictures again, and not have to put any time into sharing them!

Leave a Reply