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.
 
cheers
Martin
 
 
 
 
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 = "http://www.bbc.co.uk/$(service)/programmes/schedules/$(region)yesterday.json"

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

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

def getSearch(service, region):
    try:
        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 = 'http://www.bbc.co.uk/iplayer/episode/'+pid
            yield (title,start,iplayer,subtitle,pid)        

    except Exception as ex:
        output ("Failed on service " + service + " " + str(ex) )
   
def searchForProgs(lines):
    dict={}
    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))
    searchForProgs(line)

[Reply]

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 11:43)

[Reply]

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... http://dnsdynamic.org/
also
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 13:56)

Add comment

Add comment

authimage