import pywikibot as pwb
from pywikibot import pagegenerators as pg
site = pwb.Site("wikidata", "wikidata") # or pwb.Site("test", "wikidata") for test.wikidata.org
repo = site.data_repository()
series = pwb.ItemPage(repo, "Q34316");
first = pwb.ItemPage(repo, "Q2052558");
season = pwb.ItemPage(repo, "Q1170231");
pwb.config.noisysleep = 10
def Q(val):
    return pwb.ItemPage(repo, val);
def pad(val, tgt):
    valS = str(val)
    while len(valS) < tgt:
        valS = "0"+valS
    return valS

def next(episode):
    if len(episode.get()["claims"]["P156"]) != 1:
        print("Error, multiple following episodes")
        raise "Error"
    nxt = episode.get()["claims"]["P156"][0].getTarget();
    if nxt == Q("Q3492617") or nxt == Q("Q3396171"):
        nxt = next(nxt)
    return nxt;

def getSeason(episode):
    itsSeason  = episode.get()["claims"].get("P361");
    if itsSeason is None:
        return None;
    if len(itsSeason) != 1:
        raise "error";
    return itsSeason[0].getTarget();    


def printSeason(first, season):

    claimN = season.get()["claims"]["P1113"][0];
    data = claimN.getTarget().amount
    seasonId = season.get()["claims"]["P179"][0].qualifiers["P1545"][0].getTarget()

    episode = first
    for i in range(int(data)):
        print("S"+seasonId+"E"+pad(i+1,2)+" - " + episode.get()["labels"]["en"] + " " + str(episode));
        seas = getSeason(episode)
        if seas != season:
            if seas is None:
                #raise "Error"
                #tgtClaim=pwb.Claim(repo, "P361");
                #tgtClaim.setTarget(season);
                #episode.addClaim(tgtClaim);
                print("No season.")
            else:
                if seas == Q("Q34316"):
                    print("Only 'Doctor Who' as part of")
                else:
                    print("Wrong season: " + str(seas))#raise ValueError("error")
        
        episode = next(episode)           
    print()
    return episode

#ep = printSeason(first, season)
ep = printSeason(Q("Q2559908"), Q("Q1611836"))
ep = printSeason(ep, Q("Q1424152"))
ep = printSeason(ep, Q("Q1612231"))
ep = printSeason(ep, Q("Q1612410"))
ep = printSeason(ep, Q("Q1612508"))
ep = printSeason(ep, Q("Q964428"))
ep = printSeason(ep, Q("Q15303361"))
ep = printSeason(ep, Q("Q18622957"))
S2E01 - The Christmas Invasion [[wikidata:Q2559908]]
S2E02 - New Earth [[wikidata:Q773019]]
S2E03 - Tooth and Claw [[wikidata:Q2559890]]
S2E04 - School Reunion [[wikidata:Q2344493]]
S2E05 - The Girl in the Fireplace [[wikidata:Q2497642]]
S2E06 - Rise of the Cybermen [[wikidata:Q2559880]]
S2E07 - The Age of Steel [[wikidata:Q2559898]]
S2E08 - The Idiot's Lantern [[wikidata:Q2070373]]
S2E09 - The Impossible Planet [[wikidata:Q3211736]]
S2E10 - The Satan Pit [[wikidata:Q3211735]]
S2E11 - Love & Monsters [[wikidata:Q3205888]]
S2E12 - Fear Her [[wikidata:Q3258990]]
S2E13 - Army of Ghosts [[wikidata:Q2511242]]
S2E14 - Doomsday [[wikidata:Q2497678]]

