Note: I'm not using any third party libraries like PyDrive for example, this script goes straight to the Google APIs. (PyDrive has not been touched for a long time by its author and is missing features).
Motion Google Drive Uploader on Github
Installation
Google setup:
- Go to https://code.google.com/apis/console - make sure you're logged in as the correct user for the Drive you want to use.
- Use the drop-down at the top of the page to create a project called 'Uploader'.
- Click on "Enable an API" then "Drive API" and then click the "Enable API" button
- Next go to "APIs & auth -> Credentials" menu and click "Create new Client ID".
- Set Application type to "Installed application" and click "Configure Consent Screen"
- Set the Product Name to "Uploader"
- You'll be returned back to the dialog. Make sure "Installed application" and "other" are selected and then click "Create Client ID".
- Download the JSON file.
- Rename it to client_secrets.json and put it somewhere like /home/pi/.
Script setup:
- Go to /home/pi/ and get the uploader.py from github: git clone https://github.com/jerbly/motion-uploader.git
- Update/install Google Python API: sudo pip install --upgrade google-api-python-client
- Make uploader.py executable: chmod a+x uploader.py
Configuration:
- If you used the git clone command you'll find the example uploader.cfg file in the motion-uploader directory. This now has a couple more options which need setting up.
- oauth/folder - set this to the directory where you have put the client_secrets.json file and where the script can write a credentials. e.g. /home/pi/
- docs/snapshot-folder - set this to a public folder name in your drive if you wish to use this feature (explained below).
Initial authentication:
- From the command line run the script by hand with your config file and a test avi file e.g. ./uploader.py /home/pi/uploader.cfg /home/pi/test.avi
- Copy the URL to your clipboard and paste it into a browser where you are already logged in to Google as the correct user.
- Accept and copy the authentication code to the clipboard.
- Paste it back in to the command line and hit enter.
- This will authenticate you and create a credentials file which will be used for future logins.
Now, finally, you can run up Motion and it should work like it used to.
New feature: Public Snapshot
Motion comes with a feature to periodically take a snapshot regardless of whether motion has been detected. This is a nice feature if you want to have a web site with the latest view from your webcam. You can use Google Drive to host this image rather than installing a web server on your Pi and opening firewalls etc.
- Create a public folder in your Google Drive:
- Create a new folder called 'public'
- Double click on it
- Go to the sharing menu (person icon with + sign)
- Click "Advanced" and then "Change..."
- Set it to "On - Public on the Web"
- Configure your uploader.cfg so docs/snapshot-folder is set to this folder 'public'.
- Configure motion.conf to take a snapshot every n seconds named lastsnap.jpg and upload it:
- snapshot_interval 300
- snapshot_filename lastsnap
- on_picture_save /home/pi/motion-uploader/uploader.py /home/pi/uploader.cfg %f snap
To find the public URL that you can bookmark and embed in other pages run the command line with the 'snapurl' option: ./uploader.py /home/pi/uploader.cfg /home/pi/lastsnap.jpg snapurl
It will print something like this: https://googledrive.com/host/{your-folder-id-here}/lastsnap.jpg
1 comment:
Maybe a human will read this through all the spam.
I've been using this on a raspberry pi 2 to back up my weather database every night. It's been running great for at least 3 years.
I'm upgrading everything to Python3/Ubuntu 20.10 on a Raspi3 and the last piece of the puzzle before I swap the old hardware for the "new" is to get my backup script working.
I have made some trivial modifications to get motion-uploader to work on Python 3. It works fine on small files. On the old Raspi2, I have no problems backing up the 100mb file. But on the Raspi3 and Ubuntu, it fails with "killed". It seems the oom_killer doesn't like it.
Any thoughts?
Post a Comment