From 7d0018f9840e28c0c5cf0a6a83a40ed1146e7993 Mon Sep 17 00:00:00 2001 From: Lee Smith Date: Fri, 1 Dec 2017 01:42:26 +0000 Subject: [plugin.video.btsportvideo] 0.1.0 (#1531) --- plugin.video.btsportvideo/LICENSE | 21 ++++ plugin.video.btsportvideo/README.md | 9 ++ plugin.video.btsportvideo/addon.py | 125 +++++++++++++++++++++ plugin.video.btsportvideo/addon.xml | 27 +++++ plugin.video.btsportvideo/resources/__init__.py | 0 plugin.video.btsportvideo/resources/fanart.jpg | Bin 0 -> 70327 bytes plugin.video.btsportvideo/resources/icon.png | Bin 0 -> 17881 bytes .../language/resource.language.en_gb/strings.po | 33 ++++++ .../resources/lib/__init__.py | 0 plugin.video.btsportvideo/resources/lib/api.py | 105 +++++++++++++++++ 10 files changed, 320 insertions(+) create mode 100644 plugin.video.btsportvideo/LICENSE create mode 100644 plugin.video.btsportvideo/README.md create mode 100644 plugin.video.btsportvideo/addon.py create mode 100644 plugin.video.btsportvideo/addon.xml create mode 100644 plugin.video.btsportvideo/resources/__init__.py create mode 100644 plugin.video.btsportvideo/resources/fanart.jpg create mode 100644 plugin.video.btsportvideo/resources/icon.png create mode 100644 plugin.video.btsportvideo/resources/language/resource.language.en_gb/strings.po create mode 100644 plugin.video.btsportvideo/resources/lib/__init__.py create mode 100644 plugin.video.btsportvideo/resources/lib/api.py diff --git a/plugin.video.btsportvideo/LICENSE b/plugin.video.btsportvideo/LICENSE new file mode 100644 index 0000000..b5d48b2 --- /dev/null +++ b/plugin.video.btsportvideo/LICENSE @@ -0,0 +1,21 @@ +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. diff --git a/plugin.video.btsportvideo/README.md b/plugin.video.btsportvideo/README.md new file mode 100644 index 0000000..8a27e69 --- /dev/null +++ b/plugin.video.btsportvideo/README.md @@ -0,0 +1,9 @@ +BT Sport Video plugin for Kodi +=========================================== +[![Travis CI](https://travis-ci.org/LS80/plugin.video.btsportvideo.svg?branch=master)](https://travis-ci.org/LS80/plugin.video.btsportvideo) + +A [Kodi](http://kodi.tv) plugin to watch videos from BT Sport at . + +The plugin can be installed from the official Kodi addon repository. + +Contact: [Leopold](https://forum.kodi.tv/private.php?action=send&uid=82951) on . diff --git a/plugin.video.btsportvideo/addon.py b/plugin.video.btsportvideo/addon.py new file mode 100644 index 0000000..2dd8802 --- /dev/null +++ b/plugin.video.btsportvideo/addon.py @@ -0,0 +1,125 @@ +############################################################################### +# +# 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. +# +############################################################################### + +import traceback + +from kodiswift import Plugin +import rollbar.kodi + +from resources.lib import api + + +PAGE_SIZE = 9 + +plugin = Plugin(addon_id='plugin.video.btsportvideo') + + +def categories(): + yield {'label': '[B]{}[/B]'.format(plugin.get_string(30002)), + 'path': plugin.url_for('search')} + yield {'label': '[B]{}[/B]'.format(plugin.get_string(30001)), + 'path': plugin.url_for('show_videos_by_category_first_page', category='all')} + for category in api.categories(): + yield {'label': category, + 'path': plugin.url_for('show_videos_by_category_first_page', + category=category)} + + +def items(func, route, page, **kwargs): + videos, npages = func(page=page, page_size=PAGE_SIZE, **kwargs) + + if page > 1: + yield { + 'label': u'[B]<< {} ({})[/B]'.format(plugin.get_string(30003), page - 1), + 'path': plugin.url_for(route, page=page - 1, **kwargs) + } + if page < npages: + yield { + 'label': u'[B]{} ({}) >> [/B]'.format(plugin.get_string(30004), page + 1), + 'path': plugin.url_for(route, page=page + 1, **kwargs) + } + + for video in videos: + yield { + 'label': video.title, + 'thumbnail': video.thumbnail, + 'path': video.url, + 'info': { + 'date': video.date.strftime('%d.%m.%Y'), + 'duration': video.duration + }, + 'is_playable': True + } + + +def show_videos(func, route, page, update_listing, **kwargs): + return plugin.finish( + items(func, route, page, **kwargs), + sort_methods=['playlist_order', 'date', 'title', 'duration'], + update_listing=update_listing + ) + + +@plugin.cached_route('/') +def index(): + return list(categories()) + + +@plugin.route('/videos/', name='show_videos_by_category_first_page', + options={'update_listing': False}) +@plugin.route('/videos//') +def show_videos_by_category(category, page='1', update_listing=True): + return show_videos(api.videos, 'show_videos_by_category', int(page), + update_listing, category=category) + + +@plugin.route('/search/', name='show_search_results_first_page', + options={'update_listing': False}) +@plugin.route('/search//') +def show_search_results(term, page='1', update_listing=True): + return show_videos(api.search_results, 'show_search_results', int(page), + update_listing, term=term) + + +@plugin.route('/search') +def search(): + term = plugin.keyboard(heading=plugin.get_string(30002)) + if term: + url = plugin.url_for('show_search_results_first_page', term=term) + plugin.redirect(url) + + +if __name__ == '__main__': + try: + plugin.run() + except Exception as exc: + if rollbar.kodi.error_report_requested(exc): + rollbar.kodi.report_error( + access_token='cc56a52c62c5418ebc87f97b1aa44669', + version=plugin.addon.getAddonInfo('version'), + url=plugin.request.url + ) + plugin.log.error(traceback.format_exc()) diff --git a/plugin.video.btsportvideo/addon.xml b/plugin.video.btsportvideo/addon.xml new file mode 100644 index 0000000..03c79a0 --- /dev/null +++ b/plugin.video.btsportvideo/addon.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + video + + + all + en + Watch videos from BT Sport + This plugin provides access to the videos from the BT Sport website + https://sport.bt.com + https://github.com/LS80/plugin.video.btsportvideo.git + MIT + + resources/icon.png + resources/fanart.jpg + + + + diff --git a/plugin.video.btsportvideo/resources/__init__.py b/plugin.video.btsportvideo/resources/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plugin.video.btsportvideo/resources/fanart.jpg b/plugin.video.btsportvideo/resources/fanart.jpg new file mode 100644 index 0000000..acbbbef Binary files /dev/null and b/plugin.video.btsportvideo/resources/fanart.jpg differ diff --git a/plugin.video.btsportvideo/resources/icon.png b/plugin.video.btsportvideo/resources/icon.png new file mode 100644 index 0000000..ea1c085 Binary files /dev/null and b/plugin.video.btsportvideo/resources/icon.png differ diff --git a/plugin.video.btsportvideo/resources/language/resource.language.en_gb/strings.po b/plugin.video.btsportvideo/resources/language/resource.language.en_gb/strings.po new file mode 100644 index 0000000..850394c --- /dev/null +++ b/plugin.video.btsportvideo/resources/language/resource.language.en_gb/strings.po @@ -0,0 +1,33 @@ +# Kodi Media Center language file +# Addon Name: BT Sport Video +# Addon id: plugin.video.btsportvideo +# Addon Provider: Leopold +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: YEAR-MO-DA HO:MI+ZONE\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: Kodi Translation Team\n" +"Language-Team: English (http://www.transifex.com/projects/p/xbmc-addons/language/en/)\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Language: en_GB\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" + +msgctxt "#30001" +msgid "All Videos" +msgstr "" + +msgctxt "#30002" +msgid "Search" +msgstr "" + +msgctxt "#30003" +msgid "Previous Page" +msgstr "" + +msgctxt "#30004" +msgid "Next Page" +msgstr "" diff --git a/plugin.video.btsportvideo/resources/lib/__init__.py b/plugin.video.btsportvideo/resources/lib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/plugin.video.btsportvideo/resources/lib/api.py b/plugin.video.btsportvideo/resources/lib/api.py new file mode 100644 index 0000000..72d63e4 --- /dev/null +++ b/plugin.video.btsportvideo/resources/lib/api.py @@ -0,0 +1,105 @@ +############################################################################### +# +# 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 BT Sport website +''' + +from urlparse import urljoin +from datetime import datetime +import time +from collections import namedtuple +import math + +import requests +from bs4 import BeautifulSoup + + +CATEGORIES_URL = 'http://sport.bt.com/all-videos/videos-01364228997406' +API_URL = 'http://api-search.sport.bt.com/search/sport/select' + +_Video = namedtuple('Video', 'title url thumbnail date duration') + + +def _video_params(page, page_size): + '''Returns the common params for the BT Sport API''' + return dict( + fq='AssetType:BTVideo', + sort='publicationdate desc', + wt='json', + start=(page - 1) * page_size, + rows=page_size, + ) + + +def _soup(path=''): + '''Returns a BeautifulSoup tree for the specified path''' + url = urljoin(CATEGORIES_URL, path) + response = requests.get(url) + return BeautifulSoup(response.text, 'html.parser') + + +def _date_from_str(date_str, fmt='%Y-%m-%dT%H:%M:%S.%fZ'): + '''Returns a data object from a string. + datetime.strptime is avoided due to an issue with Python in Kodi''' + return datetime(*(time.strptime(date_str, fmt)[0:6])).date() + + +def categories(): + '''Generator for all sport categories on the website''' + for category in _soup().find('div', 'video-hub-nav')('a'): + if not category.text.startswith('All '): + yield category.text + + +def _videos(videos_response): + for video in videos_response['docs']: + yield _Video( + title=video['h1title'], + url=video.get('hlsurl') or video.get('streamingurl'), + thumbnail=video['imageurl'], + date=_date_from_str(video['publicationdate']), + duration=int(video['duration']) + ) + + +def _video_results(page=1, page_size=10, **params): + params.update(_video_params(page, page_size)) + videos_response = requests.get(API_URL, params=params).json()['response'] + num_pages = int(math.ceil(float(videos_response['numFound']) / page_size)) + return _videos(videos_response), num_pages + + +def videos(category=None, page=1, page_size=10): + '''Returns a generator for videos in a category and the number of pages''' + category = None if category.lower() == 'all' else category + query = 'tags:("{}")'.format(category) if category else 'Publist:btsport' + return _video_results(page, page_size, q=query) + + +def search_results(term, page=1, page_size=10): + '''Returns a generator for video search results and the number of pages''' + return _video_results(page, page_size, q='text:{}'.format(term)) -- cgit v1.2.3