S3E01 - The Runaway Bride [[wikidata:Q2082426]]
S3E02 - Smith and Jones [[wikidata:Q2079973]]
S3E03 - The Shakespeare Code [[wikidata:Q1089582]]
S3E04 - Gridlock [[wikidata:Q3202850]]
S3E05 - Daleks in Manhattan [[wikidata:Q2749947]]
S3E06 - Evolution of the Daleks [[wikidata:Q2749942]]
S3E07 - The Lazarus Experiment [[wikidata:Q3203418]]
S3E08 - 42 [[wikidata:Q2927335]]
S3E09 - Human Nature [[wikidata:Q3208621]]
S3E10 - The Family of Blood [[wikidata:Q3470536]]
S3E11 - Blink [[wikidata:Q1885659]]
S3E12 - Utopia [[wikidata:Q2075057]]
S3E13 - The Sound of Drums [[wikidata:Q2074999]]
S3E14 - Last of the Time Lords [[wikidata:Q3222182]]

S4E01 - Time Crash [[wikidata:Q3528925]]
S4E02 - Voyage of the Damned [[wikidata:Q2087589]]
S4E03 - Partners in Crime [[wikidata:Q2497631]]
S4E04 - The Fires of Pompeii [[wikidata:Q3207378]]
S4E05 - Planet of the Ood [[wikidata:Q3221008]]
S4E06 - The Sontaran Stratagem [[wikidata:Q2818910]]
S4E07 - The Poison Sky [[wikidata:Q2818909]]
S4E08 - The Doctor's Daughter [[wikidata:Q3208889]]
S4E09 - The Unicorn and the Wasp [[wikidata:Q2826460]]
S4E10 - Silence in the Library [[wikidata:Q2567880]]
S4E11 - Forest of the Dead [[wikidata:Q2901406]]
S4E12 - Midnight [[wikidata:Q3470543]]
S4E13 - Turn Left [[wikidata:Q3221321]]
S4E14 - The Stolen Earth [[wikidata:Q2089442]]
S4E15 - Journey's End [[wikidata:Q3208970]]
S4E16 - The Next Doctor [[wikidata:Q3008132]]
S4E17 - Planet of the Dead [[wikidata:Q3024843]]
S4E18 - The Waters of Mars [[wikidata:Q198440]]
S4E19 - The End of Time [[wikidata:Q2511224]]

S5E01 - The Eleventh Hour [[wikidata:Q2567868]]
S5E02 - The Beast Below [[wikidata:Q2586232]]
S5E03 - Victory of the Daleks [[wikidata:Q2617010]]
S5E04 - The Time of Angels [[wikidata:Q2604201]]
S5E05 - Flesh and Stone [[wikidata:Q2604151]]
S5E06 - The Vampires of Venice [[wikidata:Q3043797]]
S5E07 - Amy's Choice [[wikidata:Q3044343]]
S5E08 - The Hungry Earth [[wikidata:Q3212625]]
S5E09 - Cold Blood [[wikidata:Q3212626]]
S5E10 - Vincent and the Doctor [[wikidata:Q3043829]]
S5E11 - The Lodger [[wikidata:Q3053671]]
S5E12 - The Pandorica Opens [[wikidata:Q2669002]]
S5E13 - The Big Bang [[wikidata:Q2586347]]

S6E01 - A Christmas Carol [[wikidata:Q3062468]]
No season.
S6E02 - The Impossible Astronaut [[wikidata:Q2587141]]
No season.
S6E03 - Day of the Moon [[wikidata:Q2587136]]
No season.
S6E04 - The Curse of the Black Spot [[wikidata:Q2980673]]
No season.
S6E05 - The Doctor's Wife [[wikidata:Q3065061]]
No season.
S6E06 - The Rebel Flesh [[wikidata:Q2604141]]
No season.
S6E07 - The Almost People [[wikidata:Q3069535]]
No season.
S6E08 - A Good Man Goes to War [[wikidata:Q2604135]]
No season.
S6E09 - Let's Kill Hitler [[wikidata:Q2838646]]
No season.
S6E10 - Night Terrors [[wikidata:Q2718191]]
No season.
S6E11 - The Girl Who Waited [[wikidata:Q3069528]]
No season.
S6E12 - The God Complex [[wikidata:Q3069550]]
No season.
S6E13 - Closing Time [[wikidata:Q2718212]]
No season.
S6E14 - The Wedding of River Song [[wikidata:Q2718184]]
No season.

