Skip to content

Commit

Permalink
0.3.7 #3 some more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kuriho committed Jun 9, 2023
1 parent 5463c49 commit 55d9ed7
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 89 deletions.
2 changes: 1 addition & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version='1.0' encoding='UTF-8'?>
<addon id="plugin.video.tver" name="TVer" version="0.3.5" provider-name="kuriho">
<addon id="plugin.video.tver" name="TVer" version="0.3.7" provider-name="kuriho">

<requires>
<import addon="xbmc.python" version="3.0.1" />
Expand Down
3 changes: 1 addition & 2 deletions lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from lib.utils import *
from lib.db import database
from lib.db import database, create_tables

from lib.cache import Cache
from lib.tver import *
from lib.favourites import Favourites
from lib.watcher import Watcher
from lib.mylist import MyList
Expand Down
48 changes: 34 additions & 14 deletions lib/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
import json

from time import time
from lib import database

class Cache:
def create(self):
with sql.connect(database()) as conn:
table = '''
CREATE TABLE IF NOT EXISTS categories (
id TEXT PRIMARY KEY NOT NULL,
expires INTEGER,
data JSON );
'''
conn.execute(table)
from lib.tver import fetch_episodes, URL_VIDEO_PICTURE, URL_VIDEO_WEBSITE
from lib import database, strip_or_none


class Cache:
def get(self, category):
data = None
self.delete_expired()

with sql.connect(database()) as conn:
cur = conn.execute(f'SELECT data FROM categories WHERE id = ?', (category,),)
Expand All @@ -40,7 +32,6 @@ def get_all(self):

return data


def insert(self, category, data, expire_after=14400.0):
expires_at = round(time() + expire_after)

Expand All @@ -50,4 +41,33 @@ def insert(self, category, data, expire_after=14400.0):

def delete_expired(self):
with sql.connect(database()) as conn:
conn.execute(f'DELETE FROM categories WHERE expires <= ?', (round(time()),),)
conn.execute(f'DELETE FROM categories WHERE expires <= ?', (round(time()),),)

def get_or_download(self, category):
json_episodes = self.get(category)
if not json_episodes:
json_episodes = fetch_episodes(category)
self.insert(category, json_episodes)
return json_episodes

def get_episodes(self, category):
json_episodes = self.get_or_download(category)

episodes = []

for episode in json_episodes['result']['contents']:
video_type = episode['type']

if video_type == 'episode':
series_id = episode['content']['seriesID']
video_id = episode['content']['id']
series_title = strip_or_none(episode['content']['seriesTitle'])
label = ' '.join(filter(None, [series_title, strip_or_none(episode['content']['title'])]))
episodes.append({ 'name': label,
'series': series_id,
'thumb': URL_VIDEO_PICTURE.format(video_type, video_id),
'video': URL_VIDEO_WEBSITE.format(video_type, video_id),
'genre': category,
'series_title': series_title })

return episodes
33 changes: 33 additions & 0 deletions lib/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@
from pathlib import Path
from xbmcvfs import translatePath

import sqlite3 as sql

def create_tables():
with sql.connect(database()) as conn:
table = '''
CREATE TABLE IF NOT EXISTS categories (
id TEXT PRIMARY KEY NOT NULL,
expires INTEGER,
data JSON );
'''
conn.execute(table)

table = '''
CREATE TABLE IF NOT EXISTS favourites (
id TEXT PRIMARY KEY NOT NULL,
category TEXT,
title TEXT
);
'''
conn.execute(table)

table = '''
CREATE TABLE IF NOT EXISTS mylist (
id TEXT PRIMARY KEY NOT NULL,
expires INTEGER,
vid_type TEXT,
title TEXT,
series TEXT
);
'''
conn.execute(table)


def get_filename():
addon = xbmcaddon.Addon()
cache_path = Path(translatePath(addon.getAddonInfo('profile')))
Expand Down
11 changes: 0 additions & 11 deletions lib/favourites.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@
from lib import database, localize

class Favourites:
def create(self):
with sql.connect(database()) as conn:
table = '''
CREATE TABLE IF NOT EXISTS favourites (
id TEXT PRIMARY KEY NOT NULL,
category TEXT,
title TEXT
);
'''
conn.execute(table)

