Wednesday, April 09, 2014

Python searching BBC iPlayer

I have a lovely little Raspberry PI running a CRON job that sends me a list of favorite BBC iPlayer programs when thet are ready to download
I am a newbie Python'er so forgive any bad code, the 'BBC specific' code was from another blog and when I find it I will credit them, the BBC API documentation sucks
Create a file called SearchPrograms.txt with a list of programs you want to search for and it will output a list or URL's that can when clicked will take you to iPlayer for download. It attempts the HD version of everything which might fail.
import io
import json
from operator import itemgetter, attrgetter
from Common import *

import urllib.request

services = (
    ('bbcone', 'london/'),
    ('bbctwo', 'england/'),
    ('bbcthree', \[code\]),
    ('bbcfour', \[/code\]),

url = "$(service)/programmes/schedules/$(region)yesterday.json"

def getSearchStrings():
    result = []
    with'SearchPrograms.txt', 'r') as file:        
        while cont:
            line = file.readline()
            if len(line)==0:
            result.append( line.replace('\n','') )
    return result

def getJson(url):
        opener = urllib.request.FancyURLopener({})
        stream =
        result ="utf-8")    
        return result
    except Exception as ex:
        output ("getJson "+ url + str(ex) )
def getServiceJson(service, region):
    return getJson(url.replace("$(service)",service).replace("$(region)",region))

def getSearch(service, region):
        jsonStr = getServiceJson( service, region)
        jsonObj = json.loads(jsonStr)
        date = jsonObj['schedule']['day']['date']

        for show in jsonObj['schedule']['day']['broadcasts']:         
            start = show['start']
            program = show['programme']                
            title = program['display_titles']['title']
            subtitle = program['display_titles']['subtitle']
            pid = program['pid']
            iplayer = ''+pid
            yield (title,start,iplayer,subtitle,pid)        

    except Exception as ex:
        output ("Failed on service " + service + " " + str(ex) )
def searchForProgs(lines):
    for service in services:
        progs = getSearch(service[0],service[1])
        for prog in progs:
            title = prog[0]
            for line in lines:
                index = title.find(line)
                if index >= 0:                    
                    dict[prog[4]] = prog
    sorted(dict, key=itemgetter(0))

    for item in sorted(dict, key=itemgetter(0)):
        prog = dict[item]
        output(prog[0]+"-"+prog[3]+" | "+prog[1] + " <<" + prog[2] + "/hd>>")

def bbc():
    line = getSearchStrings()
    output( ',"'.join(line))


I did not know bbc has public json api. That is amazing!
What else do you do with your Raspberry PI? Do you use it as 'always on' home server?

Comment by Eric (05/15/2014 08:43)


It is def a cheap home server. Couple of times the PI has bailed so need to backup the PI image so can rebuild it quickly.

my pet projects:-

Got dynamic DNS update every hour with these guys...
I have CCTV cameras uploading to an FTP site so got a CRON job that deletes all files over 7 days.

I am a Microsoft sell out for 20 years but managed to setup lots on the PI, as email Server (full MTA), various Database servers, great tool

My next project is to take 4 CCTV feeds and multiplex them onto 1 video stream and publish that through PI web server.

All Python of course...

Comment by Martin (05/15/2014 10:56)

Add comment

Add comment