S7E01 - The Doctor, the Widow and the Wardrobe [[wikidata:Q401986]]
No season.
S7E02 - Asylum of the Daleks [[wikidata:Q1574878]]
S7E03 - Dinosaurs on a Spaceship [[wikidata:Q1573846]]
No season.
S7E04 - A Town Called Mercy [[wikidata:Q1574631]]
No season.
S7E05 - The Power of Three [[wikidata:Q3522296]]
No season.
S7E06 - The Angels Take Manhattan [[wikidata:Q1573859]]
No season.
S7E07 - The Snowmen [[wikidata:Q2945161]]
No season.
S7E08 - The Bells of St John [[wikidata:Q6357142]]
No season.
S7E09 - The Rings of Akhaten [[wikidata:Q7485410]]
No season.
S7E10 - Cold War [[wikidata:Q11746942]]
No season.
S7E11 - Hide (Doctor Who) [[wikidata:Q7612477]]
No season.
S7E12 - Journey to the Centre of the TARDIS [[wikidata:Q4188777]]
No season.
S7E13 - The Crimson Horror [[wikidata:Q7599244]]
No season.
S7E14 - Nightmare in Silver [[wikidata:Q6929712]]
No season.
S7E15 - The Name of the Doctor [[wikidata:Q11535738]]
S7E16 - The Day of the Doctor [[wikidata:Q5840086]]
Wrong season: [[wikidata:Q13359539]]
S7E17 - The Time of the Doctor [[wikidata:Q13432769]]
No season.

S8E01 - Deep Breath [[wikidata:Q17343514]]
No season.
S8E02 - Into the Dalek [[wikidata:Q17538180]]
No season.
S8E03 - Robot of Sherwood [[wikidata:Q17566356]]
No season.
S8E04 - Listen [[wikidata:Q17683155]]
No season.
S8E05 - Time Heist [[wikidata:Q17640044]]
S8E06 - The Caretaker [[wikidata:Q17639829]]
Only 'Doctor Who' as part of
S8E07 - Kill the Moon [[wikidata:Q18091479]]
Only 'Doctor Who' as part of
S8E08 - Mummy on the Orient Express [[wikidata:Q18145741]]
S8E09 - Flatline [[wikidata:Q18175806]]
S8E10 - In the Forest of the Night [[wikidata:Q18208019]]
Only 'Doctor Who' as part of
S8E11 - Dark Water [[wikidata:Q18324795]]
Only 'Doctor Who' as part of
S8E12 - Death in Heaven [[wikidata:Q18328139]]
No season.
S8E13 - 2014 Christmas special [[wikidata:Q18159602]]
No season.

S9E01 - The Magician's Apprentice [[wikidata:Q20667011]]
No season.
S9E02 - The Witch's Familiar [[wikidata:Q20667024]]
No season.
S9E03 - Under the Lake [[wikidata:Q20972559]]
No season.
S9E04 - Before the Flood [[wikidata:Q20978140]]
No season.
S9E05 - The Girl Who Died [[wikidata:Q20982237]]
S9E06 - The Woman Who Lived [[wikidata:Q21052419]]
S9E07 - The Zygon Invasion [[wikidata:Q21086508]]
No season.
S9E08 - The Zygon Inversion [[wikidata:Q21102197]]
No season.
S9E09 - Sleep No More [[wikidata:Q21139706]]
No season.
S9E10 - Face the Raven [[wikidata:Q21139720]]
No season.
S9E11 - Heaven Sent [[wikidata:Q21139731]]
No season.
S9E12 - Hell Bent [[wikidata:Q21148921]]

print(episode.get()["labels"]["en"])
Doctor Who: Children in Need