def insert(self,category,series,title):
with sql.connect(database()) as conn:
conn.execute(f'INSERT OR REPLACE INTO favourites (id,category,title) VALUES (?,?,?)', (series, category, title,),)
Expand Down
16 changes: 1 addition & 15 deletions lib/mylist.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import sqlite3 as sql

from time import time
Expand All @@ -11,20 +10,7 @@
class MyList:
def __init__(self):
self.favourites = Favourites()

def create(self):
with sql.connect(database()) as conn:
table = '''
CREATE TABLE IF NOT EXISTS mylist (
id TEXT PRIMARY KEY NOT NULL,
expires INTEGER,
vid_type TEXT,
title TEXT,
series TEXT
);
'''
conn.execute(table)


def build(self):
mylist = [item[0] for item in self.select()]
favourites = self.favourites.select()
Expand Down
37 changes: 4 additions & 33 deletions lib/tver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
from lib import Cache
from lib import get_random_ua, strip_or_none, get_custom_img_path, localize

from lib import get_random_ua, get_custom_img_path, localize

URL_TOKEN_SERVICE = 'https://platform-api.tver.jp/v2/api/platform_users/browser/create'
URL_TAG_SEARCH_WS = 'https://platform-api.tver.jp/service/api/v1/callTagSearch/{}'
Expand Down Expand Up @@ -34,38 +34,9 @@ def fetch_api_token():
token = json_token['result']['platform_token']
return (uid,token)

def fetch_episodes(category):
cache = Cache()

cached_episodes = cache.get(category)
if cached_episodes != None:
return cached_episodes

def fetch_episodes(category):
(uid, token) = fetch_api_token()
resp = requests.get(URL_LIST_EPISODES.format(category, uid, token), headers={'x-tver-platform-type': 'web'}, timeout=10)
data = resp.json()

cache.insert(category, data)
return data

def get_episodes(category):
json_episodes = fetch_episodes(category)

episodes = []

for episode in json_episodes['result']['contents']:
video_type = episode['type']

if video_type == 'episode':
series_id = episode['content']['seriesID']
video_id = episode['content']['id']
series_title = strip_or_none(episode['content']['seriesTitle'])
label = ' '.join(filter(None, [series_title, strip_or_none(episode['content']['title'])]))
episodes.append({ 'name': label,
'series': series_id,
'thumb': URL_VIDEO_PICTURE.format(video_type, video_id),
'video': URL_VIDEO_WEBSITE.format(video_type, video_id),
'genre': category,
'series_title': series_title })

return episodes
return data
3 changes: 2 additions & 1 deletion lib/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import sqlite3 as sql

from urllib.parse import parse_qsl
from lib import Cache, lookup_db, find_episode, strip_or_none, URL_VIDEO_PICTURE
from lib.tver import URL_VIDEO_PICTURE
from lib import Cache, lookup_db, find_episode, strip_or_none

_URL = sys.argv[0]
_DB = lookup_db("MyVideos")
Expand Down
7 changes: 2 additions & 5 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import xbmcplugin

from lib import tver

from lib import Favourites
from lib import Watcher
from lib import MyList
from lib import Cache, Favourites, Watcher, MyList

from lib import log, show_info, check_if_kodi_supports_manifest, extract_info, extract_manifest_url_from_info, get_url, refresh, get_adaptive_type_from_url, localize, clear_thumbnails

Expand All @@ -30,7 +27,7 @@ def list_videos(category):
videos = Watcher().get_watching_episodes()

else:
videos = tver.get_episodes(category)
videos = Cache().get_episodes(category)
context = (localize(30021),'mylist')

for video in videos:
Expand Down
15 changes: 8 additions & 7 deletions service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from lib import Cache, tver, Favourites, MyList
from lib.tver import get_categories
from lib import Cache, MyList, create_tables

if __name__ == '__main__':
#initialize DB
mylist = MyList()
Cache().create()
Favourites().create()
mylist.create()
create_tables()

# cache warming
for (category, _, _) in tver.get_categories():
tver.fetch_episodes(category)
cache = Cache()
cache.delete_expired()
for (category, _, _) in get_categories():
cache.get_or_download(category)

# build MyList
mylist = MyList()
mylist.delete_expired()
mylist.build()

0 comments on commit 55d9ed7

Please sign in to comment.