forked from Killerkiwi2005/lemontv-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbc.py
104 lines (79 loc) · 2.79 KB
/
bbc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from urllib.request import urlopen
import json
import re
import util
API_KEY = 'q5wcnsqvnacnhjap7gzts9y6' # This is the android app key
DATA_URL = 'http://ibl.api.bbci.co.uk/ibl/v1/channels/%s/programmes?lang=en&rights=tv&availability=available&api_key=%s&page=%s'
EPISODE_BASE = "http://www.bbc.co.uk/iplayer/episode/%s"
CHANNELS = ["bbc_one_london", "bbc_two_england", "bbc_three", "bbc_four" ,"cbbc", "cbeebies"]
# Programmes on BBC One
# http://ibl.api.bbci.co.uk/ibl/v1/channels?lang=en&api_key=q5wcnsqvnacnhjap7gzts9y6
# http://www.bbc.co.uk/iplayer/episode/[ID]
def get_listings():
shows = []
for channel in CHANNELS:
page = 0
data = None
while page == 0 or (data and len(data) > 0):
page = page + 1
url = DATA_URL % (channel, API_KEY, page)
print(url)
data = util.get_url_json(url)["channel_programmes"]["elements"]
for show_entry in data:
show_title = show_entry["title"]
for entry in show_entry["initial_children"]:
episode = {}
episode["show"] = entry["title"]
#episode["date"] = entry["release_date"]
episode["uri"] = EPISODE_BASE % entry["id"]
episode["s"] = 0
episode["e"] = 0
if "subtitle" in entry:
sub_title = entry["subtitle"]
episode["show"] = sub_title
#episode["e"] = sub_title
matches = re.search(r"Series (\d+)\: (\d+)", sub_title)
if matches:
episode["s"] = matches.group(1)
episode["e"] = matches.group(2)
else:
matches = re.search(r"Series (\d+)", sub_title)
if matches:
episode["s"] = matches.group(1)
else:
matches = re.search(r"Episode (\d+)", sub_title)
if matches:
episode["s"] = 1
episode["e"] = matches.group(1)
matches = re.search(r"Episode (\d+)", sub_title)
if matches:
if episode["s"] == 0:
episode["s"] = 1
episode["e"] = matches.group(1)
"""if episode["s"] != 0 and episode["e"] == 0:
title2 = entry.xpath(".//link[contains(@rel, 'self')]")[0].get('title')
matches = re.search(r"(\d+)", title2)
if matches:
episode["e"] = matches.group(1)
print("Episode b : ", episode["e"])"""
print (" - ", episode["s"], episode["e"], episode["show"])
print (" ", episode["uri"])
# Add to parent show
for x in shows:
# merge shows
if x["title"] == show_title:
x["episodes"] = x["episodes"] + [episode]
break
else:
# new show
show = {}
show["title"] = show_title
show["episodes"] = [episode]
show["type"] = "tv"
show["image"] = show_entry["images"]["standard"].replace("{recipe}","192x108")
shows.append(show)
return shows
if __name__ == "__main__":
f = open("bbc.js", "w")
f.write(json.dumps(get_listings()))
f.close()