[Python] Calendar remote hosting

This is a simple script that allows you to periodically send your sonarr calendar file to a remote PHP server, from which you can retrieve the calendar at any time. This is useful for people that do not want to open their sonarr port and people that don’t use sonarr 24/7. I use it myself because I run sonarr on a PC which isn’t always on and I check upcoming episodes on my phone.

It’s a very simple combination of python and php scripts, but I thought I’d share it anyway because it might be helpful to people like me.

sonarr-calendar.py is run on the system with sonarr

import requests
sonarr_url = 'http://localhost:8989/feed/calendar/NzbDrone.ics?apikey=xxxxxxxxxxxxxxxxxxxx
hosting_url = 'http://your_hosting_url/sonarr-post.php’
r = requests.get(sonarr_url)
data = r.text
p = requests.post(hosting_url, data={file: data})
print p.text

sonarr-post.php saves the calendar on the webserver

<?php $content = file_get_contents("php://input"); if (!(empty($content))) { file_put_contents("calendar", $content); echo 1; } ?>

sonarr-get.php retrieves the calendar

<?php $content = file_get_contents("calendar"); if (!(empty($content))) { header('Content-type: text/calendar; charset=utf-8'); header('Content-Disposition: attachment; filename=calendar.ics'); echo substr(urldecode($content), 14); } ?>

The two php files need to be uploaded to a folder on a webserver that has writing permissions (chmod 777) and the python script needs to be ran periodically on a schedule or a cronjob. I’ve tested it on both linux and windows and it works fine. The python script requires the requests library. On linux you can use a cronjob, on windows the task scheduler. I update hourly myself.

If anyone wants to use my webserver that’s fine too, you can get personal URLs here.