MagPi – auto-downloader

Grab each edition of Mag Pi as soon as possible!

MagPi notification

MagPi – auto-downloader is designed to notify your when the new magazine is available as a PDF. Why should you care about it? This is a great source of inspiration if you are familiar with the RPI already, or a great way to get you going if you are new to the RPI. Either way, the MagPi can be free if you decide to use PDF file only. Each month the file is uploaded to the server, but since the dates aren’t fixed, simple calendar reminder won’t work. The fun way of doing this is getting the Raspberry Pi to notify us about the new issue of the Mag Pi.

 

There are other alternatives to get the magazine as well.  These are various ways you could obtain your copy of MagPi:

  • Periodically check the https://www.raspberrypi.org/magpi/issues/
  • Sign up for a newsletter and you will get weekly updates including mentions when MagPi is out!
  • Download the app, and pay the subscription fee, to receive your MagPi as soon as it is available!
  • Subscribe and get the paper copy of the magazine delivered to your doorstep!
  • Follow @TheMagp1 on Twitter
  • You can follow this tutorial and hire your Raspberry Pi to do the job for you.

Requirements for MagPi auto-downloader

In this scenario, we will be sending the PDF download instructions to an Android device. Apart from phone or tablet, Raspberry PI, we will also need few things.

Tasker
Price: $2.99
AutoRemote
Price: $3.49

To install this, please open the terminal and use:

sudo apt-get install python3-pip
pip install requests
sudo apt-get install python3-bs4

The script and the Tasker project:

You will have to connect AutoRemote to the Raspberry Pi, and you are in luck as I already have the tutorial for you. Click here to set it up. The script checks each day if the new issue is available then notifies the device via AutoRemote.

To check if a new issue is available I use parsed details from the website. If a new entry is found, the link is composed and sent to your phone.

PYTHON 2: Magpi.py
import requests
import os
import pickle
from bs4 import BeautifulSoup, SoupStrainer
import urllib.request

"""
Authentication keys enter your keys
"""
auth_key_mob = 'YOUR_KEY_GOES_HERE'
auth_key_pc = 'YOUR_KEY_GOES_HERE'
auth_key_tab = 'YOUR_KEY_GOES_HERE'

#Function that sends the Autoremote to the desired device

message = ''
def send_AR(message, auth_key):
	url_mobile = 'https://autoremotejoaomgcd.appspot.com/sendmessage?key=' + auth_key + '&message='
	ar_message = url_mobile + message
	with urllib.request.urlopen(ar_message) as url:
		s = url.read()
	
#variables that we are going to use 

url = 'https://www.raspberrypi.org/magpi-issues/'
file_type = '.pdf'
response = requests.get(url)
dictLibraryOLD = {}
dictLibraryNEW = {}

#open last session - open it as OLD dictionary

try:
	with open('magpies.pickle', 'rb') as pilist:
		dictLibraryOLD = pickle.load(pilist)
except IOError:
	print('can\'t open')
	pass
try:
#look up a href links and create a dict, k = MagPi no v = link

	for link in BeautifulSoup(response.content, 'html.parser', parse_only=SoupStrainer('a')):
		if link.has_attr('href'):
			if file_type in link['href']:
				full_path = url + link['href']
				name = link['href']
				dictLibraryNEW[name] = full_path
	if len(dictLibraryOLD) == 0:
		print ('empty dictionary')
		#print(dictLibraryNEW) 
		
		#save current list to the file to be used as the next OLD dictionary
		
		with open('magpies.pickle', 'wb') as pilist:
			pickle.dump(dictLibraryNEW, pilist)
	else:
		#Compare 2 dictionaries and list the differences
		for key in dictLibraryNEW.keys():
			if not key in dictLibraryOLD:
				print(key)
				messageMobile = url + key + '=:=' + key
				print(messageMobile)
				send_AR(messageMobile, auth_key_mob)			
	
		with open('magpies.pickle', 'wb') as pilist:
			pickle.dump(dictLibraryNEW, pilist)
	
		#print(dictLibraryOLD)
		#print(dictLibraryNEW)
except:
	pass
TASKER PROJECT: MagPi
Profile: MagPi Available 

Event: AutoRemote [ Configuration:MagPi ]
Enter: Download Pi (11)

	A1: Variable Set [ Name:%MagPiurl To:%arpar1 Do Maths:Off Append:Off ]
	A2: AutoNotification [ Configuration:Use HTML: false
		Title: New issue of MagPi is available!
		Text: Expand to download %arcomm
		Action on Touch: Closeandwait
		Url: %arpar1
		Icon: android.resource://net.dinglisch.android.ipack.bluewhitepearlhd/creditcard2
		Status Bar Icon: ic_action_book
		Status Bar Text Size: 16
		Id: 314
		Is Group Summary: false
		Title Expanded: Touch and download now %arcomm
		Skip Picture Cache: false
		Text Expanded: Touch to download now!
		Button 1: Download
		Label 1: Download
		Icon 1: av_download
		Update Notification: false
		Only on Phone: false Timeout (Seconds):20 ]
Profile: On Touch 

Event: AutoApps Command [ Configuration:Command Filter: Download
	Case Insensitive: false
	Exact: false
	Regex: false
	Variable Array: false ]
Enter: Close Magpi 

	A1: AutoNotification Cancel [ Configuration:Id: 314
		Cancel All: false Timeout (Seconds):0 ]
	A2: Browse URL [ URL:%MagPiurl ]
	A3: Wait [ MS:0 Seconds:2 Minutes:0 Hours:0 Days:0 ]
	A4: Go Home [ Page:0

Schedule it!

Now that we have this sorted, time to schedule the job. We will use cron to execute the script once a day. MagPi comes out on various days, once a month. To get the timely notification we need to check for the new issues every day!
To set the schedule of MagPi – auto-downloader open the cron:

crontab -e

and add the line:

0 1 */1 * * python3 /path/to/your/file/MagPi_watcher.py &

Cron takes the commands that are structured in the following way: * * * * * command /path & – if you want it in the background. (*/1 = in a daily interval, & means run in the background)
Each * is responsible for a timer:

Crontab Scheedule
# * * * * * command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
And this is it. Your RPI will check for a new edition of MagPi each day at 1:00AM and will push a notification via Tasker! When the download is clicked, a pdf file will be saved to your default download location.
Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE