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)



Congratulations!

If you can read this post, it means that the registration process was successful and that you can start blogging.
Please replace this with your first real post as soon as possible.
Happy Python blogging!



Copyright(c) 2017 - PythonBlogs.com
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved