From 8e9086475d1c15fae483718924b733ce3b926a13 Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Sun, 9 Jan 2022 13:24:17 +0100 Subject: [PATCH] Harmonize id --- src/Channel/SVT/__init__.py | 36 ++++++++++++++++++------------------ src/Utils/__init__.py | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/Channel/SVT/__init__.py b/src/Channel/SVT/__init__.py index 9e276ad..ef5fc9a 100644 --- a/src/Channel/SVT/__init__.py +++ b/src/Channel/SVT/__init__.py @@ -36,12 +36,12 @@ class SVT(Channel): published_parsed: datetime = datetime.now() video_id = str() - if svt_id == 'feed': + if self.m_id == 'feed': feed = feedparser.parse(self.get_feed()) entries = feed['entries'] for entry in entries: video_id = hash_string(entry['id']) - if video_exists(video_id, svt_id): + if video_exists(video_id, self.m_id): break for link in entry['links']: if str(link['type']).startswith('image/'): @@ -56,9 +56,9 @@ class SVT(Channel): datart = element.get('data-rt') if datart == 'top-area-play-button': - svt_id = href.split('=')[1].split('&')[0] + self.m_id = href.split('=')[1].split('&')[0] - resolved_link = resolve_link(svt_id) + resolved_link = self.resolve_link() description = str(entry['description']) published_parsed = entry['published_parsed'] title = str(entry['title']) @@ -68,12 +68,12 @@ class SVT(Channel): item = Item(description, resolved_link, self.m_provider_name, published_parsed, thumbnail, title) self.m_items.append(item) - add_video(video_id, svt_id, self.m_provider_name, description, + add_video(video_id, self.m_id, self.m_provider_name, description, resolved_link, published_parsed, thumbnail, title, 0) else: - chan_dict = resolve_svt_channel(svt_id) - resolved_link = resolve_link(svt_id) + chan_dict = resolve_svt_channel(self.m_id) + resolved_link = self.resolve_link() video_id = hash_string(resolved_link) title = chan_dict['name'] description = "Live channel stream" @@ -85,15 +85,15 @@ class SVT(Channel): self.m_items.append(item) -def resolve_link(svt_id: str) -> str: - api = json.loads( - requests.get('https://api.svt.se/video/{}'.format(svt_id)).text) - resolved_link = '' + def resolve_link(self) -> str: + api = json.loads( + requests.get('https://api.svt.se/video/{}'.format(self.m_id)).text) + resolved_link = '' - try: - for reference in api['videoReferences']: - if reference['format'] == "dashhbbtv": - resolved_link = reference['url'] - except KeyError: - pass - return resolved_link + try: + for reference in api['videoReferences']: + if reference['format'] == "dashhbbtv": + resolved_link = reference['url'] + except KeyError: + pass + return resolved_link diff --git a/src/Utils/__init__.py b/src/Utils/__init__.py index 8bf51b7..c5dd7a5 100644 --- a/src/Utils/__init__.py +++ b/src/Utils/__init__.py @@ -129,6 +129,21 @@ def get_latest(provider_id: str, pass return videos +def get_latest_video_timestamp(channel_id: str, + basepath: str = BASEPATH, + filename: str = DB_FILE_NAME) -> datetime: + fullpath = path.join(basepath, filename) + try: + con = sqlite3.connect(fullpath) + cur = con.cursor() + select_query = '''SELECT max(published) FROM {} WHERE channel_id = ?'''.format( + VIDEO_TABLE) + cur.execute(select_query, [channel_id]) + timestamp = cur.fetchone()[0] + except sqlite3.OperationalError: + timestamp = 0 + pass + return datetime.fromtimestamp(timestamp) def get_subscriptions(basepath: str = BASEPATH, filename: str = DB_FILE_NAME) -> list[tuple[str, str]]: