-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR adds a way of tracking the programs you follow. - Add a context menu to programs or episodes with either "Follow" or "Unfollow" - Download and cache favorites from VRT NU - Add an A-Z listing of followed programs - Add a Recent listing of followed programs - Add a TV guide of followed programs
- Loading branch information
Showing
20 changed files
with
422 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
|
||
from __future__ import absolute_import, division, unicode_literals | ||
import json | ||
import time | ||
|
||
from resources.lib.vrtplayer import tokenresolver | ||
|
||
try: | ||
from urllib.request import build_opener, install_opener, ProxyHandler, Request, urlopen | ||
except ImportError: | ||
from urllib2 import build_opener, install_opener, ProxyHandler, Request, urlopen | ||
|
||
|
||
class Favorites: | ||
|
||
def __init__(self, _kodiwrapper): | ||
self._kodiwrapper = _kodiwrapper | ||
self._tokenresolver = tokenresolver.TokenResolver(_kodiwrapper) | ||
self._proxies = _kodiwrapper.get_proxies() | ||
install_opener(build_opener(ProxyHandler(self._proxies))) | ||
self._cache_file = _kodiwrapper.get_userdata_path() + 'favorites.json' | ||
self._favorites = self.get_favorites() | ||
self.write_favorites() | ||
|
||
def get_favorites(self): | ||
if self._kodiwrapper.check_if_path_exists(self._cache_file): | ||
if self._kodiwrapper.stat_file(self._cache_file).st_mtime() > time.time() - (5 * 60): | ||
with self._kodiwrapper.open_file(self._cache_file) as f: | ||
return json.loads(f.read()) | ||
xvrttoken = self._tokenresolver.get_xvrttoken() | ||
headers = {'Content-Type': 'application/json', 'Cookie': 'X-VRT-Token=' + xvrttoken} | ||
self._kodiwrapper.log_notice('URL get: https://video-user-data.vrt.be/favorites/', 'Verbose') | ||
req = Request('https://video-user-data.vrt.be/favorites/', headers=headers) | ||
return json.loads(urlopen(req).read()) | ||
|
||
def set_favorite(self, path, value=True): | ||
if value is not self.is_favorite(path): | ||
xvrttoken = self._tokenresolver.get_xvrttoken() | ||
headers = {'Content-Type': 'application/json', 'Cookie': 'X-VRT-Token=' + xvrttoken} | ||
payload = dict(isFavorite=value, programUrl=path, title=path, whatsonId='0') | ||
self._kodiwrapper.log_notice('URL post: https://video-user-data.vrt.be/favorites/%s' % self.uuid(path), 'Verbose') | ||
self._kodiwrapper.log_notice('Payload: %s' % json.dumps(payload), 'Debug') | ||
req = Request('https://video-user-data.vrt.be/favorites/%s' % self.uuid(path), data=json.dumps(payload), headers=headers) | ||
# TODO: Test that we get a HTTP 200, otherwise log and fail graceful | ||
result = urlopen(req) | ||
if result.getcode() != 200: | ||
self._kodiwrapper.log_error("Failed to follow program '%s' at VRT NU" % path) | ||
# NOTE: Updates to favorites take a longer time to take effect, so we keep our own cache and use it | ||
self._favorites[self.uuid(path)] = dict(value=payload) | ||
self.write_favorites() | ||
|
||
def write_favorites(self): | ||
with self._kodiwrapper.open_file(self._cache_file, 'w') as f: | ||
f.write(json.dumps(self._favorites)) | ||
|
||
def is_favorite(self, path): | ||
value = False | ||
favorite = self._favorites.get(self.uuid(path)) | ||
if favorite: | ||
value = favorite.get('value', dict(isFavorite=False)).get('isFavorite', False) | ||
return value | ||
|
||
def follow(self, path): | ||
self.set_favorite(path, True) | ||
|
||
def unfollow(self, path): | ||
self.set_favorite(path, False) | ||
|
||
def uuid(self, path): | ||
return path.replace('/', '').replace('-', '') | ||
|
||
def name(self, path): | ||
return path.replace('.relevant/', '/').split('/')[-2] | ||
|
||
def names(self): | ||
return [self.name(p.get('value').get('programUrl')) for p in self._favorites.values() if p.get('value').get('isFavorite')] | ||
|
||
def titles(self): | ||
return [p.get('value').get('title') for p in self._favorites.values() if p.get('value').get('isFavorite')] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.