summaryrefslogtreecommitdiff
path: root/plugin.video.ecbtv/resources/lib/api.py
blob: c8072c8481b5e18eb06e34d5c6b8eeac33575155 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
###############################################################################
#
# MIT License
#
# Copyright (c) 2017 Lee Smith
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################

'''
Module for extracting video links from the England and Wales Cricket Board website
'''

import os
import re
from urlparse import urljoin, urlparse, urlunparse
from urllib import urlencode
from datetime import datetime
import time
from collections import namedtuple
import math

import requests
from bs4 import BeautifulSoup


BASE_URL = 'http://www.ecb.co.uk/'

HLS_HOST = 'https://secure.brightcove.com/'
HLS_URL_FMT = urljoin(HLS_HOST, 'services/mobile/streaming/index/master.m3u8?videoId={}')

PLAYER_THUMB_URL_FMT = 'https://ecb-resources.s3.amazonaws.com/player-photos/{}/480x480/{}.png'

SEARCH_URL = 'https://content-ecb.pulselive.com/search/ecb/'
VIDEO_LIST_URL = 'https://content-ecb.pulselive.com/content/ecb/EN/'

Video = namedtuple('Video', 'title url thumbnail date duration')
Entity = namedtuple('Entity', 'name reference thumbnail')


def _video_list_url(reference, page, page_size=10):
    '''Returns a URL for a list of videos'''
    url_parts = list(urlparse(VIDEO_LIST_URL))
    query_params = dict(
        contentTypes='video',
        references=reference if reference is not None else '',
        page=page - 1,
        pageSize=page_size
    )
    url_parts[4] = urlencode(query_params)
    return urlunparse(url_parts)


def _search_url(term, page, page_size=10):
    '''Returns a URL for the JSON search api'''
    url_parts = list(urlparse(SEARCH_URL))
    query_params = dict(
        type='VIDEO',
        fullObjectResponse=True,
        terms=term,
        size=page_size,
        start=(page - 1) * page_size
    )
    url_parts[4] = urlencode(query_params)
    return urlunparse(url_parts)


def _soup(path=''):
    '''Returns a BeautifulSoup tree for the specified path'''
    url = urljoin(BASE_URL, path)
    response = requests.get(url)
    return BeautifulSoup(response.text, 'html.parser')


def _date_from_str(date_str, fmt='%d %B %Y'):
    '''Returns a data object from a string.
       datetime.strptime is avoided due to a Python issue in Kodi'''
    return datetime(*(time.strptime(date_str, fmt)[0:6])).date()


def _date_json(json_item):
    '''Returns a date object from the JSON item.
       The date can be one of two formats'''
    date_str = json_item['date']
    for fmt in ['%Y-%m-%dT%H:%M', '%d/%m/%Y %H:%M']:
        try:
            date = _date_from_str(date_str.strip(), fmt=fmt)
        except ValueError as exc:
            continue
        else:
            return date
    raise exc


def _thumbnail_variant(video):
    if video['thumbnail'] is None:
        return
    return (variant['url'] for variant in video['thumbnail']['variants']
            if variant['tag']['id'] == 981).next()


def england():
    return Entity(
        name='England',
        reference='cricket_team:11',
        thumbnail=None
    )


def counties():
    for county in _soup('/county-championship/teams')('div', 'partners__item'):
        team_id = int(os.path.basename(county.a['href']))
        yield Entity(
            name=county.a.text,
            reference='cricket_team:{}'.format(team_id),
            thumbnail=county.img['src']
        )


def player_categories():
    for tab in _soup('/england/men/players').find_all(
            'div', attrs={'data-ui-args': re.compile(r'{ "title": "\w+" }')}):
        yield Entity(
            name=tab['data-ui-tab'],
            reference=None,
            thumbnail=None
        )


def players(category='Test'):
    soup = _soup('/england/men/players').find('div', attrs={'data-ui-tab': category})
    for player in soup('section', 'profile-player-card'):
        player_id = player.img['data-player']
        yield Entity(
            name=player.img['alt'],
            reference='cricket_player:{}'.format(player_id),
            thumbnail=PLAYER_THUMB_URL_FMT.format(category.lower(), player_id)
        )


def _video(video):
    return Video(
        title=video['title'],
        url=HLS_URL_FMT.format(video['mediaId']),
        thumbnail=_thumbnail_variant(video),
        date=_date_json(video),
        duration=video['duration']
    )


def _videos(videos_json):
    '''Generator for all videos from a particular page'''
    for video in videos_json['content']:
        yield _video(video)


def videos(reference=None, page=1, page_size=10):
    videos_json = requests.get(_video_list_url(reference, page, page_size)).json()
    npages = videos_json['pageInfo']['numPages']
    return _videos(videos_json), npages


def _search_results(search_results_json):
    '''Generator for videos matching a search term'''
    results = search_results_json['hits']['hit']
    for result in results:
        video = result['response']
        yield _video(video)


def search_results(term, page=1, page_size=10):
    search_results_json = requests.get(_search_url(term, page, page_size)).json()
    total = search_results_json['hits']['found']
    npages = int(math.ceil(float(total) / page_size))
    return _search_results(search_results_json), npages


def _print_team_videos():
    '''Test function to print all categories and videos'''
    for team in [england()] + list(counties()):
        print '{} ({})'.format(team.name, team.reference)
        videos_page, _num_pages = videos(team.reference)
        for video in videos_page:
            print '\t', video.title


def _print_search_results(term):
    '''Test function to print search results'''
    print 'Search: {}'.format(term)
    videos_page, _num_pages = search_results(term)
    for video in videos_page:
        print '\t', video.title


if __name__ == '__main__':
    _print_team_videos()
    print
    _print_search_results('test cricket')