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))


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 -
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved